2003-01-20 19:38:34 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: locks.cpp
|
2004-03-01 05:57:43 +01:00
|
|
|
* DESCRIPTION: Win32 Mutex support compatible with
|
2003-01-20 19:38:34 +01:00
|
|
|
* old OS versions (like Windows 95)
|
|
|
|
*
|
2005-01-10 19:06:14 +01: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.
|
2003-01-20 19:38:34 +01:00
|
|
|
*
|
2005-01-10 19:06:14 +01:00
|
|
|
* 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.
|
2003-01-20 19:38:34 +01:00
|
|
|
*
|
2005-01-10 19:06:14 +01:00
|
|
|
* The Original Code was created by Alexander Peshkoff
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
2003-01-20 19:38:34 +01:00
|
|
|
*
|
2005-01-12 05:34:16 +01:00
|
|
|
* Copyright (c) 2003 Alexander Peshkoff <peshkoff@mail.ru>
|
2005-01-10 19:06:14 +01:00
|
|
|
* and all contributors signed below.
|
2003-01-20 19:38:34 +01:00
|
|
|
*
|
2005-01-10 19:06:14 +01:00
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
2003-01-20 19:38:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../../include/firebird.h"
|
|
|
|
#include "../../common/classes/locks.h"
|
2008-01-23 16:52:40 +01:00
|
|
|
#include "../../common/thd.h"
|
|
|
|
#include "../../jrd/common.h"
|
2003-01-20 19:38:34 +01:00
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
2003-01-20 19:38:34 +01:00
|
|
|
namespace Firebird {
|
2003-01-21 12:08:59 +01:00
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
#if defined(WIN_NT)
|
2003-01-20 19:38:34 +01:00
|
|
|
|
|
|
|
#define MISS_SPIN_COUNT ((tSetCriticalSectionSpinCount *)(-1))
|
|
|
|
#define INIT_SPIN_COUNT ((tSetCriticalSectionSpinCount *)(0))
|
|
|
|
|
|
|
|
tSetCriticalSectionSpinCount*
|
2006-05-03 07:44:26 +02:00
|
|
|
Spinlock::SetCriticalSectionSpinCount = INIT_SPIN_COUNT;
|
2003-01-20 19:38:34 +01:00
|
|
|
|
2008-01-23 20:27:36 +01:00
|
|
|
void Spinlock::init()
|
|
|
|
{
|
2003-01-20 19:38:34 +01:00
|
|
|
if (SetCriticalSectionSpinCount == MISS_SPIN_COUNT)
|
|
|
|
return;
|
2008-01-23 20:27:36 +01:00
|
|
|
|
2003-01-20 19:38:34 +01:00
|
|
|
if (SetCriticalSectionSpinCount == INIT_SPIN_COUNT) {
|
|
|
|
HMODULE kernel32 = LoadLibrary("kernel32.dll");
|
2008-01-23 20:27:36 +01:00
|
|
|
if (!kernel32) {
|
2003-01-20 19:38:34 +01:00
|
|
|
SetCriticalSectionSpinCount = MISS_SPIN_COUNT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetCriticalSectionSpinCount =
|
|
|
|
(tSetCriticalSectionSpinCount *) GetProcAddress(
|
|
|
|
kernel32, "SetCriticalSectionSpinCount");
|
2008-01-23 20:27:36 +01:00
|
|
|
if (!SetCriticalSectionSpinCount) {
|
2003-01-20 19:38:34 +01:00
|
|
|
SetCriticalSectionSpinCount = MISS_SPIN_COUNT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-01-23 20:27:36 +01:00
|
|
|
|
2003-01-20 19:38:34 +01:00
|
|
|
SetCriticalSectionSpinCount(&spinlock, 4000);
|
|
|
|
}
|
2003-01-21 12:08:59 +01:00
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
#elif defined(SOLARIS_MT)
|
2003-01-21 12:08:59 +01:00
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
#error Fix me!
|
2008-01-23 16:52:40 +01:00
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
#else //posix mutex
|
2008-01-23 16:52:40 +01:00
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
pthread_mutexattr_t Mutex::attr;
|
2008-02-06 01:43:54 +01:00
|
|
|
|
2008-02-11 10:52:32 +01:00
|
|
|
void Mutex::initMutexes()
|
2008-01-23 16:52:40 +01:00
|
|
|
{
|
2008-02-11 10:52:32 +01:00
|
|
|
// Throw exceptions on errors, but they will not be processed in init
|
|
|
|
// (first constructor). Better logging facilities are required here.
|
2008-02-05 09:21:18 +01:00
|
|
|
int rc = pthread_mutexattr_init(&attr);
|
|
|
|
if (rc < 0)
|
|
|
|
system_call_failed::raise("pthread_mutexattr_init", rc);
|
|
|
|
|
|
|
|
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
if (rc < 0)
|
|
|
|
system_call_failed::raise("pthread_mutexattr_settype", rc);
|
2008-01-23 16:52:40 +01:00
|
|
|
}
|
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
#endif
|
2008-01-23 16:52:40 +01:00
|
|
|
|
2008-02-05 09:21:18 +01:00
|
|
|
|
|
|
|
#ifdef DEV_BUILD
|
|
|
|
void MutexLockGuard::halt()
|
|
|
|
{
|
|
|
|
abort();
|
2008-01-23 16:52:40 +01:00
|
|
|
}
|
2008-02-05 09:21:18 +01:00
|
|
|
#endif
|
2008-01-23 16:52:40 +01:00
|
|
|
|
2008-01-23 20:27:36 +01:00
|
|
|
} // namespace Firebird
|