2003-08-06 18:30:49 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: locks.h
|
|
|
|
* DESCRIPTION: Single-state locks
|
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
* You may obtain a copy of the Licence at
|
|
|
|
* http://www.gnu.org/licences/lgpl.html
|
|
|
|
*
|
|
|
|
* As a special exception this file can also be included in modules
|
|
|
|
* with other source code as long as that source code has been
|
|
|
|
* released under an Open Source Initiative certificed licence.
|
|
|
|
* More information about OSI certification can be found at:
|
|
|
|
* http://www.opensource.org
|
|
|
|
*
|
|
|
|
* This module is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public Licence for more details.
|
|
|
|
*
|
|
|
|
* This module was created by members of the firebird development
|
|
|
|
* team. All individual contributions remain the Copyright (C) of
|
|
|
|
* those individuals and all rights are reserved. Contributors to
|
|
|
|
* this file are either listed below or can be obtained from a CVS
|
|
|
|
* history command.
|
2003-08-06 18:30:49 +02:00
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* Created by: Nickolay Samofatov <skidder@bssys.com>
|
2003-08-06 18:30:49 +02:00
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
2003-08-06 18:30:49 +02:00
|
|
|
*
|
2004-06-11 16:23:30 +02:00
|
|
|
* $Id: locks.h,v 1.16 2004-06-11 14:23:24 kkuznetsov Exp $
|
2003-08-06 18:30:49 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-03-07 08:58:55 +01:00
|
|
|
#ifndef CLASSES_LOCKS_H
|
|
|
|
#define CLASSES_LOCKS_H
|
2003-01-07 17:35:10 +01:00
|
|
|
|
|
|
|
#include "firebird.h"
|
|
|
|
|
2003-08-10 17:43:23 +02:00
|
|
|
#ifdef MULTI_THREAD
|
2003-01-07 17:35:10 +01:00
|
|
|
#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 <thread.h>
|
2004-06-11 16:23:30 +02:00
|
|
|
#include <synch.h>
|
2003-01-19 19:32:23 +01:00
|
|
|
#endif
|
2003-01-07 17:35:10 +01:00
|
|
|
#endif
|
2003-08-10 17:43:23 +02:00
|
|
|
#endif /* MULTI_THREAD */
|
2003-01-07 17:35:10 +01:00
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2003-08-10 17:43:23 +02:00
|
|
|
#ifdef MULTI_THREAD
|
2003-01-07 17:35:10 +01:00
|
|
|
#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
|
|
|
|
);
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
class Mutex {
|
2003-01-07 17:35:10 +01:00
|
|
|
private:
|
|
|
|
CRITICAL_SECTION spinlock;
|
2003-01-20 19:38:34 +01:00
|
|
|
static tSetCriticalSectionSpinCount* SetCriticalSectionSpinCount;
|
2003-01-07 17:35:10 +01:00
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
Mutex();
|
|
|
|
~Mutex() {
|
2003-01-07 17:35:10 +01:00
|
|
|
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
|
2004-05-21 01:05:02 +02:00
|
|
|
#if !defined(SOLARIS) && !defined(DARWIN) && !defined(FREEBSD) && !defined(NETBSD)
|
2004-03-01 04:35:23 +01:00
|
|
|
class Mutex {
|
2003-01-07 17:35:10 +01:00
|
|
|
private:
|
|
|
|
pthread_spinlock_t spinlock;
|
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
Mutex() {
|
2003-01-07 17:35:10 +01:00
|
|
|
if (pthread_spin_init(&spinlock, false))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("pthread_spin_init");
|
2003-01-07 17:35:10 +01:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
~Mutex() {
|
2003-01-07 17:35:10 +01:00
|
|
|
if (pthread_spin_destroy(&spinlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("pthread_spin_destroy");
|
2003-01-07 17:35:10 +01:00
|
|
|
}
|
|
|
|
void enter() {
|
|
|
|
if (pthread_spin_lock(&spinlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("pthread_spin_lock");
|
2003-01-07 17:35:10 +01:00
|
|
|
}
|
|
|
|
void leave() {
|
|
|
|
if (pthread_spin_unlock(&spinlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("pthread_spin_unlock");
|
2003-01-07 17:35:10 +01:00
|
|
|
}
|
|
|
|
};
|
2003-01-19 19:32:23 +01:00
|
|
|
#else
|
2003-11-12 00:58:49 +01:00
|
|
|
#ifdef SOLARIS
|
2003-01-19 19:32:23 +01:00
|
|
|
// Who knows why Solaris 2.6 have not THIS funny spins?
|
|
|
|
//The next code is not comlpeted but let me compile //Konstantin
|
2004-03-01 04:35:23 +01:00
|
|
|
class Mutex {
|
2003-01-19 19:32:23 +01:00
|
|
|
private:
|
|
|
|
mutex_t spinlock;
|
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
Mutex() {
|
2004-06-11 16:23:30 +02:00
|
|
|
if (mutex_init(&spinlock, USYNC_PROCESS, NULL))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("mutex_init");
|
2003-01-19 19:32:23 +01:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
~Mutex() {
|
2003-01-19 19:32:23 +01:00
|
|
|
if (mutex_destroy(&spinlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("mutex_destroy");
|
2003-01-19 19:32:23 +01:00
|
|
|
}
|
|
|
|
void enter() {
|
|
|
|
if (mutex_lock(&spinlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("mutex_lock");
|
2003-01-19 19:32:23 +01:00
|
|
|
}
|
|
|
|
void leave() {
|
|
|
|
if (mutex_unlock(&spinlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("mutex_unlock");
|
2003-01-19 19:32:23 +01:00
|
|
|
}
|
|
|
|
};
|
2004-05-21 01:05:02 +02:00
|
|
|
#else // DARWIN and FREEBSD and NETBSD
|
2004-03-01 04:35:23 +01:00
|
|
|
class Mutex {
|
2003-11-12 00:58:49 +01:00
|
|
|
private:
|
|
|
|
pthread_mutex_t mlock;
|
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
Mutex() {
|
2003-11-12 00:58:49 +01:00
|
|
|
if (pthread_mutex_init(&mlock, 0))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("pthread_mutex_init");
|
2003-11-12 00:58:49 +01:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
~Mutex() {
|
2003-11-12 00:58:49 +01:00
|
|
|
if (pthread_mutex_destroy(&mlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("pthread_mutex_destroy");
|
2003-11-12 00:58:49 +01:00
|
|
|
}
|
|
|
|
void enter() {
|
|
|
|
if (pthread_mutex_lock(&mlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("pthread_mutex_lock");
|
2003-11-12 00:58:49 +01:00
|
|
|
}
|
|
|
|
void leave() {
|
|
|
|
if (pthread_mutex_unlock(&mlock))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("pthread_mutex_unlock");
|
2003-11-12 00:58:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
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
|
2004-03-01 04:35:23 +01:00
|
|
|
#else
|
|
|
|
// Non-MT version
|
|
|
|
class Mutex {
|
2003-01-10 22:37:18 +01:00
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
Mutex() {
|
2003-01-10 22:37:18 +01:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
~Mutex() {
|
2003-01-10 22:37:18 +01:00
|
|
|
}
|
|
|
|
void enter() {
|
|
|
|
}
|
|
|
|
void leave() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
#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(); }
|
2004-05-03 01:06:37 +02:00
|
|
|
~MutexLockGuard() { lock->leave(); };
|
|
|
|
private:
|
|
|
|
// Forbid copy constructor
|
|
|
|
MutexLockGuard(const MutexLockGuard& source);
|
|
|
|
Mutex *lock;
|
|
|
|
};
|
|
|
|
|
2004-03-14 14:40:14 +01:00
|
|
|
} //namespace Firebird
|
2003-01-07 17:35:10 +01:00
|
|
|
|
2004-03-07 08:58:55 +01:00
|
|
|
#endif // CLASSES_LOCKS_H
|