2003-08-06 18:30:49 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: locks.h
|
|
|
|
* DESCRIPTION: Single-state locks
|
|
|
|
*
|
2004-06-30 03:26:40 +02:00
|
|
|
* 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): ______________________________________.
|
|
|
|
*
|
|
|
|
*
|
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"
|
2008-03-25 09:44:11 +01:00
|
|
|
#include "../jrd/gdsassert.h"
|
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
|
2004-12-24 10:35:48 +01:00
|
|
|
#ifndef SOLARIS_MT
|
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
|
2008-03-12 14:36:33 +01:00
|
|
|
#include <errno.h>
|
2003-01-07 17:35:10 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2008-02-02 17:57:15 +01:00
|
|
|
class MemoryPool; // Needed for ctors that must always ignore it
|
2008-02-05 09:21:18 +01:00
|
|
|
class Exception; // Needed for catch
|
2008-01-23 16:52:40 +01:00
|
|
|
|
2003-01-07 17:35:10 +01:00
|
|
|
#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
|
|
|
|
2008-03-12 09:17:15 +01:00
|
|
|
class TryEnterCS
|
2008-01-23 20:03:16 +01:00
|
|
|
{
|
2008-03-12 09:17:15 +01:00
|
|
|
public:
|
|
|
|
TryEnterCS();
|
|
|
|
|
|
|
|
static bool tryEnter(LPCRITICAL_SECTION lpCS)
|
|
|
|
{
|
|
|
|
return ((*m_funct) (lpCS) == TRUE);
|
|
|
|
}
|
|
|
|
|
2008-03-11 15:08:23 +01:00
|
|
|
private:
|
2008-12-05 01:56:15 +01:00
|
|
|
typedef WINBASEAPI BOOL WINAPI tTryEnterCriticalSection
|
2008-03-12 09:17:15 +01:00
|
|
|
(LPCRITICAL_SECTION lpCriticalSection);
|
2008-03-11 15:08:23 +01:00
|
|
|
|
2008-03-12 09:17:15 +01:00
|
|
|
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:
|
2003-01-07 17:35:10 +01:00
|
|
|
CRITICAL_SECTION spinlock;
|
2008-02-11 10:52:32 +01:00
|
|
|
|
2003-01-07 17:35:10 +01:00
|
|
|
public:
|
2008-04-18 12:03:04 +02:00
|
|
|
Mutex()
|
|
|
|
{
|
2006-05-03 07:44:26 +02:00
|
|
|
InitializeCriticalSection(&spinlock);
|
|
|
|
}
|
2008-12-13 10:26:00 +01:00
|
|
|
explicit Mutex(MemoryPool&)
|
|
|
|
{
|
2008-01-23 16:52:40 +01:00
|
|
|
InitializeCriticalSection(&spinlock);
|
|
|
|
}
|
2008-03-13 13:54:31 +01:00
|
|
|
|
2008-04-18 12:03:04 +02:00
|
|
|
~Mutex()
|
|
|
|
{
|
2008-03-13 13:54:31 +01:00
|
|
|
#ifdef DEV_BUILD
|
|
|
|
if (spinlock.OwningThread != 0)
|
|
|
|
DebugBreak();
|
|
|
|
#endif
|
2003-01-07 17:35:10 +01:00
|
|
|
DeleteCriticalSection(&spinlock);
|
|
|
|
}
|
2008-03-13 13:54:31 +01:00
|
|
|
|
2008-04-18 12:03:04 +02:00
|
|
|
void enter()
|
|
|
|
{
|
2003-01-07 17:35:10 +01:00
|
|
|
EnterCriticalSection(&spinlock);
|
|
|
|
}
|
2008-03-13 13:54:31 +01:00
|
|
|
|
2008-04-18 12:03:04 +02:00
|
|
|
bool tryEnter()
|
|
|
|
{
|
2008-03-12 09:17:15 +01:00
|
|
|
return TryEnterCS::tryEnter(&spinlock);
|
|
|
|
}
|
2008-03-13 13:54:31 +01:00
|
|
|
|
2008-04-18 12:03:04 +02:00
|
|
|
void leave()
|
|
|
|
{
|
2008-03-13 13:54:31 +01:00
|
|
|
#ifdef DEV_BUILD
|
2008-09-15 01:17:58 +02:00
|
|
|
if ((U_IPTR) spinlock.OwningThread != GetCurrentThreadId())
|
2008-03-13 13:54:31 +01:00
|
|
|
DebugBreak();
|
|
|
|
#endif
|
2003-01-07 17:35:10 +01:00
|
|
|
LeaveCriticalSection(&spinlock);
|
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-02-11 10:52:32 +01:00
|
|
|
public:
|
|
|
|
static void initMutexes() { }
|
2003-01-07 17:35:10 +01:00
|
|
|
};
|
|
|
|
|
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:
|
2008-01-23 16:52:40 +01:00
|
|
|
Spinlock()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
2008-01-23 20:27:36 +01:00
|
|
|
|
2008-01-23 16:52:40 +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
|
2003-01-07 17:35:10 +01:00
|
|
|
|
2004-12-24 10:35:48 +01:00
|
|
|
#ifdef SOLARIS_MT
|
2005-02-17 13:42:49 +01:00
|
|
|
|
2008-01-23 20:03:16 +01:00
|
|
|
class Mutex
|
|
|
|
{
|
2003-01-19 19:32:23 +01:00
|
|
|
private:
|
2005-12-18 17:10:48 +01:00
|
|
|
mutex_t mlock;
|
2008-02-11 10:52:32 +01:00
|
|
|
|
2008-12-28 16:26:05 +01:00
|
|
|
void init()
|
2008-04-18 12:03:04 +02:00
|
|
|
{
|
2008-12-28 16:26:05 +01:00
|
|
|
int rc = mutex_init(&mlock, USYNC_THREAD | LOCK_RECURSIVE, NULL);
|
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("mutex_init", rc);
|
2008-01-23 16:52:40 +01:00
|
|
|
}
|
2008-12-28 16:26:05 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
Mutex() { init(); }
|
|
|
|
explicit Mutex(MemoryPool&) { init(); }
|
2008-04-18 12:03:04 +02:00
|
|
|
~Mutex()
|
|
|
|
{
|
2008-12-28 16:26:05 +01:00
|
|
|
int rc = mutex_destroy(&mlock);
|
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("mutex_destroy", rc);
|
2003-01-19 19:32:23 +01:00
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
void enter()
|
|
|
|
{
|
2008-12-28 16:26:05 +01:00
|
|
|
int rc = mutex_lock(&mlock);
|
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("mutex_lock", rc);
|
|
|
|
}
|
|
|
|
bool tryEnter()
|
|
|
|
{
|
|
|
|
int rc = mutex_trylock(&mlock);
|
|
|
|
if (rc == EBUSY)
|
|
|
|
return false;
|
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("mutex_trylock", rc);
|
|
|
|
return true;
|
2003-01-19 19:32:23 +01:00
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
void leave()
|
|
|
|
{
|
2008-12-28 16:26:05 +01:00
|
|
|
int rc = mutex_unlock(&mlock);
|
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("mutex_unlock", rc);
|
2003-01-19 19:32:23 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-02-11 10:52:32 +01:00
|
|
|
public:
|
|
|
|
static void initMutexes() { }
|
2003-01-19 19:32:23 +01:00
|
|
|
};
|
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;
|
2008-02-05 09:21:18 +01:00
|
|
|
static pthread_mutexattr_t attr;
|
2008-02-06 01:43:54 +01:00
|
|
|
|
2008-02-11 10:52:32 +01:00
|
|
|
private:
|
2008-04-18 12:03:04 +02:00
|
|
|
void init()
|
|
|
|
{
|
2008-02-05 09:21:18 +01:00
|
|
|
int rc = pthread_mutex_init(&mlock, &attr);
|
2008-03-12 14:36:33 +01:00
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("pthread_mutex_init", rc);
|
2008-01-23 16:52:40 +01:00
|
|
|
}
|
2008-02-11 10:52:32 +01:00
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
public:
|
|
|
|
Mutex() { init(); }
|
|
|
|
explicit Mutex(MemoryPool&) { init(); }
|
2008-04-18 12:03:04 +02:00
|
|
|
~Mutex()
|
|
|
|
{
|
2008-02-05 09:21:18 +01:00
|
|
|
int rc = pthread_mutex_destroy(&mlock);
|
2008-03-12 14:36:33 +01:00
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("pthread_mutex_destroy", rc);
|
2003-11-12 00:58:49 +01:00
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
void enter()
|
|
|
|
{
|
2008-02-05 09:21:18 +01:00
|
|
|
int rc = pthread_mutex_lock(&mlock);
|
2008-03-12 14:36:33 +01:00
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("pthread_mutex_lock", rc);
|
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
bool tryEnter()
|
|
|
|
{
|
2008-03-12 14:36:33 +01:00
|
|
|
int rc = pthread_mutex_trylock(&mlock);
|
|
|
|
if (rc == EBUSY)
|
|
|
|
return false;
|
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("pthread_mutex_trylock", rc);
|
|
|
|
return true;
|
2003-11-12 00:58:49 +01:00
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
void leave()
|
|
|
|
{
|
2008-02-05 09:21:18 +01:00
|
|
|
int rc = pthread_mutex_unlock(&mlock);
|
2008-03-12 14:36:33 +01:00
|
|
|
if (rc)
|
|
|
|
system_call_failed::raise("pthread_mutex_unlock", rc);
|
2003-11-12 00:58:49 +01:00
|
|
|
}
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2008-02-11 10:52:32 +01:00
|
|
|
public:
|
|
|
|
static void initMutexes();
|
2003-11-12 00:58:49 +01:00
|
|
|
};
|
2003-01-07 17:35:10 +01:00
|
|
|
|
2008-12-11 11:59:26 +01:00
|
|
|
#if !(defined(DARWIN) || defined(HPUX))
|
2008-01-23 20:03:16 +01:00
|
|
|
class Spinlock
|
|
|
|
{
|
2006-05-03 07:44:26 +02:00
|
|
|
private:
|
|
|
|
pthread_spinlock_t spinlock;
|
|
|
|
public:
|
2008-04-18 12:03:04 +02:00
|
|
|
Spinlock()
|
|
|
|
{
|
2006-05-03 07:44:26 +02:00
|
|
|
if (pthread_spin_init(&spinlock, false))
|
|
|
|
system_call_failed::raise("pthread_spin_init");
|
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
explicit Spinlock(MemoryPool&)
|
|
|
|
{
|
2008-01-23 16:52:40 +01:00
|
|
|
if (pthread_spin_init(&spinlock, false))
|
|
|
|
system_call_failed::raise("pthread_spin_init");
|
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
~Spinlock()
|
|
|
|
{
|
2006-05-03 07:44:26 +02:00
|
|
|
if (pthread_spin_destroy(&spinlock))
|
|
|
|
system_call_failed::raise("pthread_spin_destroy");
|
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
void enter()
|
|
|
|
{
|
2006-05-03 07:44:26 +02:00
|
|
|
if (pthread_spin_lock(&spinlock))
|
|
|
|
system_call_failed::raise("pthread_spin_lock");
|
|
|
|
}
|
2008-04-18 12:03:04 +02:00
|
|
|
void leave()
|
|
|
|
{
|
2006-05-03 07:44:26 +02:00
|
|
|
if (pthread_spin_unlock(&spinlock))
|
|
|
|
system_call_failed::raise("pthread_spin_unlock");
|
|
|
|
}
|
|
|
|
};
|
2008-12-11 11:59:26 +01:00
|
|
|
#else // have spinlocks
|
|
|
|
typedef Mutex Spinlock;
|
|
|
|
#endif // have spinlocks
|
2006-05-03 07:44:26 +02:00
|
|
|
|
2005-12-18 17:10:48 +01:00
|
|
|
#endif //SOLARIS_MT
|
|
|
|
|
|
|
|
#endif //WIN_NT
|
|
|
|
|
2008-01-23 16:52:40 +01:00
|
|
|
|
2008-01-29 16:03:34 +01:00
|
|
|
// RAII holder
|
2008-02-05 09:21:18 +01:00
|
|
|
class MutexLockGuard
|
2008-01-29 16:03:34 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-12-05 01:56:15 +01:00
|
|
|
explicit MutexLockGuard(Mutex &alock)
|
2008-01-29 19:07:37 +01:00
|
|
|
: lock(&alock)
|
|
|
|
{
|
|
|
|
lock->enter();
|
|
|
|
}
|
|
|
|
|
2008-12-05 01:56:15 +01:00
|
|
|
~MutexLockGuard()
|
|
|
|
{
|
2008-02-05 09:21:18 +01:00
|
|
|
try {
|
|
|
|
lock->leave();
|
|
|
|
}
|
2008-02-06 01:43:54 +01:00
|
|
|
catch (const Exception&)
|
2008-02-05 09:21:18 +01:00
|
|
|
{
|
2008-03-25 09:44:11 +01:00
|
|
|
DtorException::devHalt();
|
2008-02-05 09:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-29 16:03:34 +01:00
|
|
|
private:
|
2008-10-19 13:41:43 +02:00
|
|
|
// Forbid copying
|
|
|
|
MutexLockGuard(const MutexLockGuard&);
|
|
|
|
MutexLockGuard& operator=(const MutexLockGuard&);
|
2008-02-06 01:43:54 +01:00
|
|
|
|
|
|
|
Mutex* lock;
|
2008-01-29 16:03:34 +01:00
|
|
|
};
|
|
|
|
|
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
|