2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mono / utils / mono-semaphore.c
1 /*
2  * mono-semaphore.c: mono-semaphore functions
3  *
4  * Author:
5  *      Gonzalo Paniagua Javier  <gonzalo@novell.com>
6  *
7  * (C) 2010 Novell, Inc.
8  */
9
10 #include <config.h>
11 #include "utils/mono-semaphore.h"
12
13 #if defined(HAVE_SEMAPHORE_H) || defined(USE_MACH_SEMA)
14 /* sem_* or semaphore_* functions in use */
15 #  ifdef USE_MACH_SEMA
16 #    define TIMESPEC mach_timespec_t
17 #    define WAIT_BLOCK(a,b) semaphore_timedwait (*(a), *(b))
18 #  else
19 #    define TIMESPEC struct timespec
20 #    define WAIT_BLOCK(a,b) sem_timedwait (a, b)
21 #  endif
22
23 gboolean
24 mono_sem_timedwait (MonoSemType *sem, guint32 timeout_ms)
25 {
26         TIMESPEC tv;
27
28 #ifndef USE_MACH_SEMA
29         if (timeout_ms == 0)
30                 return (!sem_trywait (sem));
31 #endif
32
33         tv.tv_sec = timeout_ms / 1000;
34         tv.tv_nsec = (timeout_ms % 1000) * 1000000;
35         return (!WAIT_BLOCK (sem, &tv));
36 }
37
38 #else
39 /* Windows or io-layer functions in use */
40 gboolean
41 mono_sem_timedwait (MonoSemType *sem, guint32 timeout_ms)
42 {
43         return WaitForSingleObjectEx (*sem, timeout_ms, TRUE);
44 }
45
46 #endif
47