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
This commit is contained in:
Vadim Zeitlin
2011-12-01 14:22:15 +00:00
parent 4c5d5d633d
commit b9697cb410
6 changed files with 44 additions and 0 deletions

View 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):

View 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

View 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.

View 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);

View 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 );

View File

@@ -146,6 +146,11 @@ void wxCriticalSection::Enter()
{
}
bool wxCriticalSection::TryEnter()
{
return false;
}
void wxCriticalSection::Leave()
{
}