Fix bugs in src/core/semaphore.cpp
[tairon.git] / src / core / semaphore.cpp
bloba23ace4781eb1afeafb60abd095287383cd54d4a
1 /***************************************************************************
2 * *
3 * Copyright (C) <year> <author> *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License as published by the Free Software Foundation and appearing *
8 * in the file LICENSE.LGPL included in the packaging of this file. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Library General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #include <errno.h>
18 #include <time.h>
20 #include "semaphore.h"
22 namespace Tairon
25 namespace Core
28 /* {{{ Semaphore::Semaphore(unsigned int) */
29 Semaphore::Semaphore(unsigned int value)
31 sem_init(&s, 0, value);
33 /* }}} */
35 /* {{{ Semaphore::~Semaphore */
36 Semaphore::~Semaphore()
38 sem_destroy(&s);
40 /* }}} */
42 /* {{{ Semaphore::timedWait(time_t, long, bool) */
43 bool Semaphore::timedWait(time_t seconds, long nanoseconds, bool absolute)
45 struct timespec t;
46 if (absolute) {
47 t.tv_sec = seconds;
48 t.tv_nsec = nanoseconds;
49 } else {
50 struct timespec current;
51 clock_gettime(CLOCK_REALTIME, &current);
52 t.tv_sec = current.tv_sec + seconds + (current.tv_nsec + nanoseconds) / 1000000000;
53 t.tv_nsec = (current.tv_nsec + nanoseconds) % 1000000000;
56 if (sem_timedwait(&s, &t) != 0)
57 return false;
58 return true;
60 /* }}} */
62 }; // namespace Core
64 }; // namespace Tairon
66 // vim: ai sw=4 ts=4 noet fdm=marker