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