2003-09-16 22:45:31 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: semaphore.h
|
|
|
|
* DESCRIPTION: Semaphore lock
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Created by: Nickolay Samofatov <skidder@bssys.com>
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
*
|
2004-03-18 06:56:06 +01:00
|
|
|
* $Id: semaphore.h,v 1.9 2004-03-18 05:54:22 robocop Exp $
|
2003-09-16 22:45:31 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-03-18 06:56:06 +01:00
|
|
|
#ifndef CLASSES_SEMAPHORE_H
|
|
|
|
#define CLASSES_SEMAPHORE_H
|
2003-09-16 22:45:31 +02:00
|
|
|
|
|
|
|
#ifdef WIN_NT
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
class Semaphore {
|
|
|
|
private:
|
|
|
|
HANDLE hSemaphore;
|
|
|
|
public:
|
|
|
|
Semaphore() {
|
|
|
|
hSemaphore = CreateSemaphore(NULL, 0 /*initial count*/,
|
|
|
|
INT_MAX, NULL);
|
2004-03-11 06:30:07 +01:00
|
|
|
if (hSemaphore == NULL)
|
|
|
|
system_call_failed::raise("CreateSemaphore");
|
2003-09-16 22:45:31 +02:00
|
|
|
}
|
2004-03-11 06:30:07 +01:00
|
|
|
~Semaphore() {
|
|
|
|
if (hSemaphore && !CloseHandle(hSemaphore))
|
|
|
|
system_call_failed::raise("CloseHandle");
|
|
|
|
}
|
2003-09-16 22:45:31 +02:00
|
|
|
|
|
|
|
bool tryEnter(int seconds = 0) {
|
|
|
|
DWORD result = WaitForSingleObject(
|
2003-11-01 11:26:43 +01:00
|
|
|
hSemaphore, seconds >= 0 ? seconds * 1000 : INFINITE);
|
2003-09-16 22:45:31 +02:00
|
|
|
if (result == WAIT_FAILED)
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("WaitForSingleObject");
|
2003-09-16 22:45:31 +02:00
|
|
|
return result != WAIT_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void release(SLONG count = 1) {
|
|
|
|
if (!ReleaseSemaphore(hSemaphore, count, NULL))
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("ReleaseSemaphore");
|
2003-09-16 22:45:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#ifdef MULTI_THREAD
|
|
|
|
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
class Semaphore {
|
|
|
|
private:
|
|
|
|
sem_t sem;
|
2003-10-30 11:59:32 +01:00
|
|
|
bool init;
|
2003-09-16 22:45:31 +02:00
|
|
|
public:
|
2003-11-01 11:26:43 +01:00
|
|
|
Semaphore() : init(false) {
|
|
|
|
if (sem_init(&sem, 0, 0) == -1) {
|
2004-03-01 04:35:23 +01:00
|
|
|
//gds__log("Error on semaphore.h: constructor");
|
|
|
|
system_call_failed::raise("sem_init");
|
2003-10-30 11:59:32 +01:00
|
|
|
}
|
|
|
|
init = true;
|
2003-09-16 22:45:31 +02:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
|
2003-09-16 22:45:31 +02:00
|
|
|
~Semaphore() {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(init == true);
|
2003-10-30 11:59:32 +01:00
|
|
|
if (sem_destroy(&sem) == -1) {
|
2004-03-01 04:35:23 +01:00
|
|
|
//gds__log("Error on semaphore.h: destructor");
|
|
|
|
system_call_failed::raise("sem_destroy");
|
2003-10-30 11:59:32 +01:00
|
|
|
}
|
|
|
|
init = false;
|
|
|
|
|
2003-09-16 22:45:31 +02:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
|
2003-09-16 22:45:31 +02:00
|
|
|
bool tryEnter(int seconds = 0) {
|
2004-03-01 04:35:23 +01:00
|
|
|
// Return true in case of success
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(init == true);
|
2003-09-16 22:45:31 +02:00
|
|
|
if (seconds == 0) {
|
2004-03-01 04:35:23 +01:00
|
|
|
// Instant try
|
2003-10-30 11:59:32 +01:00
|
|
|
if (sem_trywait(&sem) != -1)
|
|
|
|
return true;
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return false;
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("sem_trywait");
|
2004-01-28 08:50:41 +01:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
if (seconds < 0) {
|
|
|
|
// Unlimited wait, like enter()
|
2003-10-30 11:59:32 +01:00
|
|
|
if (sem_wait(&sem) != -1)
|
|
|
|
return true;
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("sem_wait");
|
|
|
|
}
|
|
|
|
// Wait with timeout
|
|
|
|
struct timespec timeout;
|
|
|
|
timeout.tv_sec = time(NULL) + seconds;
|
|
|
|
timeout.tv_nsec = 0;
|
|
|
|
if (sem_timedwait(&sem, &timeout) == 0)
|
|
|
|
return true;
|
|
|
|
if (errno == ETIMEDOUT) {
|
|
|
|
return false;
|
2003-09-16 22:45:31 +02:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed::raise("sem_timedwait");
|
2003-09-16 22:45:31 +02:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
|
2003-09-16 22:45:31 +02:00
|
|
|
void enter() {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(init == true);
|
2003-11-01 11:26:43 +01:00
|
|
|
if (sem_wait(&sem) == -1) {
|
2004-03-01 04:35:23 +01:00
|
|
|
//gds__log("Error on semaphore.h: enter");
|
|
|
|
system_call_failed::raise("sem_wait");
|
2003-10-30 11:59:32 +01:00
|
|
|
}
|
2003-09-16 22:45:31 +02:00
|
|
|
}
|
2004-03-01 04:35:23 +01:00
|
|
|
|
2003-09-16 22:45:31 +02:00
|
|
|
void release(SLONG count = 1) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(init == true);
|
2004-03-01 04:35:23 +01:00
|
|
|
for (int i = 0; i < count; i++)
|
2003-11-01 11:26:43 +01:00
|
|
|
if (sem_post(&sem) == -1) {
|
2004-03-01 04:35:23 +01:00
|
|
|
//gds__log("Error on semaphore.h: release");
|
|
|
|
system_call_failed::raise("sem_post");
|
2003-10-30 11:59:32 +01:00
|
|
|
}
|
2003-09-16 22:45:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*MULTI_THREAD*/
|
|
|
|
|
|
|
|
#endif /*!WIN_NT*/
|
|
|
|
|
2004-03-18 06:56:06 +01:00
|
|
|
#endif // CLASSES_SEMAPHORE_H
|
2003-11-01 11:26:43 +01:00
|
|
|
|