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