]> git.saurik.com Git - wxWidgets.git/commitdiff
Added wxCriticalSection::TryEnter() method.
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 1 Dec 2011 14:22:15 +0000 (14:22 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 1 Dec 2011 14:22:15 +0000 (14:22 +0000)
This is similar to wxMutex::TryLock() and useful for the same reasons.

Closes #13638.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@69883 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
include/wx/thread.h
interface/wx/thread.h
src/msw/thread.cpp
src/osx/carbon/thread.cpp
src/palmos/thread.cpp

index f3d6a10c09cb4e5c246ae1e84738b1f5c3fa06eb..ac3273f5bf56ff3e0f3e3f80ed123e6625726d89 100644 (file)
@@ -463,6 +463,7 @@ All:
   wxStopWatch precision.
 - Made wxGetLocalTimeMillis() really return local time, added
   wxGetUTCTimeMillis() returning what this function used to return.
+- Added wxCriticalSection::TryEnter() (Catalin Raceanu).
 
 All (GUI):
 
index ce17e6438d93ddc5b2aa1cc9a1f9bd02b5571e66..9718c3998e7848c3ce79b1adab7d5e1130af4314 100644 (file)
@@ -249,6 +249,9 @@ public:
     // enter the section (the same as locking a mutex)
     wxCRITSECT_INLINE void Enter();
 
+    // try to enter the section (the same as trying to lock a mutex)
+    wxCRITSECT_INLINE bool TryEnter();
+
     // leave the critical section (same as unlocking a mutex)
     wxCRITSECT_INLINE void Leave();
 
@@ -291,6 +294,7 @@ private:
     inline wxCriticalSection::~wxCriticalSection() { }
 
     inline void wxCriticalSection::Enter() { (void)m_mutex.Lock(); }
+    inline bool wxCriticalSection::TryEnter() { return m_mutex.TryLock() == wxMUTEX_NO_ERROR; }
     inline void wxCriticalSection::Leave() { (void)m_mutex.Unlock(); }
 #endif // wxCRITSECT_IS_MUTEX
 
index adfe7740916d08bfdfd19436d6e6c5c0ef3d7487..56d79333ccbb9f023fe80e480f32e9dba9c4b91c 100644 (file)
@@ -584,6 +584,14 @@ public:
     */
     void Enter();
 
+    /**
+        Try to enter the critical section (same as trying to lock a mutex).
+        If it can't, immediately returns false.
+
+        @since 2.9.3
+    */
+    bool TryEnter();
+
     /**
         Leave the critical section allowing other threads use the global data
         protected by it. There is no error return for this function.
index 58bb6013ba0491e7f13322b0f073a7ac9664dfc0..d57bbb9b0fc36826e75bc52891ef2ae324e2ea6a 100644 (file)
@@ -40,6 +40,8 @@
 
 #include "wx/except.h"
 
+#include "wx/dynlib.h"
+
 // must have this symbol defined to get _beginthread/_endthread declarations
 #ifndef _MT
     #define _MT
@@ -163,6 +165,25 @@ void wxCriticalSection::Enter()
     ::EnterCriticalSection((CRITICAL_SECTION *)m_buffer);
 }
 
+bool wxCriticalSection::TryEnter()
+{
+#if wxUSE_DYNLIB_CLASS
+    typedef BOOL
+      (WINAPI *TryEnterCriticalSection_t)(LPCRITICAL_SECTION lpCriticalSection);
+
+    static TryEnterCriticalSection_t
+        pfnTryEnterCriticalSection = (TryEnterCriticalSection_t)
+            wxDynamicLibrary(wxT("kernel32.dll")).
+                GetSymbol(wxT("TryEnterCriticalSection"));
+
+    return pfnTryEnterCriticalSection
+            ? (*pfnTryEnterCriticalSection)((CRITICAL_SECTION *)m_buffer) != 0
+            : false;
+#else
+    return false;
+#endif
+}
+
 void wxCriticalSection::Leave()
 {
     ::LeaveCriticalSection((CRITICAL_SECTION *)m_buffer);
index db0a03d9b76a8b1febc8a43475bc9ec214889904..429bf44ee44414ef01fd12f1acd18f23d1eb64db 100644 (file)
@@ -121,6 +121,11 @@ void wxCriticalSection::Enter()
     MPEnterCriticalRegion( (MPCriticalRegionID) m_critRegion, kDurationForever );
 }
 
+bool wxCriticalSection::TryEnter()
+{
+    return MPEnterCriticalRegion( (MPCriticalRegionID) m_critRegion, kDurationImmediate ) == noErr;
+}
+
 void wxCriticalSection::Leave()
 {
     MPExitCriticalRegion( (MPCriticalRegionID) m_critRegion );
index febc8f1a676c238bb336e1ba7a4c325e8280d57e..4939d6a054f7c06e56ecfc75acc2f65101c233bd 100644 (file)
@@ -146,6 +146,11 @@ void wxCriticalSection::Enter()
 {
 }
 
+bool wxCriticalSection::TryEnter()
+{
+    return false;
+}
+
 void wxCriticalSection::Leave()
 {
 }