[io-layer] Extract file (#4255)
[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 #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                 SetLastError (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         utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
192
193         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);
194
195         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDSEM, utf8_name);
196         if (handle == INVALID_HANDLE_VALUE) {
197                 /* The name has already been used for a different object. */
198                 handle = NULL;
199                 SetLastError (ERROR_INVALID_HANDLE);
200         } else if (handle) {
201                 /* Not an error, but this is how the caller is informed that the semaphore wasn't freshly created */
202                 SetLastError (ERROR_ALREADY_EXISTS);
203
204                 /* mono_w32handle_namespace_search_handle already adds a ref to the 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         MonoBoolean ret;
263
264         if (!handle) {
265                 SetLastError (ERROR_INVALID_HANDLE);
266                 return FALSE;
267         }
268
269         switch (type = mono_w32handle_get_type (handle)) {
270         case MONO_W32HANDLE_SEM:
271         case MONO_W32HANDLE_NAMEDSEM:
272                 break;
273         default:
274                 SetLastError (ERROR_INVALID_HANDLE);
275                 return FALSE;
276         }
277
278         if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
279                 g_warning ("%s: error looking up sem handle %p", __func__, handle);
280                 return FALSE;
281         }
282
283         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: releasing %s handle %p",
284                 __func__, mono_w32handle_get_typename (type), handle);
285
286         mono_w32handle_lock_handle (handle);
287
288         /* Do this before checking for count overflow, because overflowing
289          * max is a listed technique for finding the current value */
290         if (prevcount)
291                 *prevcount = sem_handle->val;
292
293         /* No idea why max is signed, but thats the spec :-( */
294         if (sem_handle->val + releaseCount > (guint32)sem_handle->max) {
295                 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",
296                         __func__, mono_w32handle_get_typename (type), handle, sem_handle->val, releaseCount, sem_handle->max);
297
298                 ret = FALSE;
299         } else {
300                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s handle %p val %d count %d max %d",
301                         __func__, mono_w32handle_get_typename (type), handle, sem_handle->val, releaseCount, sem_handle->max);
302
303                 sem_handle->val += releaseCount;
304                 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
305
306                 ret = TRUE;
307         }
308
309         mono_w32handle_unlock_handle (handle);
310
311         return ret;
312 }
313
314 gpointer
315 ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
316 {
317         gpointer handle;
318         gchar *utf8_name;
319
320         *error = ERROR_SUCCESS;
321
322         /* w32 seems to guarantee that opening named objects can't race each other */
323         mono_w32handle_namespace_lock ();
324
325         utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
326
327         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named sem [%s]", __func__, utf8_name);
328
329         handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDSEM, utf8_name);
330         if (handle == INVALID_HANDLE_VALUE) {
331                 /* The name has already been used for a different object. */
332                 *error = ERROR_INVALID_HANDLE;
333                 goto cleanup;
334         } else if (!handle) {
335                 /* This name doesn't exist */
336                 *error = ERROR_FILE_NOT_FOUND;
337                 goto cleanup;
338         }
339
340         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named sem handle %p", __func__, handle);
341
342 cleanup:
343         g_free (utf8_name);
344
345         mono_w32handle_namespace_unlock ();
346
347         return handle;
348 }
349
350 MonoW32HandleNamespace*
351 mono_w32semaphore_get_namespace (MonoW32HandleNamedSemaphore *semaphore)
352 {
353         return &semaphore->sharedns;
354 }