d21ef9743faf77fc4c542b77dae548350425d19a
[mono.git] / mono / metadata / w32semaphore-unix.c
1 /*
2  * w32semaphore-unix.c: Runtime support for managed Semaphore on Unix
3  *
4  * Author:
5  *      Ludovic Henry (luhenry@microsoft.com)
6  *
7  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8  */
9
10 #include "w32semaphore.h"
11
12 #include "w32handle-namespace.h"
13 #include "mono/io-layer/io-layer.h"
14 #include "mono/utils/mono-logger-internals.h"
15 #include "mono/utils/w32handle.h"
16
17 typedef struct {
18         guint32 val;
19         gint32 max;
20 } MonoW32HandleSemaphore;
21
22 struct MonoW32HandleNamedSemaphore {
23         MonoW32HandleSemaphore s;
24         MonoW32HandleNamespace sharedns;
25 };
26
27 static gboolean sem_handle_own (gpointer handle, MonoW32HandleType type)
28 {
29         MonoW32HandleSemaphore *sem_handle;
30
31         if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
32                 g_warning ("%s: error looking up %s handle %p",
33                         __func__, mono_w32handle_ops_typename (type), handle);
34                 return FALSE;
35         }
36
37         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
38                 __func__, mono_w32handle_ops_typename (type), handle);
39
40         sem_handle->val--;
41
42         if (sem_handle->val == 0)
43                 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
44
45         return TRUE;
46 }
47
48 static void sema_signal(gpointer handle)
49 {
50         ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal(handle, 1, NULL);
51 }
52
53 static gboolean sema_own (gpointer handle)
54 {
55         return sem_handle_own (handle, MONO_W32HANDLE_SEM);
56 }
57
58 static void namedsema_signal (gpointer handle)
59 {
60         ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (handle, 1, NULL);
61 }
62
63 /* NB, always called with the shared handle lock held */
64 static gboolean namedsema_own (gpointer handle)
65 {
66         return sem_handle_own (handle, MONO_W32HANDLE_NAMEDSEM);
67 }
68
69 static void sema_details (gpointer data)
70 {
71         MonoW32HandleSemaphore *sem = (MonoW32HandleSemaphore *)data;
72         g_print ("val: %5u, max: %5d", sem->val, sem->max);
73 }
74
75 static void namedsema_details (gpointer data)
76 {
77         MonoW32HandleNamedSemaphore *namedsem = (MonoW32HandleNamedSemaphore *)data;
78         g_print ("val: %5u, max: %5d, name: \"%s\"", namedsem->s.val, namedsem->s.max, namedsem->sharedns.name);
79 }
80
81 static const gchar* sema_typename (void)
82 {
83         return "Semaphore";
84 }
85
86 static gsize sema_typesize (void)
87 {
88         return sizeof (MonoW32HandleSemaphore);
89 }
90
91 static const gchar* namedsema_typename (void)
92 {
93         return "N.Semaphore";
94 }
95
96 static gsize namedsema_typesize (void)
97 {
98         return sizeof (MonoW32HandleNamedSemaphore);
99 }
100
101 void
102 mono_w32semaphore_init (void)
103 {
104         static MonoW32HandleOps sem_ops = {
105                 NULL,                   /* close */
106                 sema_signal,            /* signal */
107                 sema_own,               /* own */
108                 NULL,                   /* is_owned */
109                 NULL,                   /* special_wait */
110                 NULL,                   /* prewait */
111                 sema_details,   /* details */
112                 sema_typename,  /* typename */
113                 sema_typesize,  /* typesize */
114         };
115
116         static MonoW32HandleOps namedsem_ops = {
117                 NULL,                   /* close */
118                 namedsema_signal,       /* signal */
119                 namedsema_own,          /* own */
120                 NULL,                   /* is_owned */
121                 NULL,                   /* special_wait */
122                 NULL,                   /* prewait */
123                 namedsema_details,      /* details */
124                 namedsema_typename,     /* typename */
125                 namedsema_typesize,     /* typesize */
126         };
127
128         mono_w32handle_register_ops (MONO_W32HANDLE_SEM,      &sem_ops);
129         mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDSEM, &namedsem_ops);
130
131         mono_w32handle_register_capabilities (MONO_W32HANDLE_SEM,
132                 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
133         mono_w32handle_register_capabilities (MONO_W32HANDLE_NAMEDSEM,
134                 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
135 }
136
137 static gpointer
138 sem_handle_create (MonoW32HandleSemaphore *sem_handle, MonoW32HandleType type, gint32 initial, gint32 max)
139 {
140         gpointer handle;
141         int thr_ret;
142
143         sem_handle->val = initial;
144         sem_handle->max = max;
145
146         handle = mono_w32handle_new (type, sem_handle);
147         if (handle == INVALID_HANDLE_VALUE) {
148                 g_warning ("%s: error creating %s handle",
149                         __func__, mono_w32handle_ops_typename (type));
150                 SetLastError (ERROR_GEN_FAILURE);
151                 return NULL;
152         }
153
154         thr_ret = mono_w32handle_lock_handle (handle);
155         g_assert (thr_ret == 0);
156
157         if (initial != 0)
158                 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
159
160         thr_ret = mono_w32handle_unlock_handle (handle);
161         g_assert (thr_ret == 0);
162
163         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
164                 __func__, mono_w32handle_ops_typename (type), handle);
165
166         return handle;
167 }
168
169 static gpointer
170 sem_create (gint32 initial, gint32 max)
171 {
172         MonoW32HandleSemaphore sem_handle;
173         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle, initial %d max %d",
174                 __func__, mono_w32handle_ops_typename (MONO_W32HANDLE_SEM), initial, max);
175         return sem_handle_create (&sem_handle, MONO_W32HANDLE_SEM, initial, max);
176 }
177
178 static gpointer
179 namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
180 {
181         gpointer handle;
182         gchar *utf8_name;
183
184         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle, initial %d max %d name \"%s\"",
185                 __func__, mono_w32handle_ops_typename (MONO_W32HANDLE_NAMEDSEM), initial, max, name);
186
187         /* w32 seems to guarantee that opening named objects can't race each other */
188         mono_w32handle_namespace_lock ();
189
190         utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
191
192         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating named sem name [%s] initial %d max %d", __func__, utf8_name, initial, max);
193
194         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDSEM, utf8_name);
195         if (handle == INVALID_HANDLE_VALUE) {
196                 /* The name has already been used for a different object. */
197                 handle = NULL;
198                 SetLastError (ERROR_INVALID_HANDLE);
199         } else if (handle) {
200                 /* Not an error, but this is how the caller is informed that the semaphore wasn't freshly created */
201                 SetLastError (ERROR_ALREADY_EXISTS);
202
203                 /* this is used as creating a new handle */
204                 mono_w32handle_ref (handle);
205         } else {
206                 /* A new named semaphore */
207                 MonoW32HandleNamedSemaphore namedsem_handle;
208
209                 strncpy (&namedsem_handle.sharedns.name [0], utf8_name, MAX_PATH);
210                 namedsem_handle.sharedns.name [MAX_PATH] = '\0';
211
212                 handle = sem_handle_create ((MonoW32HandleSemaphore*) &namedsem_handle, MONO_W32HANDLE_NAMEDSEM, initial, max);
213         }
214
215         g_free (utf8_name);
216
217         mono_w32handle_namespace_unlock ();
218
219         return handle;
220 }
221
222 gpointer
223 ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, gint32 *error)
224
225         gpointer sem;
226
227         if (maximumCount <= 0) {
228                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: maximumCount <= 0", __func__);
229
230                 *error = ERROR_INVALID_PARAMETER;
231                 return NULL;
232         }
233
234         if (initialCount > maximumCount || initialCount < 0) {
235                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: initialCount > maximumCount or < 0", __func__);
236
237                 *error = ERROR_INVALID_PARAMETER;
238                 return NULL;
239         }
240
241         /* Need to blow away any old errors here, because code tests
242          * for ERROR_ALREADY_EXISTS on success (!) to see if a
243          * semaphore was freshly created
244          */
245         SetLastError (ERROR_SUCCESS);
246
247         if (!name)
248                 sem = sem_create (initialCount, maximumCount);
249         else
250                 sem = namedsem_create (initialCount, maximumCount, mono_string_chars (name));
251
252         *error = GetLastError ();
253
254         return sem;
255 }
256
257 MonoBoolean
258 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (gpointer handle, gint32 releaseCount, gint32 *prevcount)
259 {
260         MonoW32HandleType type;
261         MonoW32HandleSemaphore *sem_handle;
262         int thr_ret;
263         MonoBoolean ret;
264
265         if (!handle) {
266                 SetLastError (ERROR_INVALID_HANDLE);
267                 return FALSE;
268         }
269
270         switch (type = mono_w32handle_get_type (handle)) {
271         case MONO_W32HANDLE_SEM:
272         case MONO_W32HANDLE_NAMEDSEM:
273                 break;
274         default:
275                 SetLastError (ERROR_INVALID_HANDLE);
276                 return FALSE;
277         }
278
279         if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
280                 g_warning ("%s: error looking up sem handle %p", __func__, handle);
281                 return FALSE;
282         }
283
284         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: releasing %s handle %p",
285                 __func__, mono_w32handle_ops_typename (type), handle);
286
287         thr_ret = mono_w32handle_lock_handle (handle);
288         g_assert (thr_ret == 0);
289
290         /* Do this before checking for count overflow, because overflowing
291          * max is a listed technique for finding the current value */
292         if (prevcount)
293                 *prevcount = sem_handle->val;
294
295         /* No idea why max is signed, but thats the spec :-( */
296         if (sem_handle->val + releaseCount > (guint32)sem_handle->max) {
297                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s handle %p val %d count %d max %d, max value would be exceeded",
298                         __func__, mono_w32handle_ops_typename (type), handle, sem_handle->val, releaseCount, sem_handle->max);
299
300                 ret = FALSE;
301         } else {
302                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s handle %p val %d count %d max %d",
303                         __func__, mono_w32handle_ops_typename (type), handle, sem_handle->val, releaseCount, sem_handle->max);
304
305                 sem_handle->val += releaseCount;
306                 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
307
308                 ret = TRUE;
309         }
310
311         thr_ret = mono_w32handle_unlock_handle (handle);
312         g_assert (thr_ret == 0);
313
314         return ret;
315 }
316
317 gpointer
318 ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
319 {
320         gpointer handle;
321         gchar *utf8_name;
322
323         *error = ERROR_SUCCESS;
324
325         /* w32 seems to guarantee that opening named objects can't race each other */
326         mono_w32handle_namespace_lock ();
327
328         utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
329
330         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named sem [%s]", __func__, utf8_name);
331
332         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDSEM, utf8_name);
333         if (handle == INVALID_HANDLE_VALUE) {
334                 /* The name has already been used for a different object. */
335                 *error = ERROR_INVALID_HANDLE;
336                 goto cleanup;
337         } else if (!handle) {
338                 /* This name doesn't exist */
339                 *error = ERROR_FILE_NOT_FOUND;
340                 goto cleanup;
341         }
342
343         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named sem handle %p", __func__, handle);
344
345 cleanup:
346         g_free (utf8_name);
347
348         mono_w32handle_namespace_unlock ();
349
350         return handle;
351 }
352
353 MonoW32HandleNamespace*
354 mono_w32semaphore_get_namespace (MonoW32HandleNamedSemaphore *semaphore)
355 {
356         return &semaphore->sharedns;
357 }