]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/wakeuppipe.h
b2dda1635ca44e4aa62dc14476f14a6eafaa196f
[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/private/fdiohandler.h"
15
16 #include "wx/unix/pipe.h"
17
18 // ----------------------------------------------------------------------------
19 // wxWakeUpPipe: allows to wake up the event loop by writing to it
20 // ----------------------------------------------------------------------------
21
22 // This class is not MT-safe, see wxWakeUpPipeMT below for a wake up pipe
23 // usable from other threads.
24
25 class wxWakeUpPipe : public wxFDIOHandler
26 {
27 public:
28 // Create and initialize the pipe.
29 //
30 // It's the callers responsibility to add the read end of this pipe,
31 // returned by GetReadFd(), to the code blocking on input.
32 wxWakeUpPipe();
33
34 // Wake up the blocking operation involving this pipe.
35 //
36 // It simply writes to the write end of the pipe.
37 //
38 // As indicated by its name, this method does no locking and so can be
39 // called only from the main thread.
40 void WakeUpNoLock();
41
42 // Same as WakeUp() but without locking.
43
44 // Return the read end of the pipe.
45 int GetReadFd() { return m_pipe[wxPipe::Read]; }
46
47
48 // implement wxFDIOHandler pure virtual methods
49 virtual void OnReadWaiting();
50 virtual void OnWriteWaiting() { }
51 virtual void OnExceptionWaiting() { }
52
53 private:
54 wxPipe m_pipe;
55
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.
60 bool m_pipeIsEmpty;
61 };
62
63 // ----------------------------------------------------------------------------
64 // wxWakeUpPipeMT: thread-safe version of wxWakeUpPipe
65 // ----------------------------------------------------------------------------
66
67 // This class can be used from multiple threads, i.e. its WakeUp() can be
68 // called concurrently.
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 #endif // _WX_UNIX_PRIVATE_WAKEUPPIPE_H_