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