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