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)
7 // Copyright: (c) 2013 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
12 #define _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
14 #include "wx/private/fdiohandler.h"
16 #include "wx/unix/pipe.h"
18 // ----------------------------------------------------------------------------
19 // wxWakeUpPipe: allows to wake up the event loop by writing to it
20 // ----------------------------------------------------------------------------
22 // This class is not MT-safe, see wxWakeUpPipeMT below for a wake up pipe
23 // usable from other threads.
25 class wxWakeUpPipe
: public wxFDIOHandler
28 // Create and initialize the pipe.
30 // It's the callers responsibility to add the read end of this pipe,
31 // returned by GetReadFd(), to the code blocking on input.
34 // Wake up the blocking operation involving this pipe.
36 // It simply writes to the write end of the pipe.
38 // As indicated by its name, this method does no locking and so can be
39 // called only from the main thread.
42 // Same as WakeUp() but without locking.
44 // Return the read end of the pipe.
45 int GetReadFd() { return m_pipe
[wxPipe::Read
]; }
48 // implement wxFDIOHandler pure virtual methods
49 virtual void OnReadWaiting();
50 virtual void OnWriteWaiting() { }
51 virtual void OnExceptionWaiting() { }
56 // This flag is set to true after writing to the pipe and reset to false
57 // after reading from it in the main thread. Having it allows us to avoid
58 // overflowing the pipe with too many writes if the main thread can't keep
59 // up with reading from it.
63 // ----------------------------------------------------------------------------
64 // wxWakeUpPipeMT: thread-safe version of wxWakeUpPipe
65 // ----------------------------------------------------------------------------
67 // This class can be used from multiple threads, i.e. its WakeUp() can be
68 // called concurrently.
70 class wxWakeUpPipeMT
: public wxWakeUpPipe
75 // Thread-safe wrapper around WakeUpNoLock(): can be called from another
76 // thread to wake up the main one.
79 wxCriticalSectionLocker
lock(m_pipeLock
);
84 virtual void OnReadWaiting()
86 wxCriticalSectionLocker
lock(m_pipeLock
);
88 wxWakeUpPipe::OnReadWaiting();
92 // Protects access to m_pipeIsEmpty.
93 wxCriticalSection m_pipeLock
;
96 #endif // _WX_UNIX_PRIVATE_WAKEUPPIPE_H_