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

187 lines
4.2 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"
#ifdef MULTI_THREAD
#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
#endif /* MULTI_THREAD */
namespace Firebird {
#ifdef MULTI_THREAD
#ifdef WIN_NT
/* Process-local spinlock. Used to manage memory heaps in threaded environment. */
// Windows version of the class
2003-01-20 19:38:34 +01:00
typedef WINBASEAPI DWORD WINAPI tSetCriticalSectionSpinCount (
LPCRITICAL_SECTION lpCriticalSection,
DWORD dwSpinCount
);
class Mutex {
private:
CRITICAL_SECTION spinlock;
2003-01-20 19:38:34 +01:00
static tSetCriticalSectionSpinCount* SetCriticalSectionSpinCount;
public:
Mutex();
~Mutex() {
DeleteCriticalSection(&spinlock);
}
void enter() {
EnterCriticalSection(&spinlock);
}
void leave() {
LeaveCriticalSection(&spinlock);
}
};
#else
/* Process-local spinlock. Used to manage memory heaps in threaded environment. */
// Pthreads version of the class
#if !defined(SOLARIS_MT) && !defined(DARWIN) && !defined(FREEBSD) && !defined(NETBSD)
class Mutex {
private:
pthread_spinlock_t spinlock;
public:
Mutex() {
if (pthread_spin_init(&spinlock, false))
system_call_failed::raise("pthread_spin_init");
}
~Mutex() {
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");
}
};
#else
#ifdef SOLARIS_MT
// Who knows why Solaris 2.6 have not THIS funny spins?
//The next code is not comlpeted but let me compile //Konstantin
class Mutex {
private:
mutex_t spinlock;
public:
Mutex() {
if (mutex_init(&spinlock, USYNC_PROCESS, NULL))
system_call_failed::raise("mutex_init");
}
~Mutex() {
if (mutex_destroy(&spinlock))
system_call_failed::raise("mutex_destroy");
}
void enter() {
if (mutex_lock(&spinlock))
system_call_failed::raise("mutex_lock");
}
void leave() {
if (mutex_unlock(&spinlock))
system_call_failed::raise("mutex_unlock");
}
};
2004-05-21 01:05:02 +02:00
#else // DARWIN and FREEBSD and NETBSD
class Mutex {
2003-11-12 00:58:49 +01:00
private:
pthread_mutex_t mlock;
public:
Mutex() {
2003-11-12 00:58:49 +01:00
if (pthread_mutex_init(&mlock, 0))
system_call_failed::raise("pthread_mutex_init");
2003-11-12 00:58:49 +01:00
}
~Mutex() {
2003-11-12 00:58:49 +01:00
if (pthread_mutex_destroy(&mlock))
system_call_failed::raise("pthread_mutex_destroy");
2003-11-12 00:58:49 +01:00
}
void enter() {
if (pthread_mutex_lock(&mlock))
system_call_failed::raise("pthread_mutex_lock");
2003-11-12 00:58:49 +01:00
}
void leave() {
if (pthread_mutex_unlock(&mlock))
system_call_failed::raise("pthread_mutex_unlock");
2003-11-12 00:58:49 +01:00
}
};
#endif
#endif
#endif
#else
// Non-MT version
class Mutex {
public:
Mutex() {
}
~Mutex() {
}
void enter() {
}
void leave() {
}
};
#endif /* MULTI_THREAD */
2004-05-03 01:06:37 +02:00
// RAII holder of mutex lock
class MutexLockGuard {
public:
2004-06-08 08:51:13 +02:00
explicit MutexLockGuard(Mutex &alock)
: lock(&alock) { lock->enter(); }
~MutexLockGuard() { lock->leave(); }
2004-05-03 01:06:37 +02:00
private:
// Forbid copy constructor
MutexLockGuard(const MutexLockGuard& source);
Mutex *lock;
};
} //namespace Firebird
2004-03-07 08:58:55 +01:00
#endif // CLASSES_LOCKS_H