]> git.lizzy.rs Git - minetest.git/blob - src/jthread/pthread/jsemaphore.cpp
OS X compatibility fixes
[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 #ifdef __MACH__
24 #include <unistd.h>
25 #endif
26
27 #define UNUSED(expr) do { (void)(expr); } while (0)
28
29 #ifdef __MACH__
30 #undef sem_t
31 #undef sem_init
32 #undef sem_wait
33 #undef sem_post
34 #undef sem_destroy
35 #define sem_t             semaphore_t
36 #define sem_init(s, p, c) semaphore_create(mach_task_self(), (s), 0, (c))
37 #define sem_wait(s)       semaphore_wait(*(s))
38 #define sem_post(s)       semaphore_signal(*(s))
39 #define sem_destroy(s)    semaphore_destroy(mach_task_self(), *(s))
40 pthread_mutex_t semcount_mutex;
41 #endif
42
43 JSemaphore::JSemaphore() {
44         int sem_init_retval = sem_init(&m_semaphore,0,0);
45         assert(sem_init_retval == 0);
46         UNUSED(sem_init_retval);
47 #ifdef __MACH__
48         semcount = 0;
49 #endif
50 }
51
52 JSemaphore::~JSemaphore() {
53         int sem_destroy_retval = sem_destroy(&m_semaphore);
54         assert(sem_destroy_retval == 0);
55         UNUSED(sem_destroy_retval);
56 }
57
58 JSemaphore::JSemaphore(int initval) {
59         int sem_init_retval = sem_init(&m_semaphore,0,initval);
60         assert(sem_init_retval == 0);
61         UNUSED(sem_init_retval);
62 }
63
64 void JSemaphore::Post() {
65         int sem_post_retval = sem_post(&m_semaphore);
66         assert(sem_post_retval == 0);
67         UNUSED(sem_post_retval);
68 #ifdef __MACH__
69         pthread_mutex_lock(&semcount_mutex);
70         semcount++;
71         pthread_mutex_unlock(&semcount_mutex);
72 #endif
73 }
74
75 void JSemaphore::Wait() {
76         int sem_wait_retval = sem_wait(&m_semaphore);
77         assert(sem_wait_retval == 0);
78         UNUSED(sem_wait_retval);
79 #ifdef __MACH__
80         pthread_mutex_lock(&semcount_mutex);
81         semcount--;
82         pthread_mutex_unlock(&semcount_mutex);
83 #endif
84 }
85
86 bool JSemaphore::Wait(unsigned int time_ms) {
87 #ifdef __MACH__
88         mach_timespec_t waittime;
89         waittime.tv_sec = time_ms / 1000;
90         waittime.tv_nsec = 1000000 * (time_ms % 1000);
91 #else
92         struct timespec waittime;
93 #endif
94         struct timeval now;
95
96         if (gettimeofday(&now, NULL) == -1) {
97                 assert("Unable to get time by clock_gettime!" == 0);
98                 return false;
99         }
100
101 #ifndef __MACH__
102         waittime.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000);
103         waittime.tv_sec  = (time_ms / 1000) + (waittime.tv_nsec / (1000*1000*1000)) + now.tv_sec;
104         waittime.tv_nsec %= 1000*1000*1000;
105 #endif
106
107         errno = 0;
108 #ifdef __MACH__
109         int sem_wait_retval = semaphore_timedwait(m_semaphore, waittime);
110 #else
111         int sem_wait_retval = sem_timedwait(&m_semaphore, &waittime);
112 #endif
113
114         if (sem_wait_retval == 0)
115         {
116 #ifdef __MACH__
117                 pthread_mutex_lock(&semcount_mutex);
118                 semcount--;
119                 pthread_mutex_unlock(&semcount_mutex);
120 #endif
121                 return true;
122         }
123         else {
124                 assert((errno == ETIMEDOUT) || (errno == EINTR));
125                 return false;
126         }
127         return sem_wait_retval == 0 ? true : false;
128 }
129
130 int JSemaphore::GetValue() {
131         int retval = 0;
132 #ifdef __MACH__
133         pthread_mutex_lock(&semcount_mutex);
134         retval = semcount;
135         pthread_mutex_unlock(&semcount_mutex);
136 #else
137         sem_getvalue(&m_semaphore, &retval);
138 #endif
139         return retval;
140 }
141