2003-01-07 17:35:10 +01:00
|
|
|
#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
|
2003-01-19 19:32:23 +01:00
|
|
|
#ifndef SOLARIS
|
2003-01-07 17:35:10 +01:00
|
|
|
#include <pthread.h>
|
2003-01-19 19:32:23 +01:00
|
|
|
#else
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <thread.h>
|
|
|
|
#endif
|
2003-01-07 17:35:10 +01:00
|
|
|
#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
|
2003-01-19 19:32:23 +01:00
|
|
|
#ifndef SOLARIS
|
2003-01-07 17:35:10 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
2003-01-19 19:32:23 +01:00
|
|
|
#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();
|
|
|
|
}
|
|
|
|
};
|
2003-01-07 17:35:10 +01:00
|
|
|
|
2003-01-19 19:32:23 +01:00
|
|
|
#endif
|
2003-01-07 17:35:10 +01:00
|
|
|
#endif
|
|
|
|
|
2003-01-10 22:37:18 +01:00
|
|
|
// Spinlock in shared memory. Not implemented yet !
|
|
|
|
class SharedSpinlock {
|
|
|
|
public:
|
|
|
|
SharedSpinlock() {
|
|
|
|
}
|
|
|
|
~SharedSpinlock() {
|
|
|
|
}
|
|
|
|
void enter() {
|
|
|
|
}
|
|
|
|
void leave() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-01-07 17:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|