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

364 lines
6.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 "fb_atomic.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
#ifndef SOLARIS_MT
#include <pthread.h>
#else
#include <thread.h>
#include <synch.h>
#endif
#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:
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
{
2006-05-03 07:44:26 +02:00
protected:
CRITICAL_SECTION spinlock;
public:
2006-05-03 07:44:26 +02:00
Mutex() {
InitializeCriticalSection(&spinlock);
}
explicit Mutex(MemoryPool&) {
InitializeCriticalSection(&spinlock);
}
2006-05-03 09:06:23 +02:00
~Mutex() {
DeleteCriticalSection(&spinlock);
}
2006-05-03 09:06:23 +02:00
void enter() {
EnterCriticalSection(&spinlock);
}
bool tryEnter() {
return TryEnterCS::tryEnter(&spinlock);
}
2006-05-03 09:06:23 +02:00
void leave() {
LeaveCriticalSection(&spinlock);
}
public:
static void initMutexes() { }
};
2006-05-03 07:44:26 +02:00
typedef WINBASEAPI DWORD WINAPI tSetCriticalSectionSpinCount (
LPCRITICAL_SECTION lpCriticalSection,
DWORD dwSpinCount
);
2008-01-23 20:03:16 +01:00
class Spinlock : public Mutex
{
2006-05-03 07:44:26 +02:00
private:
static tSetCriticalSectionSpinCount* SetCriticalSectionSpinCount;
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
#ifdef SOLARIS_MT
2005-02-17 13:42:49 +01:00
#error: Make mutexes on Solaris recursive!
2008-01-23 20:03:16 +01:00
class Mutex
{
private:
2005-12-18 17:10:48 +01:00
mutex_t mlock;
public:
Mutex() {
2005-12-18 17:10:48 +01:00
if (mutex_init(&mlock, USYNC_PROCESS, NULL))
system_call_failed::raise("mutex_init");
}
explicit Mutex(MemoryPool&) {
if (mutex_init(&mlock, USYNC_PROCESS, NULL))
system_call_failed::raise("mutex_init");
}
~Mutex() {
2005-12-18 17:10:48 +01:00
if (mutex_destroy(&mlock))
system_call_failed::raise("mutex_destroy");
}
void enter() {
2005-12-18 17:10:48 +01:00
if (mutex_lock(&mlock))
system_call_failed::raise("mutex_lock");
}
void leave() {
2005-12-18 17:10:48 +01:00
if (mutex_unlock(&mlock))
system_call_failed::raise("mutex_unlock");
}
public:
static void initMutexes() { }
};
2005-12-18 17:10:48 +01:00
2006-05-03 07:44:26 +02:00
typedef Mutex Spinlock;
2005-12-18 17:10:48 +01:00
#else //SOLARIS_MT
// Pthreads version of the class
2008-01-23 20:03:16 +01:00
class Mutex
{
2003-11-12 00:58:49 +01:00
private:
pthread_mutex_t mlock;
static pthread_mutexattr_t attr;
2008-02-06 01:43:54 +01:00
private:
void init() {
int rc = pthread_mutex_init(&mlock, &attr);
if (rc < 0)
system_call_failed::raise("pthread_mutex_init");
}
public:
Mutex() { init(); }
explicit Mutex(MemoryPool&) { init(); }
~Mutex() {
int rc = pthread_mutex_destroy(&mlock);
if (rc < 0)
system_call_failed::raise("pthread_mutex_destroy");
2003-11-12 00:58:49 +01:00
}
void enter() {
int rc = pthread_mutex_lock(&mlock);
if (rc < 0)
system_call_failed::raise("pthread_mutex_lock");
2003-11-12 00:58:49 +01:00
}
void leave() {
int rc = pthread_mutex_unlock(&mlock);
if (rc < 0)
system_call_failed::raise("pthread_mutex_unlock");
2003-11-12 00:58:49 +01:00
}
public:
static void initMutexes();
2003-11-12 00:58:49 +01:00
};
2007-06-11 16:21:33 +02:00
#ifndef DARWIN
2008-01-23 20:03:16 +01:00
class Spinlock
{
2006-05-03 07:44:26 +02:00
private:
pthread_spinlock_t spinlock;
public:
Spinlock() {
if (pthread_spin_init(&spinlock, false))
system_call_failed::raise("pthread_spin_init");
}
explicit Spinlock(MemoryPool&) {
if (pthread_spin_init(&spinlock, false))
system_call_failed::raise("pthread_spin_init");
}
2006-05-03 07:44:26 +02:00
~Spinlock() {
if (pthread_spin_destroy(&spinlock))
system_call_failed::raise("pthread_spin_destroy");
}
void enter() {
if (pthread_spin_lock(&spinlock))
system_call_failed::raise("pthread_spin_lock");
}
void leave() {
if (pthread_spin_unlock(&spinlock))
system_call_failed::raise("pthread_spin_unlock");
}
};
2007-06-11 16:21:33 +02:00
#endif //DARWIN
2006-05-03 07:44:26 +02:00
2005-12-18 17:10:48 +01:00
#endif //SOLARIS_MT
#endif //WIN_NT
// mutex with reference count
// This class is useful if mutex can be "deleted" by the
// code between enter and leave calls
class RefMutex : public Mutex
{
public:
RefMutex() : Mutex()
{
m_refcnt = 0;
}
explicit RefMutex(MemoryPool& pool) : Mutex(pool)
{
m_refcnt = 0;
}
int addRef()
{
return ++m_refcnt;
}
int release()
{
const int ret = --m_refcnt;
if (!ret) {
delete this;
}
return ret;
}
private:
~RefMutex() {}
AtomicCounter m_refcnt;
};
// RAII holder
class MutexLockGuard
{
public:
explicit MutexLockGuard(Mutex &alock)
2008-01-29 19:07:37 +01:00
: lock(&alock)
{
lock->enter();
}
~MutexLockGuard()
{
try {
lock->leave();
}
2008-02-06 01:43:54 +01:00
catch (const Exception&)
{
onDtorException();
}
}
static void onDtorException()
{
// If LockGuard's dtor is executed during exception processing,
// (remember - this guards live on the stack), exception
// in leave() causes std::terminate() to be called, therefore
// loosing original exception information. Not good for us.
// Therefore ignore in release and abort in debug.
#ifdef DEV_BUILD
halt();
#endif
}
2008-01-29 19:07:37 +01:00
private:
// Forbid copy constructor
MutexLockGuard(const MutexLockGuard& source);
MutexLockGuard(RefMutex*);
MutexLockGuard(RefMutex&);
2008-02-06 01:43:54 +01:00
#ifdef DEV_BUILD
static void halt();
#endif
2008-02-06 01:43:54 +01:00
Mutex* lock;
};
class RefMutexGuard
{
public:
explicit RefMutexGuard(RefMutex &alock)
: lock(&alock)
{
lock->addRef();
lock->enter();
}
~RefMutexGuard()
{
try {
lock->leave();
}
catch (const Exception&)
{
MutexLockGuard::onDtorException();
}
2008-03-11 15:33:09 +01:00
lock->release();
}
private:
// Forbid copy constructor
RefMutexGuard(const RefMutexGuard& source);
RefMutex *lock;
};
} //namespace Firebird
2004-03-07 08:58:55 +01:00
#endif // CLASSES_LOCKS_H