]> git.saurik.com Git - wxWidgets.git/blobdiff - include/wx/unix/private/wakeuppipe.h
Don't define __STRICT_ANSI__, we should build both with and without it.
[wxWidgets.git] / include / wx / unix / private / wakeuppipe.h
index 2a942d05d7c5be90b4ada67c63b91ae1d0d4a71b..a601478a9f187c3ab704cc1d83d5549b4d9bbae0 100644 (file)
@@ -3,7 +3,6 @@
 // Purpose:     Helper class allowing to wake up the main thread.
 // Author:      Vadim Zeitlin
 // Created:     2013-06-09 (extracted from src/unix/evtloopunix.cpp)
-// RCS-ID:      $Id$
 // Copyright:   (c) 2013 Vadim Zeitlin <vadim@wxwidgets.org>
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 #ifndef _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
 #define _WX_UNIX_PRIVATE_WAKEUPPIPE_H_
 
-#include "wx/private/fdiohandler.h"
-
 #include "wx/unix/pipe.h"
+#include "wx/evtloopsrc.h"
 
 // ----------------------------------------------------------------------------
 // wxWakeUpPipe: allows to wake up the event loop by writing to it
 // ----------------------------------------------------------------------------
 
-class wxWakeUpPipe : public wxFDIOHandler
+// This class is not MT-safe, see wxWakeUpPipeMT below for a wake up pipe
+// usable from other threads.
+
+class wxWakeUpPipe : public wxEventLoopSourceHandler
 {
 public:
     // Create and initialize the pipe.
@@ -32,15 +33,17 @@ public:
     //
     // It simply writes to the write end of the pipe.
     //
-    // Notice that this method can be, and often is, called from another
-    // thread.
-    void WakeUp();
+    // As indicated by its name, this method does no locking and so can be
+    // called only from the main thread.
+    void WakeUpNoLock();
+
+    // Same as WakeUp() but without locking.
 
     // Return the read end of the pipe.
     int GetReadFd() { return m_pipe[wxPipe::Read]; }
 
 
-    // implement wxFDIOHandler pure virtual methods
+    // Implement wxEventLoopSourceHandler pure virtual methods
     virtual void OnReadWaiting();
     virtual void OnWriteWaiting() { }
     virtual void OnExceptionWaiting() { }
@@ -48,9 +51,6 @@ public:
 private:
     wxPipe m_pipe;
 
-    // Protects access to m_pipeIsEmpty.
-    wxCriticalSection m_pipeLock;
-
     // This flag is set to true after writing to the pipe and reset to false
     // after reading from it in the main thread. Having it allows us to avoid
     // overflowing the pipe with too many writes if the main thread can't keep
@@ -58,4 +58,44 @@ private:
     bool m_pipeIsEmpty;
 };
 
+// ----------------------------------------------------------------------------
+// wxWakeUpPipeMT: thread-safe version of wxWakeUpPipe
+// ----------------------------------------------------------------------------
+
+// This class can be used from multiple threads, i.e. its WakeUp() can be
+// called concurrently.
+#if wxUSE_THREADS
+
+class wxWakeUpPipeMT : public wxWakeUpPipe
+{
+public:
+    wxWakeUpPipeMT() { }
+
+    // Thread-safe wrapper around WakeUpNoLock(): can be called from another
+    // thread to wake up the main one.
+    void WakeUp()
+    {
+        wxCriticalSectionLocker lock(m_pipeLock);
+
+        WakeUpNoLock();
+    }
+
+    virtual void OnReadWaiting()
+    {
+        wxCriticalSectionLocker lock(m_pipeLock);
+
+        wxWakeUpPipe::OnReadWaiting();
+    }
+
+private:
+    // Protects access to m_pipeIsEmpty.
+    wxCriticalSection m_pipeLock;
+};
+
+#else // !wxUSE_THREADS
+
+typedef wxWakeUpPipe wxWakeUpPipeMT;
+
+#endif // wxUSE_THREADS/!wxUSE_THREADS
+
 #endif // _WX_UNIX_PRIVATE_WAKEUPPIPE_H_