]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/wakeuppipe.h
23bcf12912f1f3a0aed639d0daff324477fd743e
[wxWidgets.git] / include / wx / unix / private / wakeuppipe.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/private/wakeuppipe.h
3 // Purpose: Helper class allowing to wake up the main thread.
4 // Author: Vadim Zeitlin
5 // Created: 2013-06-09 (extracted from src/unix/evtloopunix.cpp)
6 // RCS-ID: $Id$
7 // Copyright: (c) 2013 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
12 #define _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
13
14 #include "wx/unix/pipe.h"
15 #include "wx/evtloopsrc.h"
16
17 // ----------------------------------------------------------------------------
18 // wxWakeUpPipe: allows to wake up the event loop by writing to it
19 // ----------------------------------------------------------------------------
20
21 // This class is not MT-safe, see wxWakeUpPipeMT below for a wake up pipe
22 // usable from other threads.
23
24 class wxWakeUpPipe : public wxEventLoopSourceHandler
25 {
26 public:
27 // Create and initialize the pipe.
28 //
29 // It's the callers responsibility to add the read end of this pipe,
30 // returned by GetReadFd(), to the code blocking on input.
31 wxWakeUpPipe();
32
33 // Wake up the blocking operation involving this pipe.
34 //
35 // It simply writes to the write end of the pipe.
36 //
37 // As indicated by its name, this method does no locking and so can be
38 // called only from the main thread.
39 void WakeUpNoLock();
40
41 // Same as WakeUp() but without locking.
42
43 // Return the read end of the pipe.
44 int GetReadFd() { return m_pipe[wxPipe::Read]; }
45
46
47 // Implement wxEventLoopSourceHandler pure virtual methods
48 virtual void OnReadWaiting();
49 virtual void OnWriteWaiting() { }
50 virtual void OnExceptionWaiting() { }
51
52 private:
53 wxPipe m_pipe;
54
55 // This flag is set to true after writing to the pipe and reset to false
56 // after reading from it in the main thread. Having it allows us to avoid
57 // overflowing the pipe with too many writes if the main thread can't keep
58 // up with reading from it.
59 bool m_pipeIsEmpty;
60 };
61
62 // ----------------------------------------------------------------------------
63 // wxWakeUpPipeMT: thread-safe version of wxWakeUpPipe
64 // ----------------------------------------------------------------------------
65
66 // This class can be used from multiple threads, i.e. its WakeUp() can be
67 // called concurrently.
68 #if wxUSE_THREADS
69
70 class wxWakeUpPipeMT : public wxWakeUpPipe
71 {
72 public:
73 wxWakeUpPipeMT() { }
74
75 // Thread-safe wrapper around WakeUpNoLock(): can be called from another
76 // thread to wake up the main one.
77 void WakeUp()
78 {
79 wxCriticalSectionLocker lock(m_pipeLock);
80
81 WakeUpNoLock();
82 }
83
84 virtual void OnReadWaiting()
85 {
86 wxCriticalSectionLocker lock(m_pipeLock);
87
88 wxWakeUpPipe::OnReadWaiting();
89 }
90
91 private:
92 // Protects access to m_pipeIsEmpty.
93 wxCriticalSection m_pipeLock;
94 };
95
96 #else // !wxUSE_THREADS
97
98 typedef wxWakeUpPipe wxWakeUpPipeMT;
99
100 #endif // wxUSE_THREADS/!wxUSE_THREADS
101
102 #endif // _WX_UNIX_PRIVATE_WAKEUPPIPE_H_