1 /*
2  * Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #if defined(__APPLE__) || defined(__MINGW32__)
18 #include <sys/time.h>
19 #endif
20 #ifdef __APPLE__
21 #include <mach/semaphore.h>
22 #include <mach/task.h>
23 #include <mach/mach_init.h>
24 #else
25 #define _GNU_SOURCE
26 #include <semaphore.h>
27 #endif
28 
29 #if defined(__FreeBSD__)
30 #include <pthread_np.h>
31 #endif
32 
33 #include "bmem.h"
34 #include "threading.h"
35 
36 struct os_event_data {
37 	pthread_mutex_t mutex;
38 	pthread_cond_t cond;
39 	volatile bool signalled;
40 	bool manual;
41 };
42 
os_event_init(os_event_t ** event,enum os_event_type type)43 int os_event_init(os_event_t **event, enum os_event_type type)
44 {
45 	int code = 0;
46 
47 	struct os_event_data *data = bzalloc(sizeof(struct os_event_data));
48 
49 	if ((code = pthread_mutex_init(&data->mutex, NULL)) < 0) {
50 		bfree(data);
51 		return code;
52 	}
53 
54 	if ((code = pthread_cond_init(&data->cond, NULL)) < 0) {
55 		pthread_mutex_destroy(&data->mutex);
56 		bfree(data);
57 		return code;
58 	}
59 
60 	data->manual = (type == OS_EVENT_TYPE_MANUAL);
61 	data->signalled = false;
62 	*event = data;
63 
64 	return 0;
65 }
66 
os_event_destroy(os_event_t * event)67 void os_event_destroy(os_event_t *event)
68 {
69 	if (event) {
70 		pthread_mutex_destroy(&event->mutex);
71 		pthread_cond_destroy(&event->cond);
72 		bfree(event);
73 	}
74 }
75 
os_event_wait(os_event_t * event)76 int os_event_wait(os_event_t *event)
77 {
78 	int code = 0;
79 	pthread_mutex_lock(&event->mutex);
80 	while (!event->signalled) {
81 		code = pthread_cond_wait(&event->cond, &event->mutex);
82 		if (code != 0)
83 			break;
84 	}
85 
86 	if (code == 0) {
87 		if (!event->manual)
88 			event->signalled = false;
89 	}
90 
91 	pthread_mutex_unlock(&event->mutex);
92 
93 	return code;
94 }
95 
add_ms_to_ts(struct timespec * ts,unsigned long milliseconds)96 static inline void add_ms_to_ts(struct timespec *ts, unsigned long milliseconds)
97 {
98 	ts->tv_sec += milliseconds / 1000;
99 	ts->tv_nsec += (milliseconds % 1000) * 1000000;
100 	if (ts->tv_nsec > 1000000000) {
101 		ts->tv_sec += 1;
102 		ts->tv_nsec -= 1000000000;
103 	}
104 }
105 
os_event_timedwait(os_event_t * event,unsigned long milliseconds)106 int os_event_timedwait(os_event_t *event, unsigned long milliseconds)
107 {
108 	int code = 0;
109 	pthread_mutex_lock(&event->mutex);
110 	while (!event->signalled) {
111 		struct timespec ts;
112 #if defined(__APPLE__) || defined(__MINGW32__)
113 		struct timeval tv;
114 		gettimeofday(&tv, NULL);
115 		ts.tv_sec = tv.tv_sec;
116 		ts.tv_nsec = tv.tv_usec * 1000;
117 #else
118 		clock_gettime(CLOCK_REALTIME, &ts);
119 #endif
120 		add_ms_to_ts(&ts, milliseconds);
121 		code = pthread_cond_timedwait(&event->cond, &event->mutex, &ts);
122 		if (code != 0)
123 			break;
124 	}
125 
126 	if (code == 0) {
127 		if (!event->manual)
128 			event->signalled = false;
129 	}
130 
131 	pthread_mutex_unlock(&event->mutex);
132 
133 	return code;
134 }
135 
os_event_try(os_event_t * event)136 int os_event_try(os_event_t *event)
137 {
138 	int ret = EAGAIN;
139 
140 	pthread_mutex_lock(&event->mutex);
141 	if (event->signalled) {
142 		if (!event->manual)
143 			event->signalled = false;
144 		ret = 0;
145 	}
146 	pthread_mutex_unlock(&event->mutex);
147 
148 	return ret;
149 }
150 
os_event_signal(os_event_t * event)151 int os_event_signal(os_event_t *event)
152 {
153 	int code = 0;
154 
155 	pthread_mutex_lock(&event->mutex);
156 	code = pthread_cond_signal(&event->cond);
157 	event->signalled = true;
158 	pthread_mutex_unlock(&event->mutex);
159 
160 	return code;
161 }
162 
os_event_reset(os_event_t * event)163 void os_event_reset(os_event_t *event)
164 {
165 	pthread_mutex_lock(&event->mutex);
166 	event->signalled = false;
167 	pthread_mutex_unlock(&event->mutex);
168 }
169 
170 #ifdef __APPLE__
171 
172 struct os_sem_data {
173 	semaphore_t sem;
174 	task_t task;
175 };
176 
os_sem_init(os_sem_t ** sem,int value)177 int os_sem_init(os_sem_t **sem, int value)
178 {
179 	semaphore_t new_sem;
180 	task_t task = mach_task_self();
181 
182 	if (semaphore_create(task, &new_sem, 0, value) != KERN_SUCCESS)
183 		return -1;
184 
185 	*sem = bzalloc(sizeof(struct os_sem_data));
186 	if (!*sem)
187 		return -2;
188 
189 	(*sem)->sem = new_sem;
190 	(*sem)->task = task;
191 	return 0;
192 }
193 
os_sem_destroy(os_sem_t * sem)194 void os_sem_destroy(os_sem_t *sem)
195 {
196 	if (sem) {
197 		semaphore_destroy(sem->task, sem->sem);
198 		bfree(sem);
199 	}
200 }
201 
os_sem_post(os_sem_t * sem)202 int os_sem_post(os_sem_t *sem)
203 {
204 	if (!sem)
205 		return -1;
206 	return (semaphore_signal(sem->sem) == KERN_SUCCESS) ? 0 : -1;
207 }
208 
os_sem_wait(os_sem_t * sem)209 int os_sem_wait(os_sem_t *sem)
210 {
211 	if (!sem)
212 		return -1;
213 	return (semaphore_wait(sem->sem) == KERN_SUCCESS) ? 0 : -1;
214 }
215 
216 #else
217 
218 struct os_sem_data {
219 	sem_t sem;
220 };
221 
os_sem_init(os_sem_t ** sem,int value)222 int os_sem_init(os_sem_t **sem, int value)
223 {
224 	sem_t new_sem;
225 	int ret = sem_init(&new_sem, 0, value);
226 	if (ret != 0)
227 		return ret;
228 
229 	*sem = bzalloc(sizeof(struct os_sem_data));
230 	(*sem)->sem = new_sem;
231 	return 0;
232 }
233 
os_sem_destroy(os_sem_t * sem)234 void os_sem_destroy(os_sem_t *sem)
235 {
236 	if (sem) {
237 		sem_destroy(&sem->sem);
238 		bfree(sem);
239 	}
240 }
241 
os_sem_post(os_sem_t * sem)242 int os_sem_post(os_sem_t *sem)
243 {
244 	if (!sem)
245 		return -1;
246 	return sem_post(&sem->sem);
247 }
248 
os_sem_wait(os_sem_t * sem)249 int os_sem_wait(os_sem_t *sem)
250 {
251 	if (!sem)
252 		return -1;
253 	return sem_wait(&sem->sem);
254 }
255 
256 #endif
257 
os_set_thread_name(const char * name)258 void os_set_thread_name(const char *name)
259 {
260 #if defined(__APPLE__)
261 	pthread_setname_np(name);
262 #elif defined(__FreeBSD__)
263 	pthread_set_name_np(pthread_self(), name);
264 #elif defined(__GLIBC__) && !defined(__MINGW32__)
265 	if (strlen(name) <= 15) {
266 		pthread_setname_np(pthread_self(), name);
267 	} else {
268 		char *thread_name = bstrdup_n(name, 15);
269 		pthread_setname_np(pthread_self(), thread_name);
270 		bfree(thread_name);
271 	}
272 #endif
273 }
274