3 * Runtime support for managed Semaphore on Unix
6 * Ludovic Henry (luhenry@microsoft.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #include "w32semaphore.h"
14 #include "w32handle-namespace.h"
15 #include "mono/utils/mono-logger-internals.h"
16 #include "mono/metadata/w32handle.h"
23 } MonoW32HandleSemaphore;
25 struct MonoW32HandleNamedSemaphore {
26 MonoW32HandleSemaphore s;
27 MonoW32HandleNamespace sharedns;
30 static void sem_handle_signal (gpointer handle, MonoW32HandleType type, MonoW32HandleSemaphore *sem_handle)
32 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: signalling %s handle %p",
33 __func__, mono_w32handle_get_typename (type), handle);
35 /* No idea why max is signed, but thats the spec :-( */
36 if (sem_handle->val + 1 > (guint32)sem_handle->max) {
37 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",
38 __func__, mono_w32handle_get_typename (type), handle, sem_handle->val, 1, sem_handle->max);
40 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s handle %p val %d count %d max %d",
41 __func__, mono_w32handle_get_typename (type), handle, sem_handle->val, 1, sem_handle->max);
44 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
48 static gboolean sem_handle_own (gpointer handle, MonoW32HandleType type, gboolean *abandoned)
50 MonoW32HandleSemaphore *sem_handle;
54 if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
55 g_warning ("%s: error looking up %s handle %p",
56 __func__, mono_w32handle_get_typename (type), handle);
60 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
61 __func__, mono_w32handle_get_typename (type), handle);
65 if (sem_handle->val == 0)
66 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
71 static void sema_signal(gpointer handle, gpointer handle_specific)
73 sem_handle_signal (handle, MONO_W32HANDLE_SEM, (MonoW32HandleSemaphore*) handle_specific);
76 static gboolean sema_own (gpointer handle, gboolean *abandoned)
78 return sem_handle_own (handle, MONO_W32HANDLE_SEM, abandoned);
81 static void namedsema_signal (gpointer handle, gpointer handle_specific)
83 sem_handle_signal (handle, MONO_W32HANDLE_NAMEDSEM, (MonoW32HandleSemaphore*) handle_specific);
86 /* NB, always called with the shared handle lock held */
87 static gboolean namedsema_own (gpointer handle, gboolean *abandoned)
89 return sem_handle_own (handle, MONO_W32HANDLE_NAMEDSEM, abandoned);
92 static void sema_details (gpointer data)
94 MonoW32HandleSemaphore *sem = (MonoW32HandleSemaphore *)data;
95 g_print ("val: %5u, max: %5d", sem->val, sem->max);
98 static void namedsema_details (gpointer data)
100 MonoW32HandleNamedSemaphore *namedsem = (MonoW32HandleNamedSemaphore *)data;
101 g_print ("val: %5u, max: %5d, name: \"%s\"", namedsem->s.val, namedsem->s.max, namedsem->sharedns.name);
104 static const gchar* sema_typename (void)
109 static gsize sema_typesize (void)
111 return sizeof (MonoW32HandleSemaphore);
114 static const gchar* namedsema_typename (void)
116 return "N.Semaphore";
119 static gsize namedsema_typesize (void)
121 return sizeof (MonoW32HandleNamedSemaphore);
125 mono_w32semaphore_init (void)
127 static MonoW32HandleOps sem_ops = {
129 sema_signal, /* signal */
132 NULL, /* special_wait */
134 sema_details, /* details */
135 sema_typename, /* typename */
136 sema_typesize, /* typesize */
139 static MonoW32HandleOps namedsem_ops = {
141 namedsema_signal, /* signal */
142 namedsema_own, /* own */
144 NULL, /* special_wait */
146 namedsema_details, /* details */
147 namedsema_typename, /* typename */
148 namedsema_typesize, /* typesize */
151 mono_w32handle_register_ops (MONO_W32HANDLE_SEM, &sem_ops);
152 mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDSEM, &namedsem_ops);
154 mono_w32handle_register_capabilities (MONO_W32HANDLE_SEM,
155 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
156 mono_w32handle_register_capabilities (MONO_W32HANDLE_NAMEDSEM,
157 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
161 sem_handle_create (MonoW32HandleSemaphore *sem_handle, MonoW32HandleType type, gint32 initial, gint32 max)
165 sem_handle->val = initial;
166 sem_handle->max = max;
168 handle = mono_w32handle_new (type, sem_handle);
169 if (handle == INVALID_HANDLE_VALUE) {
170 g_warning ("%s: error creating %s handle",
171 __func__, mono_w32handle_get_typename (type));
172 mono_w32error_set_last (ERROR_GEN_FAILURE);
176 mono_w32handle_lock_handle (handle);
179 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
181 mono_w32handle_unlock_handle (handle);
183 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
184 __func__, mono_w32handle_get_typename (type), handle);
190 sem_create (gint32 initial, gint32 max)
192 MonoW32HandleSemaphore sem_handle;
193 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle, initial %d max %d",
194 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_SEM), initial, max);
195 return sem_handle_create (&sem_handle, MONO_W32HANDLE_SEM, initial, max);
199 namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
204 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle, initial %d max %d name \"%s\"",
205 __func__, mono_w32handle_get_typename (MONO_W32HANDLE_NAMEDSEM), initial, max, (const char*)name);
207 /* w32 seems to guarantee that opening named objects can't race each other */
208 mono_w32handle_namespace_lock ();
211 utf8_name = g_utf16_to_utf8 (name, -1, NULL, &utf8_len, NULL);
213 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);
215 handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDSEM, utf8_name);
216 if (handle == INVALID_HANDLE_VALUE) {
217 /* The name has already been used for a different object. */
219 mono_w32error_set_last (ERROR_INVALID_HANDLE);
221 /* Not an error, but this is how the caller is informed that the semaphore wasn't freshly created */
222 mono_w32error_set_last (ERROR_ALREADY_EXISTS);
224 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
226 /* A new named semaphore */
227 MonoW32HandleNamedSemaphore namedsem_handle;
229 size_t len = utf8_len < MAX_PATH ? utf8_len : MAX_PATH;
230 memcpy (&namedsem_handle.sharedns.name [0], utf8_name, len);
231 namedsem_handle.sharedns.name [len] = '\0';
233 handle = sem_handle_create ((MonoW32HandleSemaphore*) &namedsem_handle, MONO_W32HANDLE_NAMEDSEM, initial, max);
238 mono_w32handle_namespace_unlock ();
244 ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, gint32 *error)
248 if (maximumCount <= 0) {
249 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: maximumCount <= 0", __func__);
251 *error = ERROR_INVALID_PARAMETER;
255 if (initialCount > maximumCount || initialCount < 0) {
256 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: initialCount > maximumCount or < 0", __func__);
258 *error = ERROR_INVALID_PARAMETER;
262 /* Need to blow away any old errors here, because code tests
263 * for ERROR_ALREADY_EXISTS on success (!) to see if a
264 * semaphore was freshly created
266 mono_w32error_set_last (ERROR_SUCCESS);
269 sem = sem_create (initialCount, maximumCount);
271 sem = namedsem_create (initialCount, maximumCount, mono_string_chars (name));
273 *error = mono_w32error_get_last ();
279 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (gpointer handle, gint32 releaseCount, gint32 *prevcount)
281 MonoW32HandleType type;
282 MonoW32HandleSemaphore *sem_handle;
286 mono_w32error_set_last (ERROR_INVALID_HANDLE);
290 switch (type = mono_w32handle_get_type (handle)) {
291 case MONO_W32HANDLE_SEM:
292 case MONO_W32HANDLE_NAMEDSEM:
295 mono_w32error_set_last (ERROR_INVALID_HANDLE);
299 if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
300 g_warning ("%s: error looking up sem handle %p", __func__, handle);
304 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: releasing %s handle %p",
305 __func__, mono_w32handle_get_typename (type), handle);
307 mono_w32handle_lock_handle (handle);
309 /* Do this before checking for count overflow, because overflowing
310 * max is a listed technique for finding the current value */
312 *prevcount = sem_handle->val;
314 /* No idea why max is signed, but thats the spec :-( */
315 if (sem_handle->val + releaseCount > (guint32)sem_handle->max) {
316 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",
317 __func__, mono_w32handle_get_typename (type), handle, sem_handle->val, releaseCount, sem_handle->max);
321 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s handle %p val %d count %d max %d",
322 __func__, mono_w32handle_get_typename (type), handle, sem_handle->val, releaseCount, sem_handle->max);
324 sem_handle->val += releaseCount;
325 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
330 mono_w32handle_unlock_handle (handle);
336 ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
341 *error = ERROR_SUCCESS;
343 /* w32 seems to guarantee that opening named objects can't race each other */
344 mono_w32handle_namespace_lock ();
346 utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
348 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named sem [%s]", __func__, utf8_name);
350 handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDSEM, utf8_name);
351 if (handle == INVALID_HANDLE_VALUE) {
352 /* The name has already been used for a different object. */
353 *error = ERROR_INVALID_HANDLE;
355 } else if (!handle) {
356 /* This name doesn't exist */
357 *error = ERROR_FILE_NOT_FOUND;
361 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named sem handle %p", __func__, handle);
366 mono_w32handle_namespace_unlock ();
371 MonoW32HandleNamespace*
372 mono_w32semaphore_get_namespace (MonoW32HandleNamedSemaphore *semaphore)
374 return &semaphore->sharedns;