+// 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_isOk = mutex && ((m_mutex = 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
+// ----------------------------------------------------------------------------
+
+// you should consider wxCriticalSectionLocker whenever possible instead of
+// directly working with wxCriticalSection class - it is safer
+#ifdef __WXMSW__
+ class WXDLLEXPORT wxCriticalSectionInternal;
+ #define WXCRITICAL_INLINE
+#else // !MSW
+ #define WXCRITICAL_INLINE inline
+#endif // MSW/!MSW
+class WXDLLEXPORT wxCriticalSection
+{
+public:
+ // ctor & dtor
+ WXCRITICAL_INLINE wxCriticalSection();
+ WXCRITICAL_INLINE ~wxCriticalSection();
+
+ // enter the section (the same as locking a mutex)
+ void WXCRITICAL_INLINE Enter();
+ // leave the critical section (same as unlocking a mutex)
+ void WXCRITICAL_INLINE Leave();
+
+private:
+ // no assignment operator nor copy ctor
+ wxCriticalSection(const wxCriticalSection&);
+ wxCriticalSection& operator=(const wxCriticalSection&);
+
+#ifdef __WXMSW__
+ wxCriticalSectionInternal *m_critsect;
+#else // !MSW
+ wxMutex m_mutex;
+#endif // MSW/!MSW
+};
+
+// wxCriticalSectionLocker is the same to critical sections as wxMutexLocker is
+// to th mutexes
+class WXDLLEXPORT wxCriticalSectionLocker
+{
+public:
+ wxCriticalSectionLocker(wxCriticalSection& critsect) : m_critsect(critsect)
+ { m_critsect.Enter(); }
+ ~wxCriticalSectionLocker()
+ { m_critsect.Leave(); }
+
+private:
+ // no assignment operator nor copy ctor
+ wxCriticalSectionLocker(const wxCriticalSectionLocker&);
+ wxCriticalSectionLocker& operator=(const wxCriticalSectionLocker&);
+
+ wxCriticalSection& m_critsect;
+};
+
+// ----------------------------------------------------------------------------