8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-02-01 00:43:03 +01:00
firebird-mirror/src/common/classes/locks.h

311 lines
5.6 KiB
C
Raw Normal View History

/*
* PROGRAM: Client/Server Common Code
* MODULE: locks.h
* DESCRIPTION: Single-state locks
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Nickolay Samofatov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
*/
2004-03-07 08:58:55 +01:00
#ifndef CLASSES_LOCKS_H
#define CLASSES_LOCKS_H
#include "firebird.h"
#include "../jrd/gdsassert.h"
#ifdef WIN_NT
// It is relatively easy to avoid using this header. Maybe do the same stuff like
// in thd.h ? This is Windows platform maintainers choice
#include <windows.h>
#else
#include <pthread.h>
#include <errno.h>
#endif
namespace Firebird {
class MemoryPool; // Needed for ctors that must always ignore it
class Exception; // Needed for catch
#ifdef WIN_NT
2006-05-03 07:44:26 +02:00
// Generic process-local mutex and spinlock. The latter
// is used to manage memory heaps in a threaded environment.
2003-01-20 19:38:34 +01:00
2006-05-03 07:44:26 +02:00
// Windows version of the class
2003-01-20 19:38:34 +01:00
class TryEnterCS
2008-01-23 20:03:16 +01:00
{
public:
TryEnterCS();
static bool tryEnter(LPCRITICAL_SECTION lpCS)
{
return ((*m_funct) (lpCS) == TRUE);
}
private:
2008-12-05 01:56:15 +01:00
typedef WINBASEAPI BOOL WINAPI tTryEnterCriticalSection
(LPCRITICAL_SECTION lpCriticalSection);
static tTryEnterCriticalSection* m_funct;
};
class Mutex
{
2006-05-03 07:44:26 +02:00
protected:
CRITICAL_SECTION spinlock;
#ifdef DEV_BUILD
const char* reason;
#endif
public:
2008-04-18 12:03:04 +02:00
Mutex()
{
2006-05-03 07:44:26 +02:00
InitializeCriticalSection(&spinlock);
}
2008-12-13 10:26:00 +01:00
explicit Mutex(MemoryPool&)
{
InitializeCriticalSection(&spinlock);
}
2008-04-18 12:03:04 +02:00
~Mutex()
{
#if defined DEV_BUILD && !defined WIN9X_SUPPORT
if (spinlock.OwningThread != 0)
DebugBreak();
#endif
DeleteCriticalSection(&spinlock);
}
#ifdef DEV_BUILD
void enter(const char* aReason)
{
EnterCriticalSection(&spinlock);
reason = aReason;
}
#endif
2008-04-18 12:03:04 +02:00
void enter()
{
EnterCriticalSection(&spinlock);
#ifdef DEV_BUILD
reason = "<..unspecified..>";
#endif
}
2008-04-18 12:03:04 +02:00
bool tryEnter()
{
return TryEnterCS::tryEnter(&spinlock);
}
2008-04-18 12:03:04 +02:00
void leave()
{
#if defined DEV_BUILD && !defined WIN9X_SUPPORT
// NS: This check is based on internal structure on CRITICAL_SECTION
// On 9X it works differently, and future OS versions may break this check as well
2008-09-15 01:17:58 +02:00
if ((U_IPTR) spinlock.OwningThread != GetCurrentThreadId())
DebugBreak();
#endif
LeaveCriticalSection(&spinlock);
}
2008-12-05 01:56:15 +01:00
public:
static void initMutexes() { }
};
2008-01-23 20:03:16 +01:00
class Spinlock : public Mutex
{
2006-05-03 07:44:26 +02:00
private:
2008-01-23 20:27:36 +01:00
void init();
2006-05-03 07:44:26 +02:00
public:
Spinlock()
{
init();
}
2008-01-23 20:27:36 +01:00
explicit Spinlock(MemoryPool&)
{
init();
}
2006-05-03 07:44:26 +02:00
};
2005-12-18 17:10:48 +01:00
#else //WIN_NT
2005-12-18 17:10:48 +01:00
// Pthreads version of the class
2008-01-23 20:03:16 +01:00
class Mutex
{
friend class Condition;
2003-11-12 00:58:49 +01:00
private:
pthread_mutex_t mlock;
static pthread_mutexattr_t attr;
#ifdef DEV_BUILD
const char* reason;
#endif
2008-02-06 01:43:54 +01:00
private:
2008-04-18 12:03:04 +02:00
void init()
{
int rc = pthread_mutex_init(&mlock, &attr);
if (rc)
system_call_failed::raise("pthread_mutex_init", rc);
}
public:
Mutex() { init(); }
explicit Mutex(MemoryPool&) { init(); }
2009-05-06 04:02:37 +02:00
2008-04-18 12:03:04 +02:00
~Mutex()
{
int rc = pthread_mutex_destroy(&mlock);
if (rc)
system_call_failed::raise("pthread_mutex_destroy", rc);
2003-11-12 00:58:49 +01:00
}
2009-05-06 04:02:37 +02:00
#ifdef DEV_BUILD
void enter(const char* aReason)
{
int rc = pthread_mutex_lock(&mlock);
if (rc)
system_call_failed::raise("pthread_mutex_lock", rc);
reason = aReason;
}
#endif
2008-04-18 12:03:04 +02:00
void enter()
{
int rc = pthread_mutex_lock(&mlock);
if (rc)
system_call_failed::raise("pthread_mutex_lock", rc);
#ifdef DEV_BUILD
reason = "<..unspecified..>";
#endif
}
2009-05-06 04:02:37 +02:00
2008-04-18 12:03:04 +02:00
bool tryEnter()
{
int rc = pthread_mutex_trylock(&mlock);
if (rc == EBUSY)
return false;
if (rc)
system_call_failed::raise("pthread_mutex_trylock", rc);
return true;
2003-11-12 00:58:49 +01:00
}
2009-05-06 04:02:37 +02:00
2008-04-18 12:03:04 +02:00
void leave()
{
int rc = pthread_mutex_unlock(&mlock);
if (rc)
system_call_failed::raise("pthread_mutex_unlock", rc);
2003-11-12 00:58:49 +01:00
}
2008-12-05 01:56:15 +01:00
public:
static void initMutexes();
2003-11-12 00:58:49 +01:00
};
2009-07-21 13:51:27 +02:00
#ifdef NOT_USED_OR_REPLACED // we do not use spinlocks currently
2008-01-23 20:03:16 +01:00
class Spinlock
{
2006-05-03 07:44:26 +02:00
private:
pthread_spinlock_t spinlock;
public:
2008-04-18 12:03:04 +02:00
Spinlock()
{
2006-05-03 07:44:26 +02:00
if (pthread_spin_init(&spinlock, false))
system_call_failed::raise("pthread_spin_init");
}
2009-05-06 04:02:37 +02:00
2008-04-18 12:03:04 +02:00
explicit Spinlock(MemoryPool&)
{
if (pthread_spin_init(&spinlock, false))
system_call_failed::raise("pthread_spin_init");
}
2009-05-06 04:02:37 +02:00
2008-04-18 12:03:04 +02:00
~Spinlock()
{
2006-05-03 07:44:26 +02:00
if (pthread_spin_destroy(&spinlock))
system_call_failed::raise("pthread_spin_destroy");
}
2009-05-06 04:02:37 +02:00
2008-04-18 12:03:04 +02:00
void enter()
{
2006-05-03 07:44:26 +02:00
if (pthread_spin_lock(&spinlock))
system_call_failed::raise("pthread_spin_lock");
}
2009-05-06 04:02:37 +02:00
2008-04-18 12:03:04 +02:00
void leave()
{
2006-05-03 07:44:26 +02:00
if (pthread_spin_unlock(&spinlock))
system_call_failed::raise("pthread_spin_unlock");
}
};
2009-07-21 10:58:28 +02:00
#else
2008-12-11 11:59:26 +01:00
typedef Mutex Spinlock;
2009-07-21 10:58:28 +02:00
#endif
2006-05-03 07:44:26 +02:00
2005-12-18 17:10:48 +01:00
#endif //WIN_NT
// RAII holder
class MutexLockGuard
{
public:
MutexLockGuard(Mutex &aLock, const char* aReason)
: lock(&aLock)
{
lock->enter(
#ifdef DEV_BUILD
aReason
#endif
);
}
explicit MutexLockGuard(Mutex &aLock)
: lock(&aLock)
2008-01-29 19:07:37 +01:00
{
lock->enter();
}
2008-12-05 01:56:15 +01:00
~MutexLockGuard()
{
try {
lock->leave();
}
2008-02-06 01:43:54 +01:00
catch (const Exception&)
{
DtorException::devHalt();
}
}
private:
// Forbid copying
MutexLockGuard(const MutexLockGuard&);
MutexLockGuard& operator=(const MutexLockGuard&);
2008-02-06 01:43:54 +01:00
Mutex* lock;
};
} //namespace Firebird
2004-03-07 08:58:55 +01:00
#endif // CLASSES_LOCKS_H