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

114 lines
2.1 KiB
C
Raw Normal View History

#ifndef LOCKS_H
#define LOCKS_H
#include "firebird.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
#include <pthread.h>
#else
#include <sys/mutex.h>
#include <thread.h>
#endif
#endif
namespace Firebird {
#ifdef WIN_NT
/* Process-local spinlock. Used to manage memory heaps in threaded environment. */
// Windows version of the class
class Spinlock {
private:
CRITICAL_SECTION spinlock;
public:
Spinlock() {
if (!InitializeCriticalSectionAndSpinCount(&spinlock, 4000))
system_call_failed::raise();
}
~Spinlock() {
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
#ifndef SOLARIS
class Spinlock {
private:
pthread_spinlock_t spinlock;
public:
Spinlock() {
if (pthread_spin_init(&spinlock, false))
system_call_failed::raise();
}
~Spinlock() {
if (pthread_spin_destroy(&spinlock))
system_call_failed::raise();
}
void enter() {
if (pthread_spin_lock(&spinlock))
system_call_failed::raise();
}
void leave() {
if (pthread_spin_unlock(&spinlock))
system_call_failed::raise();
}
};
#else
// Who knows why Solaris 2.6 have not THIS funny spins?
//The next code is not comlpeted but let me compile //Konstantin
class Spinlock {
private:
mutex_t spinlock;
public:
Spinlock() {
if (mutex_init(&spinlock, MUTEX_SPIN, NULL))
system_call_failed::raise();
}
~Spinlock() {
if (mutex_destroy(&spinlock))
system_call_failed::raise();
}
void enter() {
if (mutex_lock(&spinlock))
system_call_failed::raise();
}
void leave() {
if (mutex_unlock(&spinlock))
system_call_failed::raise();
}
};
#endif
#endif
// Spinlock in shared memory. Not implemented yet !
class SharedSpinlock {
public:
SharedSpinlock() {
}
~SharedSpinlock() {
}
void enter() {
}
void leave() {
}
};
}
#endif