]> git.lizzy.rs Git - minetest.git/blob - src/jthread/pthread/jsemaphore.cpp
Fix msvc2012 build
[minetest.git] / src / jthread / pthread / jsemaphore.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 sapier, < sapier AT gmx DOT net >
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program 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
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #include <assert.h>
20 #include <errno.h>
21 #include <sys/time.h>
22 #include "jthread/jsemaphore.h"
23
24 #define UNUSED(expr) do { (void)(expr); } while (0)
25
26 JSemaphore::JSemaphore() {
27         int sem_init_retval = sem_init(&m_semaphore,0,0);
28         assert(sem_init_retval == 0);
29         UNUSED(sem_init_retval);
30 }
31
32 JSemaphore::~JSemaphore() {
33         int sem_destroy_retval = sem_destroy(&m_semaphore);
34         assert(sem_destroy_retval == 0);
35         UNUSED(sem_destroy_retval);
36 }
37
38 JSemaphore::JSemaphore(int initval) {
39         int sem_init_retval = sem_init(&m_semaphore,0,initval);
40         assert(sem_init_retval == 0);
41         UNUSED(sem_init_retval);
42 }
43
44 void JSemaphore::Post() {
45         int sem_post_retval = sem_post(&m_semaphore);
46         assert(sem_post_retval == 0);
47         UNUSED(sem_post_retval);
48 }
49
50 void JSemaphore::Wait() {
51         int sem_wait_retval = sem_wait(&m_semaphore);
52         assert(sem_wait_retval == 0);
53         UNUSED(sem_wait_retval);
54 }
55
56 bool JSemaphore::Wait(unsigned int time_ms) {
57         struct timespec waittime;
58         struct timeval now;
59
60         if (gettimeofday(&now, NULL) == -1) {
61                 assert("Unable to get time by clock_gettime!" == 0);
62                 return false;
63         }
64
65         waittime.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000);
66         waittime.tv_sec  = (time_ms / 1000) + (waittime.tv_nsec / (1000*1000*1000)) + now.tv_sec;
67         waittime.tv_nsec %= 1000*1000*1000;
68
69         errno = 0;
70         int sem_wait_retval = sem_timedwait(&m_semaphore,&waittime);
71
72         if (sem_wait_retval == 0)
73         {
74                 return true;
75         }
76         else {
77                 assert((errno == ETIMEDOUT) || (errno == EINTR));
78                 return false;
79         }
80         return sem_wait_retval == 0 ? true : false;
81 }
82
83 int JSemaphore::GetValue() {
84
85         int retval = 0;
86         sem_getvalue(&m_semaphore,&retval);
87
88         return retval;
89 }
90