+// a helper class which locks the mutex in the ctor and unlocks it in the dtor:
+// this ensures that mutex is always unlocked, even if the function returns or
+// throws an exception before it reaches the end
+class WXDLLEXPORT wxMutexLocker
+{
+public:
+ // lock the mutex in the ctor
+ wxMutexLocker(wxMutex& mutex) : m_mutex(mutex)
+ { m_isOk = m_mutex.Lock() == wxMUTEX_NO_ERROR; }
+
+ // returns TRUE if mutex was successfully locked in ctor
+ bool IsOk() const
+ { return m_isOk; }
+
+ // unlock the mutex in dtor
+ ~wxMutexLocker()
+ { if ( IsOk() ) m_mutex.Unlock(); }
+
+private:
+ // no assignment operator nor copy ctor
+ wxMutexLocker(const wxMutexLocker&);
+ wxMutexLocker& operator=(const wxMutexLocker&);
+
+ bool m_isOk;
+ wxMutex& m_mutex;
+};
+
+// ----------------------------------------------------------------------------
+// Critical section: this is the same as mutex but is only visible to the
+// threads of the same process. For the platforms which don't have native
+// support for critical sections, they're implemented entirely in terms of
+// mutexes.
+//
+// NB: wxCriticalSection object does not allocate any memory in its ctor
+// which makes it possible to have static globals of this class
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxCriticalSectionInternal;
+
+// in order to avoid any overhead under platforms where critical sections are
+// just mutexes make all wxCriticalSection class functions inline
+#if !defined(__WXMSW__) && !defined(__WXPM__)
+ #define WXCRITICAL_INLINE inline
+
+ #define wxCRITSECT_IS_MUTEX 1
+#else // MSW || OS2
+ #define WXCRITICAL_INLINE
+
+ #define wxCRITSECT_IS_MUTEX 0
+#endif // MSW/!MSW
+
+// you should consider wxCriticalSectionLocker whenever possible instead of
+// directly working with wxCriticalSection class - it is safer
+class WXDLLEXPORT wxCriticalSection
+{
+public:
+ // ctor & dtor
+ WXCRITICAL_INLINE wxCriticalSection();
+ WXCRITICAL_INLINE ~wxCriticalSection();
+
+ // enter the section (the same as locking a mutex)
+ WXCRITICAL_INLINE void Enter();
+ // leave the critical section (same as unlocking a mutex)
+ WXCRITICAL_INLINE void Leave();
+
+private:
+ // no assignment operator nor copy ctor
+ wxCriticalSection(const wxCriticalSection&);
+ wxCriticalSection& operator=(const wxCriticalSection&);
+
+#if wxCRITSECT_IS_MUTEX
+ wxMutex m_mutex;
+#elif defined(__WXMSW__)
+ // we can't allocate any memory in the ctor, so use placement new -
+ // unfortunately, we have to hardcode the sizeof() here because we can't
+ // include windows.h from this public header
+ char m_buffer[24];
+#elif !defined(__WXPM__)
+ wxCriticalSectionInternal *m_critsect;
+#else
+ // nothing for OS/2
+#endif // !Unix/Unix
+};
+
+// keep your preprocessor name space clean
+#undef WXCRITICAL_INLINE
+
+// wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
+// to th mutexes
+class WXDLLEXPORT wxCriticalSectionLocker
+{
+public:
+ inline wxCriticalSectionLocker(wxCriticalSection& critsect);
+ inline ~wxCriticalSectionLocker();
+
+private:
+ // no assignment operator nor copy ctor
+ wxCriticalSectionLocker(const wxCriticalSectionLocker&);
+ wxCriticalSectionLocker& operator=(const wxCriticalSectionLocker&);
+
+ wxCriticalSection& m_critsect;
+};
+
+// ----------------------------------------------------------------------------
+// wxCondition models a POSIX condition variable which allows one (or more)
+// thread(s) to wait until some condition is fulfilled
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxCondition
+{
+public:
+ // constructor and destructor
+
+ // Each wxCondition object is associated with with a wxMutex object The
+ // mutex object MUST be locked before calling Wait()
+ wxCondition(wxMutex& mutex);
+
+ // dtor is not virtual, don't use this class polymorphically
+ ~wxCondition();
+
+ // NB: the associated mutex MUST be locked beforehand by the calling thread
+ //
+ // it atomically releases the lock on the associated mutex
+ // and starts waiting to be woken up by a Signal()/Broadcast()
+ // once its signaled, then it will wait until it can reacquire
+ // the lock on the associated mutex object, before returning.
+ void Wait();
+
+ // exactly as Wait() except that it may also return if the specified
+ // timeout ellapses even if the condition hasn't been signalled: in this
+ // case, the return value is FALSE, otherwise (i.e. in case of a normal
+ // return) it is TRUE
+ //
+ // the timeeout parameter specifies a interval that needs to be waited in
+ // milliseconds
+ bool Wait( unsigned long timeout_millis );
+
+ // NB: the associated mutex may or may not be locked by the calling thread
+ //
+ // this method unblocks one thread if any are blocking on the condition.
+ // if no thread is blocking in Wait(), then the signal is NOT remembered
+ // The thread which was blocking on Wait(), will then reacquire the lock
+ // on the associated mutex object before returning
+ void Signal();
+
+ // NB: the associated mutex may or may not be locked by the calling thread
+ //
+ // this method unblocks all threads if any are blocking on the condition.
+ // if no thread is blocking in Wait(), then the signal is NOT remembered
+ // The threads which were blocking on Wait(), will then reacquire the lock
+ // on the associated mutex object before returning.
+ void Broadcast();
+
+private:
+ wxConditionInternal *m_internal;
+};
+
+// ----------------------------------------------------------------------------
+// wxSemaphore: a counter limiting the number of threads concurrently accessing
+// a shared resource
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxSemaphoreInternal;
+class WXDLLEXPORT wxSemaphore
+{