2 * semaphores.c: Semaphore handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
13 #ifdef HAVE_SEMAPHORE_H
14 #include <semaphore.h>
20 #include <mono/io-layer/wapi.h>
21 #include <mono/io-layer/wapi-private.h>
22 #include <mono/io-layer/misc-private.h>
23 #include <mono/io-layer/handles-private.h>
24 #include <mono/io-layer/semaphore-private.h>
25 #include <mono/utils/mono-once.h>
28 #define DEBUG(...) g_message(__VA_ARGS__)
33 static void sema_signal(gpointer handle);
34 static gboolean sema_own (gpointer handle);
36 static void namedsema_signal (gpointer handle);
37 static gboolean namedsema_own (gpointer handle);
39 struct _WapiHandleOps _wapi_sem_ops = {
41 sema_signal, /* signal */
44 NULL, /* special_wait */
48 void _wapi_sem_details (gpointer handle_info)
50 struct _WapiHandle_sem *sem = (struct _WapiHandle_sem *)handle_info;
52 g_print ("val: %5u, max: %5d", sem->val, sem->max);
55 struct _WapiHandleOps _wapi_namedsem_ops = {
57 namedsema_signal, /* signal */
58 namedsema_own, /* own */
60 NULL, /* special_wait */
64 static gboolean sem_release (gpointer handle, gint32 count, gint32 *prev);
65 static gboolean namedsem_release (gpointer handle, gint32 count, gint32 *prev);
69 gboolean (*release)(gpointer handle, gint32 count, gint32 *prev);
70 } sem_ops[WAPI_HANDLE_COUNT] = {
86 static mono_once_t sem_ops_once=MONO_ONCE_INIT;
88 static void sem_ops_init (void)
90 _wapi_handle_register_capabilities (WAPI_HANDLE_SEM,
91 WAPI_HANDLE_CAP_WAIT |
92 WAPI_HANDLE_CAP_SIGNAL);
93 _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDSEM,
94 WAPI_HANDLE_CAP_WAIT |
95 WAPI_HANDLE_CAP_SIGNAL);
98 static void sema_signal(gpointer handle)
100 ReleaseSemaphore(handle, 1, NULL);
103 static gboolean sema_own (gpointer handle)
105 struct _WapiHandle_sem *sem_handle;
108 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_SEM,
109 (gpointer *)&sem_handle);
111 g_warning ("%s: error looking up sem handle %p", __func__,
116 DEBUG("%s: owning sem handle %p", __func__, handle);
120 DEBUG ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
122 if(sem_handle->val==0) {
123 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
129 static void namedsema_signal (gpointer handle)
131 ReleaseSemaphore (handle, 1, NULL);
134 /* NB, always called with the shared handle lock held */
135 static gboolean namedsema_own (gpointer handle)
137 struct _WapiHandle_namedsem *namedsem_handle;
140 DEBUG ("%s: owning named sem handle %p", __func__, handle);
142 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDSEM,
143 (gpointer *)&namedsem_handle);
145 g_warning ("%s: error looking up named sem handle %p",
150 namedsem_handle->val--;
152 DEBUG ("%s: named sem %p val now %d", __func__, handle,
153 namedsem_handle->val);
155 if (namedsem_handle->val == 0) {
156 _wapi_shared_handle_set_signal_state (handle, FALSE);
161 static gpointer sem_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
162 gint32 initial, gint32 max)
164 struct _WapiHandle_sem sem_handle = {0};
168 /* Need to blow away any old errors here, because code tests
169 * for ERROR_ALREADY_EXISTS on success (!) to see if a
170 * semaphore was freshly created
172 SetLastError (ERROR_SUCCESS);
174 sem_handle.val = initial;
175 sem_handle.max = max;
177 handle = _wapi_handle_new (WAPI_HANDLE_SEM, &sem_handle);
178 if (handle == _WAPI_HANDLE_INVALID) {
179 g_warning ("%s: error creating semaphore handle", __func__);
180 SetLastError (ERROR_GEN_FAILURE);
184 thr_ret = _wapi_handle_lock_handle (handle);
185 g_assert (thr_ret == 0);
188 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
191 DEBUG ("%s: Created semaphore handle %p initial %d max %d",
192 __func__, handle, initial, max);
194 thr_ret = _wapi_handle_unlock_handle (handle);
195 g_assert (thr_ret == 0);
200 static gpointer namedsem_create (WapiSecurityAttributes *security G_GNUC_UNUSED, gint32 initial, gint32 max, const gunichar2 *name G_GNUC_UNUSED)
202 struct _WapiHandle_namedsem namedsem_handle = {{{0}}, 0};
210 /* w32 seems to guarantee that opening named objects can't
213 thr_ret = _wapi_namespace_lock ();
214 g_assert (thr_ret == 0);
216 /* Need to blow away any old errors here, because code tests
217 * for ERROR_ALREADY_EXISTS on success (!) to see if a
218 * semaphore was freshly created
220 SetLastError (ERROR_SUCCESS);
222 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
224 DEBUG ("%s: Creating named sem [%s]", __func__, utf8_name);
226 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDSEM,
229 /* The name has already been used for a different
232 SetLastError (ERROR_INVALID_HANDLE);
234 } else if (offset != 0) {
235 /* Not an error, but this is how the caller is
236 * informed that the semaphore wasn't freshly created
238 SetLastError (ERROR_ALREADY_EXISTS);
240 /* Fall through to create the semaphore handle */
243 /* A new named semaphore, so create both the private
246 if (strlen (utf8_name) < MAX_PATH) {
247 namelen = strlen (utf8_name);
252 memcpy (&namedsem_handle.sharedns.name, utf8_name, namelen);
254 namedsem_handle.val = initial;
255 namedsem_handle.max = max;
257 handle = _wapi_handle_new (WAPI_HANDLE_NAMEDSEM,
260 /* A new reference to an existing named semaphore, so
261 * just create the private part
263 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDSEM,
267 if (handle == _WAPI_HANDLE_INVALID) {
268 g_warning ("%s: error creating named sem handle", __func__);
269 SetLastError (ERROR_GEN_FAILURE);
275 /* Set the initial state, as this is a completely new
278 thr_ret = _wapi_handle_lock_shared_handles ();
279 g_assert (thr_ret == 0);
282 _wapi_shared_handle_set_signal_state (handle, TRUE);
285 _wapi_handle_unlock_shared_handles ();
288 DEBUG ("%s: returning named sem handle %p", __func__, handle);
293 _wapi_namespace_unlock (NULL);
301 * @security: Ignored for now.
302 * @initial: The initial count for the semaphore. The value must be
303 * greater than or equal to zero, and less than or equal to @max.
304 * @max: The maximum count for this semaphore. The value must be
306 * @name: Pointer to a string specifying the name of this semaphore,
307 * or %NULL. Currently ignored.
309 * Creates a new semaphore handle. A semaphore is signalled when its
310 * count is greater than zero, and unsignalled otherwise. The count
311 * is decreased by one whenever a wait function releases a thread that
312 * was waiting for the semaphore. The count is increased by calling
313 * ReleaseSemaphore().
315 * Return value: a new handle, or NULL
317 gpointer CreateSemaphore(WapiSecurityAttributes *security G_GNUC_UNUSED, gint32 initial, gint32 max, const gunichar2 *name)
319 mono_once (&sem_ops_once, sem_ops_init);
322 DEBUG ("%s: max <= 0", __func__);
324 SetLastError (ERROR_INVALID_PARAMETER);
328 if (initial > max || initial < 0) {
329 DEBUG ("%s: initial>max or < 0", __func__);
331 SetLastError (ERROR_INVALID_PARAMETER);
336 return (sem_create (security, initial, max));
338 return (namedsem_create (security, initial, max, name));
342 static gboolean sem_release (gpointer handle, gint32 count, gint32 *prevcount)
344 struct _WapiHandle_sem *sem_handle;
349 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SEM,
350 (gpointer *)&sem_handle);
352 g_warning ("%s: error looking up sem handle %p", __func__,
357 thr_ret = _wapi_handle_lock_handle (handle);
358 g_assert (thr_ret == 0);
360 DEBUG ("%s: sem %p val %d count %d", __func__, handle,
361 sem_handle->val, count);
363 /* Do this before checking for count overflow, because overflowing max
364 * is a listed technique for finding the current value
366 if (prevcount != NULL) {
367 *prevcount = sem_handle->val;
370 /* No idea why max is signed, but thats the spec :-( */
371 if (sem_handle->val + count > (guint32)sem_handle->max) {
372 DEBUG ("%s: sem %p max value would be exceeded: max %d current %d count %d", __func__, handle, sem_handle->max, sem_handle->val, count);
377 sem_handle->val += count;
378 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
382 DEBUG ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
385 thr_ret = _wapi_handle_unlock_handle (handle);
386 g_assert (thr_ret == 0);
391 static gboolean namedsem_release (gpointer handle, gint32 count,
394 struct _WapiHandle_namedsem *sem_handle;
399 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDSEM,
400 (gpointer *)&sem_handle);
402 g_warning ("%s: error looking up sem handle %p", __func__,
407 thr_ret = _wapi_handle_lock_shared_handles ();
408 g_assert (thr_ret == 0);
410 DEBUG("%s: named sem %p val %d count %d", __func__, handle,
411 sem_handle->val, count);
413 /* Do this before checking for count overflow, because overflowing max
414 * is a listed technique for finding the current value
416 if (prevcount != NULL) {
417 *prevcount = sem_handle->val;
420 /* No idea why max is signed, but thats the spec :-( */
421 if (sem_handle->val + count > (guint32)sem_handle->max) {
422 DEBUG ("%s: named sem %p max value would be exceeded: max %d current %d count %d", __func__, handle, sem_handle->max, sem_handle->val, count);
427 sem_handle->val += count;
428 _wapi_shared_handle_set_signal_state (handle, TRUE);
432 DEBUG("%s: named sem %p val now %d", __func__, handle,
436 _wapi_handle_unlock_shared_handles ();
443 * @handle: The semaphore handle to release.
444 * @count: The amount by which the semaphore's count should be
446 * @prevcount: Pointer to a location to store the previous count of
447 * the semaphore, or %NULL.
449 * Increases the count of semaphore @handle by @count.
451 * Return value: %TRUE on success, %FALSE otherwise.
453 gboolean ReleaseSemaphore(gpointer handle, gint32 count, gint32 *prevcount)
457 if (handle == NULL) {
458 SetLastError (ERROR_INVALID_HANDLE);
462 type = _wapi_handle_type (handle);
464 if (sem_ops[type].release == NULL) {
465 SetLastError (ERROR_INVALID_HANDLE);
469 return (sem_ops[type].release (handle, count, prevcount));
472 gpointer OpenSemaphore (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED,
473 const gunichar2 *name)
481 mono_once (&sem_ops_once, sem_ops_init);
483 /* w32 seems to guarantee that opening named objects can't
486 thr_ret = _wapi_namespace_lock ();
487 g_assert (thr_ret == 0);
489 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
491 DEBUG ("%s: Opening named sem [%s]", __func__, utf8_name);
493 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDSEM,
496 /* The name has already been used for a different
499 SetLastError (ERROR_INVALID_HANDLE);
501 } else if (offset == 0) {
502 /* This name doesn't exist */
503 SetLastError (ERROR_FILE_NOT_FOUND); /* yes, really */
507 /* A new reference to an existing named semaphore, so just
508 * create the private part
510 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDSEM, offset,
513 if (handle == _WAPI_HANDLE_INVALID) {
514 g_warning ("%s: error opening named sem handle", __func__);
515 SetLastError (ERROR_GEN_FAILURE);
520 DEBUG ("%s: returning named sem handle %p", __func__, handle);
525 _wapi_namespace_unlock (NULL);