]> git.lizzy.rs Git - dragonfireclient.git/blob - src/threading/semaphore.cpp
Require 'basic_debug' priv to view gameplay-relevant debug info, require 'debug'...
[dragonfireclient.git] / src / threading / semaphore.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
20 #include "threading/semaphore.h"
21
22 #include <iostream>
23 #include <cstdlib>
24 #include <cassert>
25
26 #define UNUSED(expr) do { (void)(expr); } while (0)
27
28 #ifdef _WIN32
29         #include <climits>
30         #define MAX_SEMAPHORE_COUNT LONG_MAX - 1
31 #else
32         #include <cerrno>
33         #include <sys/time.h>
34         #include <pthread.h>
35         #if defined(__MACH__) && defined(__APPLE__)
36                 #include <mach/mach.h>
37                 #include <mach/task.h>
38                 #include <mach/semaphore.h>
39                 #include <sys/semaphore.h>
40                 #include <unistd.h>
41
42                 #undef sem_t
43                 #undef sem_init
44                 #undef sem_wait
45                 #undef sem_post
46                 #undef sem_destroy
47                 #define sem_t             semaphore_t
48                 #define sem_init(s, p, c) semaphore_create(mach_task_self(), (s), 0, (c))
49                 #define sem_wait(s)       semaphore_wait(*(s))
50                 #define sem_post(s)       semaphore_signal(*(s))
51                 #define sem_destroy(s)    semaphore_destroy(mach_task_self(), *(s))
52         #endif
53 #endif
54
55
56 Semaphore::Semaphore(int val)
57 {
58 #ifdef _WIN32
59         semaphore = CreateSemaphore(NULL, val, MAX_SEMAPHORE_COUNT, NULL);
60 #else
61         int ret = sem_init(&semaphore, 0, val);
62         assert(!ret);
63         UNUSED(ret);
64 #endif
65 }
66
67
68 Semaphore::~Semaphore()
69 {
70 #ifdef _WIN32
71         CloseHandle(semaphore);
72 #else
73         int ret = sem_destroy(&semaphore);
74 #ifdef __ANDROID__
75         // Workaround for broken bionic semaphore implementation!
76         assert(!ret || errno == EBUSY);
77 #else
78         assert(!ret);
79 #endif
80         UNUSED(ret);
81 #endif
82 }
83
84
85 void Semaphore::post(unsigned int num)
86 {
87         assert(num > 0);
88 #ifdef _WIN32
89         ReleaseSemaphore(semaphore, num, NULL);
90 #else
91         for (unsigned i = 0; i < num; i++) {
92                 int ret = sem_post(&semaphore);
93                 assert(!ret);
94                 UNUSED(ret);
95         }
96 #endif
97 }
98
99
100 void Semaphore::wait()
101 {
102 #ifdef _WIN32
103         WaitForSingleObject(semaphore, INFINITE);
104 #else
105         int ret = sem_wait(&semaphore);
106         assert(!ret);
107         UNUSED(ret);
108 #endif
109 }
110
111
112 bool Semaphore::wait(unsigned int time_ms)
113 {
114 #ifdef _WIN32
115         unsigned int ret = WaitForSingleObject(semaphore, time_ms);
116
117         if (ret == WAIT_OBJECT_0) {
118                 return true;
119         } else {
120                 assert(ret == WAIT_TIMEOUT);
121                 return false;
122         }
123 #else
124 # if defined(__MACH__) && defined(__APPLE__)
125         mach_timespec_t wait_time;
126         wait_time.tv_sec = time_ms / 1000;
127         wait_time.tv_nsec = 1000000 * (time_ms % 1000);
128
129         errno = 0;
130         int ret = semaphore_timedwait(semaphore, wait_time);
131         switch (ret) {
132         case KERN_OPERATION_TIMED_OUT:
133                 errno = ETIMEDOUT;
134                 break;
135         case KERN_ABORTED:
136                 errno = EINTR;
137                 break;
138         default:
139                 if (ret)
140                         errno = EINVAL;
141         }
142 # else
143         int ret;
144         if (time_ms > 0) {
145                 struct timespec wait_time;
146                 struct timeval now;
147
148                 if (gettimeofday(&now, NULL) == -1) {
149                         std::cerr << "Semaphore::wait(ms): Unable to get time with gettimeofday!" << std::endl;
150                         abort();
151                 }
152
153                 wait_time.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000);
154                 wait_time.tv_sec  = (time_ms / 1000) + (wait_time.tv_nsec / (1000 * 1000 * 1000)) + now.tv_sec;
155                 wait_time.tv_nsec %= 1000 * 1000 * 1000;
156
157                 ret = sem_timedwait(&semaphore, &wait_time);
158         } else {
159                 ret = sem_trywait(&semaphore);
160         }
161 # endif
162
163         assert(!ret || (errno == ETIMEDOUT || errno == EINTR || errno == EAGAIN));
164         return !ret;
165 #endif
166 }
167