2 * w32handle.c: Generic and internal operations on handles
5 * Dick Porter (dick@ximian.com)
6 * Ludovic Henry (luhenry@microsoft.com)
8 * (C) 2002-2011 Novell, Inc.
9 * Copyright 2011 Xamarin Inc
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "w32handle.h"
18 #include "utils/atomic.h"
19 #include "utils/mono-logger-internals.h"
20 #include "utils/mono-os-mutex.h"
21 #include "utils/mono-proclib.h"
22 #include "utils/mono-threads.h"
23 #include "utils/mono-time.h"
27 #define SLOT_MAX (1024 * 16)
29 /* must be a power of 2 */
30 #define HANDLE_PER_SLOT (256)
33 MonoW32HandleType type;
36 mono_mutex_t signal_mutex;
37 mono_cond_t signal_cond;
41 static MonoW32HandleCapability handle_caps [MONO_W32HANDLE_COUNT];
42 static MonoW32HandleOps *handle_ops [MONO_W32HANDLE_COUNT];
45 * We can hold SLOT_MAX * HANDLE_PER_SLOT handles.
46 * If 4M handles are not enough... Oh, well... we will crash.
48 #define SLOT_INDEX(x) (x / HANDLE_PER_SLOT)
49 #define SLOT_OFFSET(x) (x % HANDLE_PER_SLOT)
51 static MonoW32HandleBase *private_handles [SLOT_MAX];
52 static guint32 private_handles_count = 0;
53 static guint32 private_handles_slots_count = 0;
55 guint32 mono_w32handle_fd_reserve;
58 * This is an internal handle which is used for handling waiting for multiple handles.
59 * Threads which wait for multiple handles wait on this one handle, and when a handle
60 * is signalled, this handle is signalled too.
62 static mono_mutex_t global_signal_mutex;
63 static mono_cond_t global_signal_cond;
65 static mono_mutex_t scan_mutex;
67 static gboolean shutting_down = FALSE;
70 type_is_fd (MonoW32HandleType type)
73 case MONO_W32HANDLE_FILE:
74 case MONO_W32HANDLE_CONSOLE:
75 case MONO_W32HANDLE_SOCKET:
76 case MONO_W32HANDLE_PIPE:
84 mono_w32handle_lookup_data (gpointer handle, MonoW32HandleBase **handle_data)
88 g_assert (handle_data);
90 index = SLOT_INDEX ((gsize) handle);
91 if (index >= SLOT_MAX)
93 if (!private_handles [index])
96 offset = SLOT_OFFSET ((gsize) handle);
97 if (private_handles [index][offset].type == MONO_W32HANDLE_UNUSED)
100 *handle_data = &private_handles [index][offset];
105 mono_w32handle_get_type (gpointer handle)
107 MonoW32HandleBase *handle_data;
109 if (!mono_w32handle_lookup_data (handle, &handle_data))
110 return MONO_W32HANDLE_UNUSED; /* An impossible type */
112 return handle_data->type;
116 mono_w32handle_ops_typename (MonoW32HandleType type);
119 mono_w32handle_get_typename (MonoW32HandleType type)
121 return mono_w32handle_ops_typename (type);
125 mono_w32handle_set_signal_state (gpointer handle, gboolean state, gboolean broadcast)
127 MonoW32HandleBase *handle_data;
129 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
134 g_message ("%s: setting state of %p to %s (broadcast %s)", __func__,
135 handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
139 /* Tell everyone blocking on a single handle */
141 /* The condition the global signal cond is waiting on is the signalling of
142 * _any_ handle. So lock it before setting the signalled state.
144 mono_os_mutex_lock (&global_signal_mutex);
146 /* This function _must_ be called with
147 * handle->signal_mutex locked
149 handle_data->signalled=state;
151 if (broadcast == TRUE) {
152 mono_os_cond_broadcast (&handle_data->signal_cond);
154 mono_os_cond_signal (&handle_data->signal_cond);
157 /* Tell everyone blocking on multiple handles that something
160 mono_os_cond_broadcast (&global_signal_cond);
162 mono_os_mutex_unlock (&global_signal_mutex);
164 handle_data->signalled=state;
169 mono_w32handle_issignalled (gpointer handle)
171 MonoW32HandleBase *handle_data;
173 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
177 return handle_data->signalled;
181 mono_w32handle_lock_signal_mutex (void)
184 g_message ("%s: lock global signal mutex", __func__);
187 mono_os_mutex_lock (&global_signal_mutex);
191 mono_w32handle_unlock_signal_mutex (void)
194 g_message ("%s: unlock global signal mutex", __func__);
197 mono_os_mutex_unlock (&global_signal_mutex);
201 mono_w32handle_lock_handle (gpointer handle)
203 MonoW32HandleBase *handle_data;
205 if (!mono_w32handle_lookup_data (handle, &handle_data))
206 g_error ("%s: failed to lookup handle %p", __func__, handle);
208 mono_w32handle_ref (handle);
210 mono_os_mutex_lock (&handle_data->signal_mutex);
212 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: lock handle %p", __func__, handle);
216 mono_w32handle_trylock_handle (gpointer handle)
218 MonoW32HandleBase *handle_data;
221 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: trylock handle %p", __func__, handle);
223 if (!mono_w32handle_lookup_data (handle, &handle_data))
224 g_error ("%s: failed to lookup handle %p", __func__, handle);
226 mono_w32handle_ref (handle);
228 locked = mono_os_mutex_trylock (&handle_data->signal_mutex) == 0;
230 mono_w32handle_unref (handle);
232 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: trylock handle %p, locked: %s", __func__, handle, locked ? "true" : "false");
238 mono_w32handle_unlock_handle (gpointer handle)
240 MonoW32HandleBase *handle_data;
242 if (!mono_w32handle_lookup_data (handle, &handle_data))
243 g_error ("%s: failed to lookup handle %p", __func__, handle);
245 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: unlock handle %p", __func__, handle);
247 mono_os_mutex_unlock (&handle_data->signal_mutex);
249 mono_w32handle_unref (handle);
255 * Initialize the io-layer.
258 mono_w32handle_init (void)
260 static gboolean initialized = FALSE;
265 g_assert ((sizeof (handle_ops) / sizeof (handle_ops[0]))
266 == MONO_W32HANDLE_COUNT);
268 /* This is needed by the code in mono_w32handle_new_internal */
269 mono_w32handle_fd_reserve = (eg_getdtablesize () + (HANDLE_PER_SLOT - 1)) & ~(HANDLE_PER_SLOT - 1);
273 * The entries in private_handles reserved for fds are allocated lazily to
277 private_handles_count += HANDLE_PER_SLOT;
278 private_handles_slots_count ++;
279 } while(mono_w32handle_fd_reserve > private_handles_count);
281 mono_os_mutex_init (&scan_mutex);
283 mono_os_cond_init (&global_signal_cond);
284 mono_os_mutex_init (&global_signal_mutex);
290 mono_w32handle_cleanup (void)
294 g_assert (!shutting_down);
295 shutting_down = TRUE;
297 /* Every shared handle we were using ought really to be closed
298 * by now, but to make sure just blow them all away. The
299 * exiting finalizer thread in particular races us to the
300 * program exit and doesn't always win, so it can be left
301 * cluttering up the shared file. Anything else left over is
304 for(i = SLOT_INDEX (0); private_handles[i] != NULL; i++) {
305 for(j = SLOT_OFFSET (0); j < HANDLE_PER_SLOT; j++) {
306 MonoW32HandleBase *handle_data = &private_handles[i][j];
307 gpointer handle = GINT_TO_POINTER (i*HANDLE_PER_SLOT+j);
309 for(k = handle_data->ref; k > 0; k--) {
310 mono_w32handle_unref (handle);
315 for (i = 0; i < SLOT_MAX; ++i)
316 g_free (private_handles [i]);
320 mono_w32handle_ops_typesize (MonoW32HandleType type);
322 static void mono_w32handle_init_handle (MonoW32HandleBase *handle,
323 MonoW32HandleType type, gpointer handle_specific)
325 g_assert (handle->ref == 0);
328 handle->signalled = FALSE;
331 mono_os_cond_init (&handle->signal_cond);
332 mono_os_mutex_init (&handle->signal_mutex);
335 handle->specific = g_memdup (handle_specific, mono_w32handle_ops_typesize (type));
339 * mono_w32handle_new_internal:
340 * @type: Init handle to this type
342 * Search for a free handle and initialize it. Return the handle on
343 * success and 0 on failure. This is only called from
344 * mono_w32handle_new, and scan_mutex must be held.
346 static guint32 mono_w32handle_new_internal (MonoW32HandleType type,
347 gpointer handle_specific)
350 static guint32 last = 0;
351 gboolean retry = FALSE;
353 /* A linear scan should be fast enough. Start from the last
354 * allocation, assuming that handles are allocated more often
355 * than they're freed. Leave the space reserved for file
359 if (last < mono_w32handle_fd_reserve) {
360 last = mono_w32handle_fd_reserve;
367 for(i = SLOT_INDEX (count); i < private_handles_slots_count; i++) {
368 if (private_handles [i]) {
369 for (k = SLOT_OFFSET (count); k < HANDLE_PER_SLOT; k++) {
370 MonoW32HandleBase *handle = &private_handles [i][k];
372 if(handle->type == MONO_W32HANDLE_UNUSED) {
375 mono_w32handle_init_handle (handle, type, handle_specific);
383 if(retry && last > mono_w32handle_fd_reserve) {
384 /* Try again from the beginning */
385 last = mono_w32handle_fd_reserve;
389 /* Will need to expand the array. The caller will sort it out */
395 mono_w32handle_new (MonoW32HandleType type, gpointer handle_specific)
397 guint32 handle_idx = 0;
400 g_assert (!shutting_down);
402 g_assert(!type_is_fd(type));
404 mono_os_mutex_lock (&scan_mutex);
406 while ((handle_idx = mono_w32handle_new_internal (type, handle_specific)) == 0) {
407 /* Try and expand the array, and have another go */
408 int idx = SLOT_INDEX (private_handles_count);
409 if (idx >= SLOT_MAX) {
413 private_handles [idx] = g_new0 (MonoW32HandleBase, HANDLE_PER_SLOT);
415 private_handles_count += HANDLE_PER_SLOT;
416 private_handles_slots_count ++;
419 mono_os_mutex_unlock (&scan_mutex);
421 if (handle_idx == 0) {
422 /* We ran out of slots */
423 handle = INVALID_HANDLE_VALUE;
424 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to create %s handle", __func__, mono_w32handle_ops_typename (type));
428 /* Make sure we left the space for fd mappings */
429 g_assert (handle_idx >= mono_w32handle_fd_reserve);
431 handle = GUINT_TO_POINTER (handle_idx);
433 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: create %s handle %p", __func__, mono_w32handle_ops_typename (type), handle);
439 gpointer mono_w32handle_new_fd (MonoW32HandleType type, int fd,
440 gpointer handle_specific)
442 MonoW32HandleBase *handle_data;
443 int fd_index, fd_offset;
445 g_assert (!shutting_down);
447 g_assert(type_is_fd(type));
449 if (fd >= mono_w32handle_fd_reserve) {
450 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to create %s handle, fd is too big", __func__, mono_w32handle_ops_typename (type));
452 return(GUINT_TO_POINTER (INVALID_HANDLE_VALUE));
455 fd_index = SLOT_INDEX (fd);
456 fd_offset = SLOT_OFFSET (fd);
458 /* Initialize the array entries on demand */
459 if (!private_handles [fd_index]) {
460 mono_os_mutex_lock (&scan_mutex);
462 if (!private_handles [fd_index])
463 private_handles [fd_index] = g_new0 (MonoW32HandleBase, HANDLE_PER_SLOT);
465 mono_os_mutex_unlock (&scan_mutex);
468 handle_data = &private_handles [fd_index][fd_offset];
470 if (handle_data->type != MONO_W32HANDLE_UNUSED) {
471 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to create %s handle, fd is already in use", __func__, mono_w32handle_ops_typename (type));
472 /* FIXME: clean up this handle? We can't do anything
473 * with the fd, cos thats the new one
475 return(GUINT_TO_POINTER (INVALID_HANDLE_VALUE));
478 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: create %s handle %p", __func__, mono_w32handle_ops_typename (type), GUINT_TO_POINTER(fd));
480 mono_w32handle_init_handle (handle_data, type, handle_specific);
482 return(GUINT_TO_POINTER(fd));
486 mono_w32handle_lookup (gpointer handle, MonoW32HandleType type,
487 gpointer *handle_specific)
489 MonoW32HandleBase *handle_data;
491 g_assert (handle_specific);
493 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
497 if (handle_data->type != type) {
501 *handle_specific = handle_data->specific;
507 mono_w32handle_ref_core (gpointer handle, MonoW32HandleBase *handle_data);
510 mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data, guint minimum);
513 mono_w32handle_foreach (gboolean (*on_each)(gpointer handle, gpointer data, gpointer user_data), gpointer user_data)
517 mono_os_mutex_lock (&scan_mutex);
519 for (i = SLOT_INDEX (0); i < private_handles_slots_count; i++) {
520 if (!private_handles [i])
522 for (k = SLOT_OFFSET (0); k < HANDLE_PER_SLOT; k++) {
523 MonoW32HandleBase *handle_data = NULL;
525 gboolean destroy, finished;
527 handle_data = &private_handles [i][k];
528 if (handle_data->type == MONO_W32HANDLE_UNUSED)
531 handle = GUINT_TO_POINTER (i * HANDLE_PER_SLOT + k);
533 if (!mono_w32handle_ref_core (handle, handle_data)) {
534 /* we are racing with mono_w32handle_unref:
535 * the handle ref has been decremented, but it
536 * hasn't yet been destroyed. */
540 finished = on_each (handle, handle_data->specific, user_data);
542 /* we do not want to have to destroy the handle here,
543 * as it would means the ref/unref are unbalanced */
544 destroy = mono_w32handle_unref_core (handle, handle_data, 2);
553 mono_os_mutex_unlock (&scan_mutex);
557 mono_w32handle_ref_core (gpointer handle, MonoW32HandleBase *handle_data)
562 old = handle_data->ref;
567 } while (InterlockedCompareExchange ((gint32*) &handle_data->ref, new, old) != old);
569 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: ref %s handle %p, ref: %d -> %d",
570 __func__, mono_w32handle_ops_typename (handle_data->type), handle, old, new);
576 mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data, guint minimum)
578 MonoW32HandleType type;
581 type = handle_data->type;
584 old = handle_data->ref;
585 if (!(old >= minimum))
586 g_error ("%s: handle %p has ref %d, it should be >= %d", __func__, handle, old, minimum);
589 } while (InterlockedCompareExchange ((gint32*) &handle_data->ref, new, old) != old);
591 /* handle_data might contain invalid data from now on, if
592 * another thread is unref'ing this handle at the same time */
594 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: unref %s handle %p, ref: %d -> %d destroy: %s",
595 __func__, mono_w32handle_ops_typename (type), handle, old, new, new == 0 ? "true" : "false");
600 void mono_w32handle_ref (gpointer handle)
602 MonoW32HandleBase *handle_data;
604 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
605 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to ref handle %p, unknown handle", __func__, handle);
609 if (!mono_w32handle_ref_core (handle, handle_data))
610 g_error ("%s: failed to ref handle %p", __func__, handle);
613 static void (*_wapi_handle_ops_get_close_func (MonoW32HandleType type))(gpointer, gpointer);
615 /* The handle must not be locked on entry to this function */
617 mono_w32handle_unref (gpointer handle)
619 MonoW32HandleBase *handle_data;
622 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
623 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to unref handle %p, unknown handle",
628 destroy = mono_w32handle_unref_core (handle, handle_data, 1);
631 /* Need to copy the handle info, reset the slot in the
632 * array, and _only then_ call the close function to
633 * avoid race conditions (eg file descriptors being
634 * closed, and another file being opened getting the
635 * same fd racing the memset())
637 MonoW32HandleType type;
638 gpointer handle_specific;
639 void (*close_func)(gpointer, gpointer);
641 type = handle_data->type;
642 handle_specific = handle_data->specific;
644 mono_os_mutex_lock (&scan_mutex);
646 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: destroy %s handle %p", __func__, mono_w32handle_ops_typename (type), handle);
648 mono_os_mutex_destroy (&handle_data->signal_mutex);
649 mono_os_cond_destroy (&handle_data->signal_cond);
651 memset (handle_data, 0, sizeof (MonoW32HandleBase));
653 mono_os_mutex_unlock (&scan_mutex);
655 close_func = _wapi_handle_ops_get_close_func (type);
656 if (close_func != NULL) {
657 close_func (handle, handle_specific);
660 g_free (handle_specific);
665 mono_w32handle_ops_close (gpointer handle, gpointer data);
668 mono_w32handle_force_close (gpointer handle, gpointer data)
670 mono_w32handle_ops_close (handle, data);
674 mono_w32handle_register_ops (MonoW32HandleType type, MonoW32HandleOps *ops)
676 handle_ops [type] = ops;
679 void mono_w32handle_register_capabilities (MonoW32HandleType type,
680 MonoW32HandleCapability caps)
682 handle_caps[type] = caps;
685 gboolean mono_w32handle_test_capabilities (gpointer handle,
686 MonoW32HandleCapability caps)
688 MonoW32HandleBase *handle_data;
689 MonoW32HandleType type;
691 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
695 type = handle_data->type;
697 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: testing 0x%x against 0x%x (%d)", __func__,
698 handle_caps[type], caps, handle_caps[type] & caps);
700 return((handle_caps[type] & caps) != 0);
703 static void (*_wapi_handle_ops_get_close_func (MonoW32HandleType type))(gpointer, gpointer)
705 if (handle_ops[type] != NULL &&
706 handle_ops[type]->close != NULL) {
707 return (handle_ops[type]->close);
714 mono_w32handle_ops_close (gpointer handle, gpointer data)
716 MonoW32HandleBase *handle_data;
717 MonoW32HandleType type;
719 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
723 type = handle_data->type;
725 if (handle_ops[type] != NULL &&
726 handle_ops[type]->close != NULL) {
727 handle_ops[type]->close (handle, data);
732 mono_w32handle_ops_details (MonoW32HandleType type, gpointer data)
734 if (handle_ops[type] != NULL &&
735 handle_ops[type]->details != NULL) {
736 handle_ops[type]->details (data);
741 mono_w32handle_ops_typename (MonoW32HandleType type)
743 g_assert (handle_ops [type]);
744 g_assert (handle_ops [type]->typename);
745 return handle_ops [type]->typename ();
749 mono_w32handle_ops_typesize (MonoW32HandleType type)
751 g_assert (handle_ops [type]);
752 g_assert (handle_ops [type]->typesize);
753 return handle_ops [type]->typesize ();
757 mono_w32handle_ops_signal (gpointer handle)
759 MonoW32HandleBase *handle_data;
760 MonoW32HandleType type;
762 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
766 type = handle_data->type;
768 if (handle_ops[type] != NULL && handle_ops[type]->signal != NULL) {
769 handle_ops[type]->signal (handle);
774 mono_w32handle_ops_own (gpointer handle, guint32 *statuscode)
776 MonoW32HandleBase *handle_data;
777 MonoW32HandleType type;
779 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
783 type = handle_data->type;
785 if (handle_ops[type] != NULL && handle_ops[type]->own_handle != NULL) {
786 return(handle_ops[type]->own_handle (handle, statuscode));
793 mono_w32handle_ops_isowned (gpointer handle)
795 MonoW32HandleBase *handle_data;
796 MonoW32HandleType type;
798 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
802 type = handle_data->type;
804 if (handle_ops[type] != NULL && handle_ops[type]->is_owned != NULL) {
805 return(handle_ops[type]->is_owned (handle));
811 static MonoW32HandleWaitRet
812 mono_w32handle_ops_specialwait (gpointer handle, guint32 timeout, gboolean *alerted)
814 MonoW32HandleBase *handle_data;
815 MonoW32HandleType type;
817 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
821 type = handle_data->type;
823 if (handle_ops[type] != NULL &&
824 handle_ops[type]->special_wait != NULL) {
825 return(handle_ops[type]->special_wait (handle, timeout, alerted));
832 mono_w32handle_ops_prewait (gpointer handle)
834 MonoW32HandleBase *handle_data;
835 MonoW32HandleType type;
837 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
841 type = handle_data->type;
843 if (handle_ops[type] != NULL &&
844 handle_ops[type]->prewait != NULL) {
845 handle_ops[type]->prewait (handle);
855 struct timespec sleepytime;
857 g_assert (ms < 1000);
859 sleepytime.tv_sec = 0;
860 sleepytime.tv_nsec = ms * 1000000;
861 nanosleep (&sleepytime, NULL);
862 #endif /* HOST_WIN32 */
866 mono_w32handle_lock_handles (gpointer *handles, gsize numhandles)
870 /* Lock all the handles, with backoff */
872 for(i=0; i<numhandles; i++) {
873 gpointer handle = handles[i];
875 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: attempting to lock %p", __func__, handle);
877 if (!mono_w32handle_trylock_handle (handle)) {
880 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: attempt failed for %p.", __func__,
886 mono_w32handle_unlock_handle (handle);
889 /* If iter ever reaches 100 the nanosleep will
890 * return EINVAL immediately, but we have a
891 * design flaw if that happens.
895 g_warning ("%s: iteration overflow!",
900 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: Backing off for %d ms", __func__,
908 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: Locked all handles", __func__);
912 mono_w32handle_unlock_handles (gpointer *handles, gsize numhandles)
916 for(i=0; i<numhandles; i++) {
917 gpointer handle = handles[i];
919 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: unlocking handle %p", __func__, handle);
921 mono_w32handle_unlock_handle (handle);
926 mono_w32handle_timedwait_signal_naked (mono_cond_t *cond, mono_mutex_t *mutex, guint32 timeout, gboolean poll, gboolean *alerted)
931 res = mono_os_cond_timedwait (cond, mutex, timeout);
933 /* This is needed when waiting for process handles */
936 * pthread_cond_(timed)wait() can return 0 even if the condition was not
937 * signalled. This happens at least on Darwin. We surface this, i.e., we
938 * get spurious wake-ups.
940 * http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html
942 res = mono_os_cond_timedwait (cond, mutex, timeout);
945 /* Real timeout is less than 100ms time */
946 res = mono_os_cond_timedwait (cond, mutex, timeout);
948 res = mono_os_cond_timedwait (cond, mutex, 100);
950 /* Mask the fake timeout, this will cause
951 * another poll if the cond was not really signaled
963 signal_global (gpointer unused)
965 /* If we reach here, then interrupt token is set to the flag value, which
966 * means that the target thread is either
967 * - before the first CAS in timedwait, which means it won't enter the wait.
968 * - it is after the first CAS, so it is already waiting, or it will enter
969 * the wait, and it will be interrupted by the broadcast. */
970 mono_os_mutex_lock (&global_signal_mutex);
971 mono_os_cond_broadcast (&global_signal_cond);
972 mono_os_mutex_unlock (&global_signal_mutex);
976 mono_w32handle_timedwait_signal (guint32 timeout, gboolean poll, gboolean *alerted)
980 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: waiting for global", __func__);
986 mono_thread_info_install_interrupt (signal_global, NULL, alerted);
991 res = mono_w32handle_timedwait_signal_naked (&global_signal_cond, &global_signal_mutex, timeout, poll, alerted);
994 mono_thread_info_uninstall_interrupt (alerted);
1000 signal_handle_and_unref (gpointer handle)
1002 MonoW32HandleBase *handle_data;
1004 mono_mutex_t *mutex;
1006 if (!mono_w32handle_lookup_data (handle, &handle_data))
1007 g_error ("cannot signal unknown handle %p", handle);
1009 /* If we reach here, then interrupt token is set to the flag value, which
1010 * means that the target thread is either
1011 * - before the first CAS in timedwait, which means it won't enter the wait.
1012 * - it is after the first CAS, so it is already waiting, or it will enter
1013 * the wait, and it will be interrupted by the broadcast. */
1014 cond = &handle_data->signal_cond;
1015 mutex = &handle_data->signal_mutex;
1017 mono_os_mutex_lock (mutex);
1018 mono_os_cond_broadcast (cond);
1019 mono_os_mutex_unlock (mutex);
1021 mono_w32handle_unref (handle);
1025 mono_w32handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboolean poll, gboolean *alerted)
1027 MonoW32HandleBase *handle_data;
1030 if (!mono_w32handle_lookup_data (handle, &handle_data))
1031 g_error ("cannot wait on unknown handle %p", handle);
1033 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: waiting for %p (type %s)", __func__, handle,
1034 mono_w32handle_ops_typename (mono_w32handle_get_type (handle)));
1040 mono_thread_info_install_interrupt (signal_handle_and_unref, handle, alerted);
1043 mono_w32handle_ref (handle);
1046 res = mono_w32handle_timedwait_signal_naked (&handle_data->signal_cond, &handle_data->signal_mutex, timeout, poll, alerted);
1049 mono_thread_info_uninstall_interrupt (alerted);
1051 /* if it is alerted, then the handle is unref in the interrupt callback */
1052 mono_w32handle_unref (handle);
1060 dump_callback (gpointer handle, gpointer handle_specific, gpointer user_data)
1062 MonoW32HandleBase *handle_data;
1064 if (!mono_w32handle_lookup_data (handle, &handle_data))
1065 g_error ("cannot dump unknown handle %p", handle);
1067 g_print ("%p [%7s] signalled: %5s ref: %3d ",
1068 handle, mono_w32handle_ops_typename (handle_data->type), handle_data->signalled ? "true" : "false", handle_data->ref);
1069 mono_w32handle_ops_details (handle_data->type, handle_data->specific);
1075 void mono_w32handle_dump (void)
1077 mono_w32handle_foreach (dump_callback, NULL);
1081 own_if_signalled (gpointer handle, guint32 *statuscode)
1083 if (!mono_w32handle_issignalled (handle))
1086 *statuscode = WAIT_OBJECT_0;
1087 mono_w32handle_ops_own (handle, statuscode);
1092 own_if_owned( gpointer handle, guint32 *statuscode)
1094 if (!mono_w32handle_ops_isowned (handle))
1097 *statuscode = WAIT_OBJECT_0;
1098 mono_w32handle_ops_own (handle, statuscode);
1102 MonoW32HandleWaitRet
1103 mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable)
1105 MonoW32HandleWaitRet ret;
1108 guint32 statuscode = 0;
1112 if (mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_SPECIAL_WAIT)) {
1113 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p has special wait",
1116 return mono_w32handle_ops_specialwait (handle, timeout, alertable ? &alerted : NULL);
1119 if (!mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_WAIT)) {
1120 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p can't be waited for",
1123 return MONO_W32HANDLE_WAIT_RET_FAILED;
1126 mono_w32handle_lock_handle (handle);
1128 if (mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_OWN)) {
1129 if (own_if_owned (handle, &statuscode)) {
1130 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p already owned",
1133 ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1138 if (timeout != MONO_INFINITE_WAIT)
1139 start = mono_msec_ticks ();
1144 if (own_if_signalled (handle, &statuscode)) {
1145 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p signalled",
1148 ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1152 mono_w32handle_ops_prewait (handle);
1154 if (timeout == MONO_INFINITE_WAIT) {
1155 waited = mono_w32handle_timedwait_signal_handle (handle, MONO_INFINITE_WAIT, FALSE, alertable ? &alerted : NULL);
1159 elapsed = mono_msec_ticks () - start;
1160 if (elapsed > timeout) {
1161 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1165 waited = mono_w32handle_timedwait_signal_handle (handle, timeout - elapsed, FALSE, alertable ? &alerted : NULL);
1169 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1174 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1180 mono_w32handle_unlock_handle (handle);
1185 MonoW32HandleWaitRet
1186 mono_w32handle_wait_multiple (gpointer *handles, gsize nhandles, gboolean waitall, guint32 timeout, gboolean alertable)
1188 MonoW32HandleWaitRet ret;
1189 gboolean alerted, poll;
1192 gpointer handles_sorted [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS];
1193 guint32 statuscodes [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS] = {0};
1196 return MONO_W32HANDLE_WAIT_RET_FAILED;
1199 return mono_w32handle_wait_one (handles [0], timeout, alertable);
1203 if (nhandles > MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS) {
1204 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: too many handles: %zd",
1205 __func__, nhandles);
1207 return MONO_W32HANDLE_WAIT_RET_FAILED;
1210 for (i = 0; i < nhandles; ++i) {
1211 if (!mono_w32handle_test_capabilities (handles[i], MONO_W32HANDLE_CAP_WAIT)
1212 && !mono_w32handle_test_capabilities (handles[i], MONO_W32HANDLE_CAP_SPECIAL_WAIT))
1214 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p can't be waited for",
1215 __func__, handles [i]);
1217 return MONO_W32HANDLE_WAIT_RET_FAILED;
1220 handles_sorted [i] = handles [i];
1223 qsort (handles_sorted, nhandles, sizeof (gpointer), g_direct_equal);
1224 for (i = 1; i < nhandles; ++i) {
1225 if (handles_sorted [i - 1] == handles_sorted [i]) {
1226 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p is duplicated",
1227 __func__, handles_sorted [i]);
1229 return MONO_W32HANDLE_WAIT_RET_FAILED;
1234 for (i = 0; i < nhandles; ++i) {
1235 if (mono_w32handle_get_type (handles [i]) == MONO_W32HANDLE_PROCESS) {
1236 /* Can't wait for a process handle + another handle without polling */
1241 if (timeout != MONO_INFINITE_WAIT)
1242 start = mono_msec_ticks ();
1244 for (i = 0; i < nhandles; ++i) {
1245 /* Add a reference, as we need to ensure the handle wont
1246 * disappear from under us while we're waiting in the loop
1247 * (not lock, as we don't want exclusive access here) */
1248 mono_w32handle_ref (handles [i]);
1252 gsize count, lowest;
1259 mono_w32handle_lock_handles (handles, nhandles);
1261 for (i = 0; i < nhandles; i++) {
1262 if ((mono_w32handle_test_capabilities (handles [i], MONO_W32HANDLE_CAP_OWN) && mono_w32handle_ops_isowned (handles [i]))
1263 || mono_w32handle_issignalled (handles [i]))
1272 signalled = (waitall && count == nhandles) || (!waitall && count > 0);
1275 for (i = 0; i < nhandles; i++)
1276 own_if_signalled (handles [i], &statuscodes [i]);
1279 mono_w32handle_unlock_handles (handles, nhandles);
1282 ret = MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + lowest;
1283 for (i = lowest; i < nhandles; i++) {
1284 if (statuscodes [i] == WAIT_ABANDONED_0) {
1285 ret = MONO_W32HANDLE_WAIT_RET_ABANDONED_0 + lowest;
1292 for (i = 0; i < nhandles; i++) {
1293 mono_w32handle_ops_prewait (handles[i]);
1295 if (mono_w32handle_test_capabilities (handles [i], MONO_W32HANDLE_CAP_SPECIAL_WAIT)
1296 && !mono_w32handle_issignalled (handles [i]))
1298 mono_w32handle_ops_specialwait (handles [i], 0, alertable ? &alerted : NULL);
1302 mono_w32handle_lock_signal_mutex ();
1306 for (i = 0; i < nhandles; ++i) {
1307 if (!mono_w32handle_issignalled (handles [i])) {
1314 for (i = 0; i < nhandles; ++i) {
1315 if (mono_w32handle_issignalled (handles [i])) {
1325 if (timeout == MONO_INFINITE_WAIT) {
1326 waited = mono_w32handle_timedwait_signal (MONO_INFINITE_WAIT, poll, alertable ? &alerted : NULL);
1330 elapsed = mono_msec_ticks () - start;
1331 if (elapsed > timeout) {
1332 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1334 mono_w32handle_unlock_signal_mutex ();
1339 waited = mono_w32handle_timedwait_signal (timeout - elapsed, poll, alertable ? &alerted : NULL);
1343 mono_w32handle_unlock_signal_mutex ();
1346 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1351 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1357 for (i = 0; i < nhandles; i++) {
1358 /* Unref everything we reffed above */
1359 mono_w32handle_unref (handles [i]);
1365 MonoW32HandleWaitRet
1366 mono_w32handle_signal_and_wait (gpointer signal_handle, gpointer wait_handle, guint32 timeout, gboolean alertable)
1368 MonoW32HandleWaitRet ret;
1371 guint32 statuscode = 0;
1372 gpointer handles [2];
1376 if (!mono_w32handle_test_capabilities (signal_handle, MONO_W32HANDLE_CAP_SIGNAL))
1377 return MONO_W32HANDLE_WAIT_RET_FAILED;
1378 if (!mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_WAIT))
1379 return MONO_W32HANDLE_WAIT_RET_FAILED;
1381 if (mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_SPECIAL_WAIT)) {
1382 g_warning ("%s: handle %p has special wait, implement me!!", __func__, wait_handle);
1383 return MONO_W32HANDLE_WAIT_RET_FAILED;
1386 handles [0] = wait_handle;
1387 handles [1] = signal_handle;
1389 mono_w32handle_lock_handles (handles, 2);
1391 mono_w32handle_ops_signal (signal_handle);
1393 mono_w32handle_unlock_handle (signal_handle);
1395 if (mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_OWN)) {
1396 if (own_if_owned (wait_handle, &statuscode)) {
1397 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p already owned",
1398 __func__, wait_handle);
1400 ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1405 if (timeout != MONO_INFINITE_WAIT)
1406 start = mono_msec_ticks ();
1411 if (own_if_signalled (wait_handle, &statuscode)) {
1412 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p signalled",
1413 __func__, wait_handle);
1415 ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1419 mono_w32handle_ops_prewait (wait_handle);
1421 if (timeout == MONO_INFINITE_WAIT) {
1422 waited = mono_w32handle_timedwait_signal_handle (wait_handle, MONO_INFINITE_WAIT, FALSE, alertable ? &alerted : NULL);
1426 elapsed = mono_msec_ticks () - start;
1427 if (elapsed > timeout) {
1428 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1432 waited = mono_w32handle_timedwait_signal_handle (wait_handle, timeout - elapsed, FALSE, alertable ? &alerted : NULL);
1436 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1441 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1447 mono_w32handle_unlock_handle (wait_handle);