wakeuppipe.h 3.0 KB

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