2 * w32semaphore-unix.c: Runtime support for managed Semaphore on Unix
5 * Ludovic Henry (luhenry@microsoft.com)
7 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10 #include "w32semaphore.h"
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"
22 } MonoW32HandleSemaphore;
24 struct MonoW32HandleNamedSemaphore {
25 MonoW32HandleSemaphore s;
26 MonoW32HandleNamespace sharedns;
29 static gboolean sem_handle_own (gpointer handle, MonoW32HandleType type, gboolean *abandoned)
31 MonoW32HandleSemaphore *sem_handle;
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);
41 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
42 __func__, mono_w32handle_get_typename (type), handle);
46 if (sem_handle->val == 0)
47 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
52 static void sema_signal(gpointer handle)
54 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal(handle, 1, NULL);
57 static gboolean sema_own (gpointer handle, gboolean *abandoned)
59 return sem_handle_own (handle, MONO_W32HANDLE_SEM, abandoned);
62 static void namedsema_signal (gpointer handle)
64 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (handle, 1, NULL);
67 /* NB, always called with the shared handle lock held */
68 static gboolean namedsema_own (gpointer handle, gboolean *abandoned)
70 return sem_handle_own (handle, MONO_W32HANDLE_NAMEDSEM, abandoned);
73 static void sema_details (gpointer data)
75 MonoW32HandleSemaphore *sem = (MonoW32HandleSemaphore *)data;
76 g_print ("val: %5u, max: %5d", sem->val, sem->max);
79 static void namedsema_details (gpointer data)
81 MonoW32HandleNamedSemaphore *namedsem = (MonoW32HandleNamedSemaphore *)data;
82 g_print ("val: %5u, max: %5d, name: \"%s\"", namedsem->s.val, namedsem->s.max, namedsem->sharedns.name);
85 static const gchar* sema_typename (void)
90 static gsize sema_typesize (void)
92 return sizeof (MonoW32HandleSemaphore);
95 static const gchar* namedsema_typename (void)
100 static gsize namedsema_typesize (void)
102 return sizeof (MonoW32HandleNamedSemaphore);
106 mono_w32semaphore_init (void)
108 static MonoW32HandleOps sem_ops = {
110 sema_signal, /* signal */
113 NULL, /* special_wait */
115 sema_details, /* details */
116 sema_typename, /* typename */
117 sema_typesize, /* typesize */
120 static MonoW32HandleOps namedsem_ops = {
122 namedsema_signal, /* signal */
123 namedsema_own, /* own */
125 NULL, /* special_wait */
127 namedsema_details, /* details */
128 namedsema_typename, /* typename */
129 namedsema_typesize, /* typesize */
132 mono_w32handle_register_ops (MONO_W32HANDLE_SEM, &sem_ops);
133 mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDSEM, &namedsem_ops);
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));
142 sem_handle_create (MonoW32HandleSemaphore *sem_handle, MonoW32HandleType type, gint32 initial, gint32 max)
146 sem_handle->val = initial;
147 sem_handle->max = max;
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);
157 mono_w32handle_lock_handle (handle);
160 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
162 mono_w32handle_unlock_handle (handle);
164 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
165 __func__, mono_w32handle_get_typename (type), handle);
171 sem_create (gint32 initial, gint32 max)
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);
180 namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
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);
188 /* w32 seems to guarantee that opening named objects can't race each other */
189 mono_w32handle_namespace_lock ();
191 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
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);
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. */
199 SetLastError (ERROR_INVALID_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);
204 /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
206 /* A new named semaphore */
207 MonoW32HandleNamedSemaphore namedsem_handle;
209 strncpy (&namedsem_handle.sharedns.name [0], utf8_name, MAX_PATH);
210 namedsem_handle.sharedns.name [MAX_PATH] = '\0';
212 handle = sem_handle_create ((MonoW32HandleSemaphore*) &namedsem_handle, MONO_W32HANDLE_NAMEDSEM, initial, max);
217 mono_w32handle_namespace_unlock ();
223 ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, gint32 *error)
227 if (maximumCount <= 0) {
228 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: maximumCount <= 0", __func__);
230 *error = ERROR_INVALID_PARAMETER;
234 if (initialCount > maximumCount || initialCount < 0) {
235 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: initialCount > maximumCount or < 0", __func__);
237 *error = ERROR_INVALID_PARAMETER;
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
245 SetLastError (ERROR_SUCCESS);
248 sem = sem_create (initialCount, maximumCount);
250 sem = namedsem_create (initialCount, maximumCount, mono_string_chars (name));
252 *error = GetLastError ();
258 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (gpointer handle, gint32 releaseCount, gint32 *prevcount)
260 MonoW32HandleType type;
261 MonoW32HandleSemaphore *sem_handle;
265 SetLastError (ERROR_INVALID_HANDLE);
269 switch (type = mono_w32handle_get_type (handle)) {
270 case MONO_W32HANDLE_SEM:
271 case MONO_W32HANDLE_NAMEDSEM:
274 SetLastError (ERROR_INVALID_HANDLE);
278 if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
279 g_warning ("%s: error looking up sem handle %p", __func__, handle);
283 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: releasing %s handle %p",
284 __func__, mono_w32handle_get_typename (type), handle);
286 mono_w32handle_lock_handle (handle);
288 /* Do this before checking for count overflow, because overflowing
289 * max is a listed technique for finding the current value */
291 *prevcount = sem_handle->val;
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);
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);
303 sem_handle->val += releaseCount;
304 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
309 mono_w32handle_unlock_handle (handle);
315 ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
320 *error = ERROR_SUCCESS;
322 /* w32 seems to guarantee that opening named objects can't race each other */
323 mono_w32handle_namespace_lock ();
325 utf8_name = g_utf16_to_utf8 (mono_string_chars (name), -1, NULL, NULL, NULL);
327 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Opening named sem [%s]", __func__, utf8_name);
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;
334 } else if (!handle) {
335 /* This name doesn't exist */
336 *error = ERROR_FILE_NOT_FOUND;
340 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: returning named sem handle %p", __func__, handle);
345 mono_w32handle_namespace_unlock ();
350 MonoW32HandleNamespace*
351 mono_w32semaphore_get_namespace (MonoW32HandleNamedSemaphore *semaphore)
353 return &semaphore->sharedns;