Merge pull request #1068 from esdrubal/bug18421
[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 <errno.h>
12 #include "utils/mono-semaphore.h"
13 #ifdef HAVE_SYS_TIME_H
14 #include <sys/time.h>
15 #endif
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19
20 #if (defined (HAVE_SEMAPHORE_H) || defined (USE_MACH_SEMA)) && !defined(HOST_WIN32)
21 /* sem_* or semaphore_* functions in use */
22 #  ifdef USE_MACH_SEMA
23 #    define TIMESPEC mach_timespec_t
24 #    define WAIT_BLOCK(a,b) semaphore_timedwait (*(a), *(b))
25 #  elif defined(__native_client__) && defined(USE_NEWLIB)
26 #    define TIMESPEC struct timespec
27 #    define WAIT_BLOCK(a, b) sem_trywait(a)
28 #  else
29 #    define TIMESPEC struct timespec
30 #    define WAIT_BLOCK(a,b) sem_timedwait (a, b)
31 #  endif
32
33 #ifndef NSEC_PER_SEC
34 #define NSEC_PER_SEC 1000000000
35 #endif
36
37 int
38 mono_sem_timedwait (MonoSemType *sem, guint32 timeout_ms, gboolean alertable)
39 {
40         TIMESPEC ts, copy;
41         struct timeval t;
42         int res = 0;
43
44 #ifndef USE_MACH_SEMA
45         if (timeout_ms == 0)
46                 return sem_trywait (sem);
47 #endif
48         if (timeout_ms == (guint32) 0xFFFFFFFF)
49                 return mono_sem_wait (sem, alertable);
50
51 #ifdef USE_MACH_SEMA
52         memset (&t, 0, sizeof (t));
53 #else
54         gettimeofday (&t, NULL);
55 #endif
56         ts.tv_sec = timeout_ms / 1000 + t.tv_sec;
57         ts.tv_nsec = (timeout_ms % 1000) * 1000000 + t.tv_usec * 1000;
58         while (ts.tv_nsec > NSEC_PER_SEC) {
59                 ts.tv_nsec -= NSEC_PER_SEC;
60                 ts.tv_sec++;
61         }
62
63         copy = ts;
64         while ((res = WAIT_BLOCK (sem, &ts)) == -1 && errno == EINTR) {
65                 struct timeval current;
66                 if (alertable)
67                         return -1;
68 #ifdef USE_MACH_SEMA
69                 memset (&current, 0, sizeof (current));
70 #else
71                 gettimeofday (&current, NULL);
72 #endif
73                 ts = copy;
74                 ts.tv_sec -= (current.tv_sec - t.tv_sec);
75                 ts.tv_nsec -= (current.tv_usec - t.tv_usec) * 1000;
76                 if (ts.tv_nsec < 0) {
77                         if (ts.tv_sec <= 0) {
78                                 ts.tv_nsec = 0;
79                         } else {
80                                 ts.tv_sec--;
81                                 ts.tv_nsec += NSEC_PER_SEC;
82                         }
83                 }
84                 if (ts.tv_sec < 0) {
85                         ts.tv_sec = 0;
86                         ts.tv_nsec = 0;
87                 }
88         }
89
90         /* OSX might return > 0 for error */
91         if (res != 0)
92                 res = -1;
93         return res;
94 }
95
96 int
97 mono_sem_wait (MonoSemType *sem, gboolean alertable)
98 {
99         int res;
100 #ifndef USE_MACH_SEMA
101         while ((res = sem_wait (sem)) == -1 && errno == EINTR)
102 #else
103         while ((res = semaphore_wait (*sem)) == KERN_ABORTED)
104 #endif
105         {
106                 if (alertable)
107                         return -1;
108         }
109         /* OSX might return > 0 for error */
110         if (res != 0)
111                 res = -1;
112         return res;
113 }
114
115 int
116 mono_sem_post (MonoSemType *sem)
117 {
118         int res;
119 #ifndef USE_MACH_SEMA
120         while ((res = sem_post (sem)) == -1 && errno == EINTR);
121 #else
122         res = semaphore_signal (*sem);
123         /* OSX might return > 0 for error */
124         if (res != KERN_SUCCESS)
125                 res = -1;
126 #endif
127         return res;
128 }
129
130 #else
131 /* Windows or io-layer functions in use */
132 int
133 mono_sem_wait (MonoSemType *sem, gboolean alertable)
134 {
135         return mono_sem_timedwait (sem, INFINITE, alertable);
136 }
137
138 int
139 mono_sem_timedwait (MonoSemType *sem, guint32 timeout_ms, gboolean alertable)
140 {
141         gboolean res;
142
143         while ((res = WaitForSingleObjectEx (*sem, timeout_ms, alertable)) == WAIT_IO_COMPLETION) {
144                 if (alertable) {
145                         errno = EINTR;
146                         return -1;
147                 }
148         }
149         switch (res) {
150         case WAIT_OBJECT_0:
151                 return 0;    
152         // WAIT_TIMEOUT and WAIT_FAILED
153         default:
154                 return -1;
155         }
156 }
157
158 int
159 mono_sem_post (MonoSemType *sem)
160 {
161         if (!ReleaseSemaphore (*sem, 1, NULL))
162                 return -1;
163         return 0;
164 }
165 #endif
166