Updated TODO.
[mpdm.git] / mpdm_t.c
blobeda7d0451778d8d3078f52cad02d03cc98564493
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 mpdm_t.c - Threading primitives
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <locale.h>
32 #include <time.h>
34 #ifdef CONFOPT_WIN32
35 #include <windows.h>
36 #endif
38 #ifdef CONFOPT_PTHREADS
39 #include <pthread.h>
40 #endif
42 #ifdef CONFOPT_POSIXSEMS
43 #include <semaphore.h>
44 #endif
46 #include "mpdm.h"
49 /** code **/
51 /**
52 * mpdm_sleep - Sleeps a number of milliseconds.
53 * @msecs: the milliseconds to sleep
55 * Sleeps a number of milliseconds.
56 * [Threading]
58 void mpdm_sleep(int msecs)
60 #ifdef CONFOPT_WIN32
62 Sleep(msecs);
64 #endif
66 #ifdef CONFOPT_NANOSLEEP
67 struct timespec ts;
69 ts.tv_sec = msecs / 1000;
70 ts.tv_nsec = (msecs % 1000) * 1000000;
72 nanosleep(&ts, NULL);
73 #endif
77 /** mutexes **/
79 /**
80 * mpdm_new_mutex - Creates a new mutex.
82 * Creates a new mutex.
83 * [Threading]
85 mpdm_t mpdm_new_mutex(void)
87 char *ptr = NULL;
88 int size = 0;
90 #ifdef CONFOPT_WIN32
91 HANDLE h;
93 h = CreateMutex(NULL, FALSE, NULL);
95 if (h != NULL) {
96 size = sizeof(h);
97 ptr = (char *) &h;
99 #endif
101 #ifdef CONFOPT_PTHREADS
102 pthread_mutex_t m;
104 if (pthread_mutex_init(&m, NULL) == 0) {
105 size = sizeof(m);
106 ptr = (char *) &m;
109 #endif
111 return MPDM_C(MPDM_MUTEX, ptr, size);
116 * mpdm_mutex_lock - Locks a mutex.
117 * @mutex: the mutex to be locked
119 * Locks a mutex. If the mutex is not already locked,
120 * it waits until it is.
121 * [Threading]
123 void mpdm_mutex_lock(mpdm_t mutex)
125 #ifdef CONFOPT_WIN32
126 HANDLE *h = (HANDLE *) mutex->data;
128 WaitForSingleObject(*h, INFINITE);
129 #endif
131 #ifdef CONFOPT_PTHREADS
132 pthread_mutex_t *m = (pthread_mutex_t *) mutex->data;
134 pthread_mutex_lock(m);
135 #endif
140 * mpdm_mutex_unlock - Unlocks a mutex.
141 * @mutex: the mutex to be unlocked
143 * Unlocks a previously locked mutex. The thread
144 * unlocking the mutex must be the one who locked it.
145 * [Threading]
147 void mpdm_mutex_unlock(mpdm_t mutex)
149 #ifdef CONFOPT_WIN32
150 HANDLE *h = (HANDLE *) mutex->data;
152 ReleaseMutex(*h);
153 #endif
155 #ifdef CONFOPT_PTHREADS
156 pthread_mutex_t *m = (pthread_mutex_t *) mutex->data;
158 pthread_mutex_unlock(m);
159 #endif
163 /** semaphores **/
166 * mpdm_new_semaphore - Creates a new semaphore.
167 * @init_value: the initial value of the semaphore.
169 * Creates a new semaphore with an @init_value.
170 * [Threading]
172 mpdm_t mpdm_new_semaphore(int init_value)
174 char *ptr = NULL;
175 int size = 0;
177 #ifdef CONFOPT_WIN32
178 HANDLE h;
180 if ((h = CreateSemaphore(NULL, init_value, 0x7fffffff, NULL)) != NULL) {
181 size = sizeof(h);
182 ptr = (char *) &h;
185 #endif
187 #ifdef CONFOPT_POSIXSEMS
188 sem_t s;
190 if (sem_init(&s, 0, init_value) == 0) {
191 size = sizeof(s);
192 ptr = (char *) &s;
195 #endif
197 return MPDM_C(MPDM_SEMAPHORE, ptr, size);
202 * mpdm_semaphore_wait - Waits for a semaphore to be ready.
203 * @sem: the semaphore to wait onto
205 * Waits for the value of a semaphore to be > 0. If it's
206 * not, the thread waits until it is.
207 * [Threading]
209 void mpdm_semaphore_wait(mpdm_t sem)
211 #ifdef CONFOPT_WIN32
212 HANDLE *h = (HANDLE *) sem->data;
214 WaitForSingleObject(*h, INFINITE);
215 #endif
217 #ifdef CONFOPT_POSIXSEMS
218 sem_t *s = (sem_t *) sem->data;
220 sem_wait(s);
221 #endif
226 * mpdm_semaphore_post - Increments the value of a semaphore.
227 * @sem: the semaphore to increment
229 * Increments by 1 the value of a semaphore.
230 * [Threading]
232 void mpdm_semaphore_post(mpdm_t sem)
234 #ifdef CONFOPT_WIN32
235 HANDLE *h = (HANDLE *) sem->data;
237 ReleaseSemaphore(*h, 1, NULL);
238 #endif
240 #ifdef CONFOPT_POSIXSEMS
241 sem_t *s = (sem_t *) sem->data;
243 sem_post(s);
244 #endif
248 /** threads **/
250 static void thread_caller(mpdm_t a)
252 /* ignore return value */
253 mpdm_void(mpdm_exec
254 (mpdm_aget(a, 0), mpdm_aget(a, 1), mpdm_aget(a, 2)));
256 /* was referenced in mpdm_exec_thread() */
257 mpdm_unref(a);
261 #ifdef CONFOPT_WIN32
262 DWORD WINAPI win32_thread(LPVOID param)
264 thread_caller((mpdm_t) param);
266 return 0;
268 #endif
270 #ifdef CONFOPT_PTHREADS
271 void *pthreads_thread(void *args)
273 thread_caller((mpdm_t) args);
275 return NULL;
277 #endif
280 * mpdm_exec_thread - Runs an executable value in a new thread.
281 * @c: the executable value
282 * @args: executable arguments
283 * @ctxt: the context
285 * Runs the @c executable value in a new thread. The code
286 * starts executing immediately. The @args and @ctxt arguments
287 * are sent to the executable value as arguments.
289 * Returns a handle for the thread.
290 * [Threading]
292 mpdm_t mpdm_exec_thread(mpdm_t c, mpdm_t args, mpdm_t ctxt)
294 mpdm_t a;
295 char *ptr = NULL;
296 int size = 0;
298 if (ctxt == NULL)
299 ctxt = MPDM_A(0);
301 /* to be unreferenced at thread stop */
302 a = mpdm_ref(MPDM_A(3));
304 mpdm_aset(a, c, 0);
305 mpdm_aset(a, args, 1);
306 mpdm_aset(a, ctxt, 2);
308 #ifdef CONFOPT_WIN32
309 HANDLE t;
311 t = CreateThread(NULL, 0, win32_thread, a, 0, NULL);
313 if (t != NULL) {
314 size = sizeof(t);
315 ptr = (char *) &t;
318 #endif
320 #ifdef CONFOPT_PTHREADS
321 pthread_t pt;
323 if (pthread_create(&pt, NULL, pthreads_thread, a) == 0) {
324 size = sizeof(pthread_t);
325 ptr = (char *) &pt;
328 #endif
330 return MPDM_C(MPDM_THREAD, ptr, size);