8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-23 06:03:02 +01:00

Made Mutex::tryEnter more efficient. Per Claudio request

This commit is contained in:
hvlad 2008-03-12 08:17:15 +00:00
parent 7131a02351
commit 6c17170820
2 changed files with 31 additions and 29 deletions

View File

@ -38,33 +38,16 @@ namespace Firebird {
#if defined(WIN_NT)
#define MISS_TRY_ENTER_CS ((tTryEnterCriticalSection*)(-1))
#define INIT_TRY_ENTER_CS ((tTryEnterCriticalSection*)(0))
TryEnterCS::tTryEnterCriticalSection* TryEnterCS::m_funct = &TryEnterCS::notImpl;
static TryEnterCS tryEnterCS;
tTryEnterCriticalSection* Mutex::TryEnterCriticalSection = INIT_TRY_ENTER_CS;
bool Mutex::tryEnter()
TryEnterCS::TryEnterCS()
{
if (TryEnterCriticalSection == MISS_TRY_ENTER_CS)
return false;
if (TryEnterCriticalSection == INIT_TRY_ENTER_CS)
{
HMODULE kernel32 = GetModuleHandle("kernel32.dll");
if (!kernel32) {
TryEnterCriticalSection = MISS_TRY_ENTER_CS;
return false;
}
TryEnterCriticalSection = (tTryEnterCriticalSection*)
HMODULE kernel32 = GetModuleHandle("kernel32.dll");
if (kernel32) {
m_funct = (tTryEnterCriticalSection*)
GetProcAddress(kernel32, "TryEnterCriticalSection");
if (!TryEnterCriticalSection) {
TryEnterCriticalSection = MISS_TRY_ENTER_CS;
system_call_failed::raise("TryEnterCriticalSection is not supported", 0);
return false;
}
}
return TryEnterCriticalSection(&spinlock);
}

View File

@ -57,14 +57,31 @@ class Exception; // Needed for catch
// Windows version of the class
typedef WINBASEAPI BOOL WINAPI tTryEnterCriticalSection
(LPCRITICAL_SECTION lpCriticalSection);
class TryEnterCS
{
public:
TryEnterCS();
static bool tryEnter(LPCRITICAL_SECTION lpCS)
{
return ((*m_funct) (lpCS) == TRUE);
}
private:
typedef WINBASEAPI BOOL WINAPI tTryEnterCriticalSection
(LPCRITICAL_SECTION lpCriticalSection);
static BOOL WINAPI notImpl(LPCRITICAL_SECTION)
{
system_call_failed::raise("TryEnterCriticalSection is not implemented", 0);
return false;
}
static tTryEnterCriticalSection* m_funct;
};
class Mutex
{
private:
static tTryEnterCriticalSection* TryEnterCriticalSection;
protected:
CRITICAL_SECTION spinlock;
@ -81,7 +98,9 @@ public:
void enter() {
EnterCriticalSection(&spinlock);
}
bool tryEnter();
bool tryEnter() {
return TryEnterCS::tryEnter(&spinlock);
}
void leave() {
LeaveCriticalSection(&spinlock);
}