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/utils/w32handle.h"
20 } MonoW32HandleSemaphore;
22 struct MonoW32HandleNamedSemaphore {
23 MonoW32HandleSemaphore s;
24 MonoW32HandleNamespace sharedns;
27 static gboolean sem_handle_own (gpointer handle, MonoW32HandleType type)
29 MonoW32HandleSemaphore *sem_handle;
31 if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
32 g_warning ("%s: error looking up %s handle %p",
33 __func__, mono_w32handle_ops_typename (type), handle);
37 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
38 __func__, mono_w32handle_ops_typename (type), handle);
42 if (sem_handle->val == 0)
43 mono_w32handle_set_signal_state (handle, FALSE, FALSE);
48 static void sema_signal(gpointer handle)
50 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal(handle, 1, NULL);
53 static gboolean sema_own (gpointer handle)
55 return sem_handle_own (handle, MONO_W32HANDLE_SEM);
58 static void namedsema_signal (gpointer handle)
60 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (handle, 1, NULL);
63 /* NB, always called with the shared handle lock held */
64 static gboolean namedsema_own (gpointer handle)
66 return sem_handle_own (handle, MONO_W32HANDLE_NAMEDSEM);
69 static void sema_details (gpointer data)
71 MonoW32HandleSemaphore *sem = (MonoW32HandleSemaphore *)data;
72 g_print ("val: %5u, max: %5d", sem->val, sem->max);
75 static void namedsema_details (gpointer data)
77 MonoW32HandleNamedSemaphore *namedsem = (MonoW32HandleNamedSemaphore *)data;
78 g_print ("val: %5u, max: %5d, name: \"%s\"", namedsem->s.val, namedsem->s.max, namedsem->sharedns.name);
81 static const gchar* sema_typename (void)
86 static gsize sema_typesize (void)
88 return sizeof (MonoW32HandleSemaphore);
91 static const gchar* namedsema_typename (void)
96 static gsize namedsema_typesize (void)
98 return sizeof (MonoW32HandleNamedSemaphore);
102 mono_w32semaphore_init (void)
104 static MonoW32HandleOps sem_ops = {
106 sema_signal, /* signal */
109 NULL, /* special_wait */
111 sema_details, /* details */
112 sema_typename, /* typename */
113 sema_typesize, /* typesize */
116 static MonoW32HandleOps namedsem_ops = {
118 namedsema_signal, /* signal */
119 namedsema_own, /* own */
121 NULL, /* special_wait */
123 namedsema_details, /* details */
124 namedsema_typename, /* typename */
125 namedsema_typesize, /* typesize */
128 mono_w32handle_register_ops (MONO_W32HANDLE_SEM, &sem_ops);
129 mono_w32handle_register_ops (MONO_W32HANDLE_NAMEDSEM, &namedsem_ops);
131 mono_w32handle_register_capabilities (MONO_W32HANDLE_SEM,
132 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
133 mono_w32handle_register_capabilities (MONO_W32HANDLE_NAMEDSEM,
134 (MonoW32HandleCapability)(MONO_W32HANDLE_CAP_WAIT | MONO_W32HANDLE_CAP_SIGNAL));
138 sem_handle_create (MonoW32HandleSemaphore *sem_handle, MonoW32HandleType type, gint32 initial, gint32 max)
143 sem_handle->val = initial;
144 sem_handle->max = max;
146 handle = mono_w32handle_new (type, sem_handle);
147 if (handle == INVALID_HANDLE_VALUE) {
148 g_warning ("%s: error creating %s handle",
149 __func__, mono_w32handle_ops_typename (type));
150 SetLastError (ERROR_GEN_FAILURE);
154 thr_ret = mono_w32handle_lock_handle (handle);
155 g_assert (thr_ret == 0);
158 mono_w32handle_set_signal_state (handle, TRUE, FALSE);
160 thr_ret = mono_w32handle_unlock_handle (handle);
161 g_assert (thr_ret == 0);
163 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
164 __func__, mono_w32handle_ops_typename (type), handle);
170 sem_create (gint32 initial, gint32 max)
172 MonoW32HandleSemaphore sem_handle;
173 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle, initial %d max %d",
174 __func__, mono_w32handle_ops_typename (MONO_W32HANDLE_SEM), initial, max);
175 return sem_handle_create (&sem_handle, MONO_W32HANDLE_SEM, initial, max);
179 namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
184 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle, initial %d max %d name \"%s\"",
185 __func__, mono_w32handle_ops_typename (MONO_W32HANDLE_NAMEDSEM), initial, max, name);
187 /* w32 seems to guarantee that opening named objects can't race each other */
188 mono_w32handle_namespace_lock ();
190 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
192 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 handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDSEM, utf8_name);
195 if (handle == INVALID_HANDLE_VALUE) {
196 /* The name has already been used for a different object. */
198 SetLastError (ERROR_INVALID_HANDLE);
200 /* Not an error, but this is how the caller is informed that the semaphore wasn't freshly created */
201 SetLastError (ERROR_ALREADY_EXISTS);
203 /* this is used as creating a new handle */
204 mono_w32handle_ref (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;
266 SetLastError (ERROR_INVALID_HANDLE);
270 switch (type = mono_w32handle_get_type (handle)) {
271 case MONO_W32HANDLE_SEM:
272 case MONO_W32HANDLE_NAMEDSEM:
275 SetLastError (ERROR_INVALID_HANDLE);
279 if (!mono_w32handle_lookup (handle, type, (gpointer *)&sem_handle)) {
280 g_warning ("%s: error looking up sem handle %p", __func__, handle);
284 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: releasing %s handle %p",
285 __func__, mono_w32handle_ops_typename (type), handle);
287 thr_ret = mono_w32handle_lock_handle (handle);
288 g_assert (thr_ret == 0);
290 /* Do this before checking for count overflow, because overflowing
291 * max is a listed technique for finding the current value */
293 *prevcount = sem_handle->val;
295 /* No idea why max is signed, but thats the spec :-( */
296 if (sem_handle->val + releaseCount > (guint32)sem_handle->max) {
297 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",
298 __func__, mono_w32handle_ops_typename (type), handle, sem_handle->val, releaseCount, sem_handle->max);
302 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: %s handle %p val %d count %d max %d",
303 __func__, mono_w32handle_ops_typename (type), handle, sem_handle->val, releaseCount, sem_handle->max);
305 sem_handle->val += releaseCount;
306 mono_w32handle_set_signal_state (handle, TRUE, TRUE);
311 thr_ret = mono_w32handle_unlock_handle (handle);
312 g_assert (thr_ret == 0);
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;