From b9697cb4104c115d39d10915b47e2381938020e0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 1 Dec 2011 14:22:15 +0000 Subject: [PATCH] Added wxCriticalSection::TryEnter() method. 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 | 1 + include/wx/thread.h | 4 ++++ interface/wx/thread.h | 8 ++++++++ src/msw/thread.cpp | 21 +++++++++++++++++++++ src/osx/carbon/thread.cpp | 5 +++++ src/palmos/thread.cpp | 5 +++++ 6 files changed, 44 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index f3d6a10c09..ac3273f5bf 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -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): diff --git a/include/wx/thread.h b/include/wx/thread.h index ce17e6438d..9718c3998e 100644 --- a/include/wx/thread.h +++ b/include/wx/thread.h @@ -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 diff --git a/interface/wx/thread.h b/interface/wx/thread.h index adfe774091..56d79333cc 100644 --- a/interface/wx/thread.h +++ b/interface/wx/thread.h @@ -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. diff --git a/src/msw/thread.cpp b/src/msw/thread.cpp index 58bb6013ba..d57bbb9b0f 100644 --- a/src/msw/thread.cpp +++ b/src/msw/thread.cpp @@ -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); diff --git a/src/osx/carbon/thread.cpp b/src/osx/carbon/thread.cpp index db0a03d9b7..429bf44ee4 100644 --- a/src/osx/carbon/thread.cpp +++ b/src/osx/carbon/thread.cpp @@ -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 ); diff --git a/src/palmos/thread.cpp b/src/palmos/thread.cpp index febc8f1a67..4939d6a054 100644 --- a/src/palmos/thread.cpp +++ b/src/palmos/thread.cpp @@ -146,6 +146,11 @@ void wxCriticalSection::Enter() { } +bool wxCriticalSection::TryEnter() +{ + return false; +} + void wxCriticalSection::Leave() { } -- 2.47.2