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/handles-private.h>
23 #include <mono/io-layer/semaphore-private.h>
24 #include <mono/utils/mono-once.h>
27 #define DEBUG(...) g_message(__VA_ARGS__)
32 static void sema_signal(gpointer handle);
33 static gboolean sema_own (gpointer handle);
35 static void namedsema_signal (gpointer handle);
36 static gboolean namedsema_own (gpointer handle);
38 struct _WapiHandleOps _wapi_sem_ops = {
40 sema_signal, /* signal */
43 NULL, /* special_wait */
47 void _wapi_sem_details (gpointer handle_info)
49 struct _WapiHandle_sem *sem = (struct _WapiHandle_sem *)handle_info;
51 g_print ("val: %5u, max: %5d", sem->val, sem->max);
54 struct _WapiHandleOps _wapi_namedsem_ops = {
56 namedsema_signal, /* signal */
57 namedsema_own, /* own */
59 NULL, /* special_wait */
63 static gboolean sem_release (gpointer handle, gint32 count, gint32 *prev);
64 static gboolean namedsem_release (gpointer handle, gint32 count, gint32 *prev);
68 gboolean (*release)(gpointer handle, gint32 count, gint32 *prev);
69 } sem_ops[WAPI_HANDLE_COUNT] = {
85 static mono_once_t sem_ops_once=MONO_ONCE_INIT;
87 static void sem_ops_init (void)
89 _wapi_handle_register_capabilities (WAPI_HANDLE_SEM,
90 WAPI_HANDLE_CAP_WAIT |
91 WAPI_HANDLE_CAP_SIGNAL);
92 _wapi_handle_register_capabilities (WAPI_HANDLE_NAMEDSEM,
93 WAPI_HANDLE_CAP_WAIT |
94 WAPI_HANDLE_CAP_SIGNAL);
97 static void sema_signal(gpointer handle)
99 ReleaseSemaphore(handle, 1, NULL);
102 static gboolean sema_own (gpointer handle)
104 struct _WapiHandle_sem *sem_handle;
107 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_SEM,
108 (gpointer *)&sem_handle);
110 g_warning ("%s: error looking up sem handle %p", __func__,
115 DEBUG("%s: owning sem handle %p", __func__, handle);
119 DEBUG ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
121 if(sem_handle->val==0) {
122 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
128 static void namedsema_signal (gpointer handle)
130 ReleaseSemaphore (handle, 1, NULL);
133 /* NB, always called with the shared handle lock held */
134 static gboolean namedsema_own (gpointer handle)
136 struct _WapiHandle_namedsem *namedsem_handle;
139 DEBUG ("%s: owning named sem handle %p", __func__, handle);
141 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDSEM,
142 (gpointer *)&namedsem_handle);
144 g_warning ("%s: error looking up named sem handle %p",
149 namedsem_handle->val--;
151 DEBUG ("%s: named sem %p val now %d", __func__, handle,
152 namedsem_handle->val);
154 if (namedsem_handle->val == 0) {
155 _wapi_shared_handle_set_signal_state (handle, FALSE);
160 static gpointer sem_create (WapiSecurityAttributes *security G_GNUC_UNUSED,
161 gint32 initial, gint32 max)
163 struct _WapiHandle_sem sem_handle = {0};
167 /* Need to blow away any old errors here, because code tests
168 * for ERROR_ALREADY_EXISTS on success (!) to see if a
169 * semaphore was freshly created
171 SetLastError (ERROR_SUCCESS);
173 sem_handle.val = initial;
174 sem_handle.max = max;
176 handle = _wapi_handle_new (WAPI_HANDLE_SEM, &sem_handle);
177 if (handle == _WAPI_HANDLE_INVALID) {
178 g_warning ("%s: error creating semaphore handle", __func__);
179 SetLastError (ERROR_GEN_FAILURE);
183 thr_ret = _wapi_handle_lock_handle (handle);
184 g_assert (thr_ret == 0);
187 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
190 DEBUG ("%s: Created semaphore handle %p initial %d max %d",
191 __func__, handle, initial, max);
193 thr_ret = _wapi_handle_unlock_handle (handle);
194 g_assert (thr_ret == 0);
199 static gpointer namedsem_create (WapiSecurityAttributes *security G_GNUC_UNUSED, gint32 initial, gint32 max, const gunichar2 *name G_GNUC_UNUSED)
201 struct _WapiHandle_namedsem namedsem_handle = {{{0}}, 0};
209 /* w32 seems to guarantee that opening named objects can't
212 thr_ret = _wapi_namespace_lock ();
213 g_assert (thr_ret == 0);
215 /* Need to blow away any old errors here, because code tests
216 * for ERROR_ALREADY_EXISTS on success (!) to see if a
217 * semaphore was freshly created
219 SetLastError (ERROR_SUCCESS);
221 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
223 DEBUG ("%s: Creating named sem [%s]", __func__, utf8_name);
225 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDSEM,
228 /* The name has already been used for a different
231 SetLastError (ERROR_INVALID_HANDLE);
233 } else if (offset != 0) {
234 /* Not an error, but this is how the caller is
235 * informed that the semaphore wasn't freshly created
237 SetLastError (ERROR_ALREADY_EXISTS);
239 /* Fall through to create the semaphore handle */
242 /* A new named semaphore, so create both the private
245 if (strlen (utf8_name) < MAX_PATH) {
246 namelen = strlen (utf8_name);
251 memcpy (&namedsem_handle.sharedns.name, utf8_name, namelen);
253 namedsem_handle.val = initial;
254 namedsem_handle.max = max;
256 handle = _wapi_handle_new (WAPI_HANDLE_NAMEDSEM,
259 /* A new reference to an existing named semaphore, so
260 * just create the private part
262 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDSEM,
266 if (handle == _WAPI_HANDLE_INVALID) {
267 g_warning ("%s: error creating named sem handle", __func__);
268 SetLastError (ERROR_GEN_FAILURE);
274 /* Set the initial state, as this is a completely new
277 thr_ret = _wapi_handle_lock_shared_handles ();
278 g_assert (thr_ret == 0);
281 _wapi_shared_handle_set_signal_state (handle, TRUE);
284 _wapi_handle_unlock_shared_handles ();
287 DEBUG ("%s: returning named sem handle %p", __func__, handle);
292 _wapi_namespace_unlock (NULL);
300 * @security: Ignored for now.
301 * @initial: The initial count for the semaphore. The value must be
302 * greater than or equal to zero, and less than or equal to @max.
303 * @max: The maximum count for this semaphore. The value must be
305 * @name: Pointer to a string specifying the name of this semaphore,
306 * or %NULL. Currently ignored.
308 * Creates a new semaphore handle. A semaphore is signalled when its
309 * count is greater than zero, and unsignalled otherwise. The count
310 * is decreased by one whenever a wait function releases a thread that
311 * was waiting for the semaphore. The count is increased by calling
312 * ReleaseSemaphore().
314 * Return value: a new handle, or NULL
316 gpointer CreateSemaphore(WapiSecurityAttributes *security G_GNUC_UNUSED, gint32 initial, gint32 max, const gunichar2 *name)
318 mono_once (&sem_ops_once, sem_ops_init);
321 DEBUG ("%s: max <= 0", __func__);
323 SetLastError (ERROR_INVALID_PARAMETER);
327 if (initial > max || initial < 0) {
328 DEBUG ("%s: initial>max or < 0", __func__);
330 SetLastError (ERROR_INVALID_PARAMETER);
335 return (sem_create (security, initial, max));
337 return (namedsem_create (security, initial, max, name));
341 static gboolean sem_release (gpointer handle, gint32 count, gint32 *prevcount)
343 struct _WapiHandle_sem *sem_handle;
348 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_SEM,
349 (gpointer *)&sem_handle);
351 g_warning ("%s: error looking up sem handle %p", __func__,
356 thr_ret = _wapi_handle_lock_handle (handle);
357 g_assert (thr_ret == 0);
359 DEBUG ("%s: sem %p val %d count %d", __func__, handle,
360 sem_handle->val, count);
362 /* Do this before checking for count overflow, because overflowing max
363 * is a listed technique for finding the current value
365 if (prevcount != NULL) {
366 *prevcount = sem_handle->val;
369 /* No idea why max is signed, but thats the spec :-( */
370 if (sem_handle->val + count > (guint32)sem_handle->max) {
371 DEBUG ("%s: sem %p max value would be exceeded: max %d current %d count %d", __func__, handle, sem_handle->max, sem_handle->val, count);
376 sem_handle->val += count;
377 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
381 DEBUG ("%s: sem %p val now %d", __func__, handle, sem_handle->val);
384 thr_ret = _wapi_handle_unlock_handle (handle);
385 g_assert (thr_ret == 0);
390 static gboolean namedsem_release (gpointer handle, gint32 count,
393 struct _WapiHandle_namedsem *sem_handle;
398 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_NAMEDSEM,
399 (gpointer *)&sem_handle);
401 g_warning ("%s: error looking up sem handle %p", __func__,
406 thr_ret = _wapi_handle_lock_shared_handles ();
407 g_assert (thr_ret == 0);
409 DEBUG("%s: named sem %p val %d count %d", __func__, handle,
410 sem_handle->val, count);
412 /* Do this before checking for count overflow, because overflowing max
413 * is a listed technique for finding the current value
415 if (prevcount != NULL) {
416 *prevcount = sem_handle->val;
419 /* No idea why max is signed, but thats the spec :-( */
420 if (sem_handle->val + count > (guint32)sem_handle->max) {
421 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);
426 sem_handle->val += count;
427 _wapi_shared_handle_set_signal_state (handle, TRUE);
431 DEBUG("%s: named sem %p val now %d", __func__, handle,
435 _wapi_handle_unlock_shared_handles ();
442 * @handle: The semaphore handle to release.
443 * @count: The amount by which the semaphore's count should be
445 * @prevcount: Pointer to a location to store the previous count of
446 * the semaphore, or %NULL.
448 * Increases the count of semaphore @handle by @count.
450 * Return value: %TRUE on success, %FALSE otherwise.
452 gboolean ReleaseSemaphore(gpointer handle, gint32 count, gint32 *prevcount)
456 if (handle == NULL) {
457 SetLastError (ERROR_INVALID_HANDLE);
461 type = _wapi_handle_type (handle);
463 if (sem_ops[type].release == NULL) {
464 SetLastError (ERROR_INVALID_HANDLE);
468 return (sem_ops[type].release (handle, count, prevcount));
471 gpointer OpenSemaphore (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED,
472 const gunichar2 *name)
480 mono_once (&sem_ops_once, sem_ops_init);
482 /* w32 seems to guarantee that opening named objects can't
485 thr_ret = _wapi_namespace_lock ();
486 g_assert (thr_ret == 0);
488 utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
490 DEBUG ("%s: Opening named sem [%s]", __func__, utf8_name);
492 offset = _wapi_search_handle_namespace (WAPI_HANDLE_NAMEDSEM,
495 /* The name has already been used for a different
498 SetLastError (ERROR_INVALID_HANDLE);
500 } else if (offset == 0) {
501 /* This name doesn't exist */
502 SetLastError (ERROR_FILE_NOT_FOUND); /* yes, really */
506 /* A new reference to an existing named semaphore, so just
507 * create the private part
509 handle = _wapi_handle_new_from_offset (WAPI_HANDLE_NAMEDSEM, offset,
512 if (handle == _WAPI_HANDLE_INVALID) {
513 g_warning ("%s: error opening named sem handle", __func__);
514 SetLastError (ERROR_GEN_FAILURE);
519 DEBUG ("%s: returning named sem handle %p", __func__, handle);
524 _wapi_namespace_unlock (NULL);