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 gboolean sem_handle_own (gpointer handle, MonoW32HandleType type, gboolean *abandoned)
32 MonoW32HandleSemaphore *sem_handle;
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);
42 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
43 __func__, mono_w32handle_get_typename (type), handle);
47 if (sem_handle->val == 0)
48 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
53 static void sema_signal(gpointer handle)
55 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal(handle, 1, NULL);
58 static gboolean sema_own (gpointer handle, gboolean *abandoned)
60 return sem_handle_own (handle, MONO_W32HANDLE_SEM, abandoned);
63 static void namedsema_signal (gpointer handle)
65 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (handle, 1, NULL);
68 /* NB, always called with the shared handle lock held */
69 static gboolean namedsema_own (gpointer handle, gboolean *abandoned)
71 return sem_handle_own (handle, MONO_W32HANDLE_NAMEDSEM, abandoned);
74 static void sema_details (gpointer data)
76 MonoW32HandleSemaphore *sem = (MonoW32HandleSemaphore *)data;
77 g_print ("val: %5u, max: %5d", sem->val, sem->max);
80 static void namedsema_details (gpointer data)
82 MonoW32HandleNamedSemaphore *namedsem = (MonoW32HandleNamedSemaphore *)data;
83 g_print ("val: %5u, max: %5d, name: \"%s\"", namedsem->s.val, namedsem->s.max, namedsem->sharedns.name);
86 static const gchar* sema_typename (void)
91 static gsize sema_typesize (void)
93 return sizeof (MonoW32HandleSemaphore);
96 static const gchar* namedsema_typename (void)
101 static gsize namedsema_typesize (void)
103 return sizeof (MonoW32HandleNamedSemaphore);
107 mono_w32semaphore_init (void)
109 static MonoW32HandleOps sem_ops = {
111 sema_signal, /* signal */
114 NULL, /* special_wait */
116 sema_details, /* details */
117 sema_typename, /* typename */
118 sema_typesize, /* typesize */
121 static MonoW32HandleOps namedsem_ops = {
123 namedsema_signal, /* signal */
124 namedsema_own, /* own */
126 NULL, /* special_wait */
128 namedsema_details, /* details */
129 namedsema_typename, /* typename */
130 namedsema_typesize, /* typesize */
133 mono_w32handle_register_ops (MONO_W32HANDLE_SEM, &sem_ops);
134 mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDSEM, &namedsem_ops);
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));
143 sem_handle_create (MonoW32HandleSemaphore *sem_handle, MonoW32HandleType type, gint32 initial, gint32 max)
147 sem_handle->val = initial;
148 sem_handle->max = max;
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);
158 mono_w32handle_lock_handle (handle);
161 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
163 mono_w32handle_unlock_handle (handle);
165 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
166 __func__, mono_w32handle_get_typename (type), handle);
172 sem_create (gint32 initial, gint32 max)
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);
181 namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
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);
189 /* w32 seems to guarantee that opening named objects can't race each other */
190 mono_w32handle_namespace_lock ();
193 utf8_name = g_utf16_to_utf8 (name, -1, NULL, &utf8_len, NULL);
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);
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. */
201 mono_w32error_set_last (ERROR_INVALID_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);
206 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
208 /* A new named semaphore */
209 MonoW32HandleNamedSemaphore namedsem_handle;
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';
215 handle = sem_handle_create ((MonoW32HandleSemaphore*) &namedsem_handle, MONO_W32HANDLE_NAMEDSEM, initial, max);
220 mono_w32handle_namespace_unlock ();
226 ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, gint32 *error)
230 if (maximumCount <= 0) {
231 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: maximumCount <= 0", __func__);
233 *error = ERROR_INVALID_PARAMETER;
237 if (initialCount > maximumCount || initialCount < 0) {
238 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: initialCount > maximumCount or < 0", __func__);
240 *error = ERROR_INVALID_PARAMETER;
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
248 mono_w32error_set_last (ERROR_SUCCESS);
251 sem = sem_create (initialCount, maximumCount);
253 sem = namedsem_create (initialCount, maximumCount, mono_string_chars (name));
255 *error = mono_w32error_get_last ();
261 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (gpointer handle, gint32 releaseCount, gint32 *prevcount)
263 MonoW32HandleType type;
264 MonoW32HandleSemaphore *sem_handle;
268 mono_w32error_set_last (ERROR_INVALID_HANDLE);
272 switch (type = mono_w32handle_get_type (handle)) {
273 case MONO_W32HANDLE_SEM:
274 case MONO_W32HANDLE_NAMEDSEM:
277 mono_w32error_set_last (ERROR_INVALID_HANDLE);
281 if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
282 g_warning ("%s: error looking up sem handle %p", __func__, handle);
286 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: releasing %s handle %p",
287 __func__, mono_w32handle_get_typename (type), handle);
289 mono_w32handle_lock_handle (handle);
291 /* Do this before checking for count overflow, because overflowing
292 * max is a listed technique for finding the current value */
294 *prevcount = sem_handle->val;
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);
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);
306 sem_handle->val += releaseCount;
307 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
312 mono_w32handle_unlock_handle (handle);
318 ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
323 *error = ERROR_SUCCESS;
325 /* w32 seems to guarantee that opening named objects can't race each other */
326 mono_w32handle_namespace_lock ();
328 utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
330 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named sem [%s]", __func__, utf8_name);
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;
337 } else if (!handle) {
338 /* This name doesn't exist */
339 *error = ERROR_FILE_NOT_FOUND;
343 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named sem handle %p", __func__, handle);
348 mono_w32handle_namespace_unlock ();
353 MonoW32HandleNamespace*
354 mono_w32semaphore_get_namespace (MonoW32HandleNamedSemaphore *semaphore)
356 return &semaphore->sharedns;