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