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.
15 #if !defined(HOST_WIN32)
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 # include <sys/socket.h>
32 #ifdef HAVE_SYS_MMAN_H
33 # include <sys/mman.h>
39 #ifdef HAVE_SYS_RESOURCE_H
40 # include <sys/resource.h>
43 #include "w32handle.h"
45 #include "utils/atomic.h"
46 #include "utils/mono-logger-internals.h"
47 #include "utils/mono-os-mutex.h"
48 #include "utils/mono-proclib.h"
49 #include "utils/mono-threads.h"
50 #include "utils/mono-time.h"
54 #define SLOT_MAX (1024 * 16)
56 /* must be a power of 2 */
57 #define HANDLE_PER_SLOT (256)
59 #define INFINITE 0xFFFFFFFF
62 MonoW32HandleType type;
65 mono_mutex_t signal_mutex;
66 mono_cond_t signal_cond;
70 static MonoW32HandleCapability handle_caps [MONO_W32HANDLE_COUNT];
71 static MonoW32HandleOps *handle_ops [MONO_W32HANDLE_COUNT];
74 * We can hold SLOT_MAX * HANDLE_PER_SLOT handles.
75 * If 4M handles are not enough... Oh, well... we will crash.
77 #define SLOT_INDEX(x) (x / HANDLE_PER_SLOT)
78 #define SLOT_OFFSET(x) (x % HANDLE_PER_SLOT)
80 static MonoW32HandleBase *private_handles [SLOT_MAX];
81 static guint32 private_handles_count = 0;
82 static guint32 private_handles_slots_count = 0;
84 guint32 mono_w32handle_fd_reserve;
87 * This is an internal handle which is used for handling waiting for multiple handles.
88 * Threads which wait for multiple handles wait on this one handle, and when a handle
89 * is signalled, this handle is signalled too.
91 static mono_mutex_t global_signal_mutex;
92 static mono_cond_t global_signal_cond;
94 static mono_mutex_t scan_mutex;
96 static gboolean shutting_down = FALSE;
99 type_is_fd (MonoW32HandleType type)
102 case MONO_W32HANDLE_FILE:
103 case MONO_W32HANDLE_CONSOLE:
104 case MONO_W32HANDLE_SOCKET:
105 case MONO_W32HANDLE_PIPE:
113 mono_w32handle_lookup_data (gpointer handle, MonoW32HandleBase **handle_data)
117 g_assert (handle_data);
119 index = SLOT_INDEX ((gsize) handle);
120 if (index >= SLOT_MAX)
122 if (!private_handles [index])
125 offset = SLOT_OFFSET ((gsize) handle);
126 if (private_handles [index][offset].type == MONO_W32HANDLE_UNUSED)
129 *handle_data = &private_handles [index][offset];
134 mono_w32handle_get_type (gpointer handle)
136 MonoW32HandleBase *handle_data;
138 if (!mono_w32handle_lookup_data (handle, &handle_data))
139 return MONO_W32HANDLE_UNUSED; /* An impossible type */
141 return handle_data->type;
145 mono_w32handle_set_signal_state (gpointer handle, gboolean state, gboolean broadcast)
147 MonoW32HandleBase *handle_data;
149 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
154 g_message ("%s: setting state of %p to %s (broadcast %s)", __func__,
155 handle, state?"TRUE":"FALSE", broadcast?"TRUE":"FALSE");
159 /* Tell everyone blocking on a single handle */
161 /* The condition the global signal cond is waiting on is the signalling of
162 * _any_ handle. So lock it before setting the signalled state.
164 mono_os_mutex_lock (&global_signal_mutex);
166 /* This function _must_ be called with
167 * handle->signal_mutex locked
169 handle_data->signalled=state;
171 if (broadcast == TRUE) {
172 mono_os_cond_broadcast (&handle_data->signal_cond);
174 mono_os_cond_signal (&handle_data->signal_cond);
177 /* Tell everyone blocking on multiple handles that something
180 mono_os_cond_broadcast (&global_signal_cond);
182 mono_os_mutex_unlock (&global_signal_mutex);
184 handle_data->signalled=state;
189 mono_w32handle_issignalled (gpointer handle)
191 MonoW32HandleBase *handle_data;
193 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
197 return handle_data->signalled;
201 mono_w32handle_lock_signal_mutex (void)
204 g_message ("%s: lock global signal mutex", __func__);
207 mono_os_mutex_lock (&global_signal_mutex);
213 mono_w32handle_unlock_signal_mutex (void)
216 g_message ("%s: unlock global signal mutex", __func__);
219 mono_os_mutex_unlock (&global_signal_mutex);
225 mono_w32handle_lock_handle (gpointer handle)
227 MonoW32HandleBase *handle_data;
230 g_message ("%s: locking handle %p", __func__, handle);
233 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
237 mono_w32handle_ref (handle);
239 mono_os_mutex_lock (&handle_data->signal_mutex);
245 mono_w32handle_trylock_handle (gpointer handle)
247 MonoW32HandleBase *handle_data;
251 g_message ("%s: locking handle %p", __func__, handle);
254 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
258 mono_w32handle_ref (handle);
260 ret = mono_os_mutex_trylock (&handle_data->signal_mutex);
262 mono_w32handle_unref (handle);
269 mono_w32handle_unlock_handle (gpointer handle)
271 MonoW32HandleBase *handle_data;
274 g_message ("%s: unlocking handle %p", __func__, handle);
277 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
281 mono_os_mutex_unlock (&handle_data->signal_mutex);
283 mono_w32handle_unref (handle);
291 * Initialize the io-layer.
294 mono_w32handle_init (void)
296 static gboolean initialized = FALSE;
301 g_assert ((sizeof (handle_ops) / sizeof (handle_ops[0]))
302 == MONO_W32HANDLE_COUNT);
304 /* This is needed by the code in mono_w32handle_new_internal */
305 mono_w32handle_fd_reserve = (eg_getdtablesize () + (HANDLE_PER_SLOT - 1)) & ~(HANDLE_PER_SLOT - 1);
309 * The entries in private_handles reserved for fds are allocated lazily to
313 private_handles_count += HANDLE_PER_SLOT;
314 private_handles_slots_count ++;
315 } while(mono_w32handle_fd_reserve > private_handles_count);
317 mono_os_mutex_init (&scan_mutex);
319 mono_os_cond_init (&global_signal_cond);
320 mono_os_mutex_init (&global_signal_mutex);
326 mono_w32handle_cleanup (void)
330 g_assert (!shutting_down);
331 shutting_down = TRUE;
333 /* Every shared handle we were using ought really to be closed
334 * by now, but to make sure just blow them all away. The
335 * exiting finalizer thread in particular races us to the
336 * program exit and doesn't always win, so it can be left
337 * cluttering up the shared file. Anything else left over is
340 for(i = SLOT_INDEX (0); private_handles[i] != NULL; i++) {
341 for(j = SLOT_OFFSET (0); j < HANDLE_PER_SLOT; j++) {
342 MonoW32HandleBase *handle_data = &private_handles[i][j];
343 gpointer handle = GINT_TO_POINTER (i*HANDLE_PER_SLOT+j);
345 for(k = handle_data->ref; k > 0; k--) {
346 mono_w32handle_unref (handle);
351 for (i = 0; i < SLOT_MAX; ++i)
352 g_free (private_handles [i]);
355 static void mono_w32handle_init_handle (MonoW32HandleBase *handle,
356 MonoW32HandleType type, gpointer handle_specific)
358 g_assert (handle->ref == 0);
361 handle->signalled = FALSE;
364 mono_os_cond_init (&handle->signal_cond);
365 mono_os_mutex_init (&handle->signal_mutex);
368 handle->specific = g_memdup (handle_specific, mono_w32handle_ops_typesize (type));
372 * mono_w32handle_new_internal:
373 * @type: Init handle to this type
375 * Search for a free handle and initialize it. Return the handle on
376 * success and 0 on failure. This is only called from
377 * mono_w32handle_new, and scan_mutex must be held.
379 static guint32 mono_w32handle_new_internal (MonoW32HandleType type,
380 gpointer handle_specific)
383 static guint32 last = 0;
384 gboolean retry = FALSE;
386 /* A linear scan should be fast enough. Start from the last
387 * allocation, assuming that handles are allocated more often
388 * than they're freed. Leave the space reserved for file
392 if (last < mono_w32handle_fd_reserve) {
393 last = mono_w32handle_fd_reserve;
400 for(i = SLOT_INDEX (count); i < private_handles_slots_count; i++) {
401 if (private_handles [i]) {
402 for (k = SLOT_OFFSET (count); k < HANDLE_PER_SLOT; k++) {
403 MonoW32HandleBase *handle = &private_handles [i][k];
405 if(handle->type == MONO_W32HANDLE_UNUSED) {
408 mono_w32handle_init_handle (handle, type, handle_specific);
416 if(retry && last > mono_w32handle_fd_reserve) {
417 /* Try again from the beginning */
418 last = mono_w32handle_fd_reserve;
422 /* Will need to expand the array. The caller will sort it out */
428 mono_w32handle_new (MonoW32HandleType type, gpointer handle_specific)
430 guint32 handle_idx = 0;
433 g_assert (!shutting_down);
435 g_assert(!type_is_fd(type));
437 mono_os_mutex_lock (&scan_mutex);
439 while ((handle_idx = mono_w32handle_new_internal (type, handle_specific)) == 0) {
440 /* Try and expand the array, and have another go */
441 int idx = SLOT_INDEX (private_handles_count);
442 if (idx >= SLOT_MAX) {
446 private_handles [idx] = g_new0 (MonoW32HandleBase, HANDLE_PER_SLOT);
448 private_handles_count += HANDLE_PER_SLOT;
449 private_handles_slots_count ++;
452 mono_os_mutex_unlock (&scan_mutex);
454 if (handle_idx == 0) {
455 /* We ran out of slots */
456 handle = INVALID_HANDLE_VALUE;
457 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to create %s handle", __func__, mono_w32handle_ops_typename (type));
461 /* Make sure we left the space for fd mappings */
462 g_assert (handle_idx >= mono_w32handle_fd_reserve);
464 handle = GUINT_TO_POINTER (handle_idx);
466 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: create %s handle %p", __func__, mono_w32handle_ops_typename (type), handle);
472 gpointer mono_w32handle_new_fd (MonoW32HandleType type, int fd,
473 gpointer handle_specific)
475 MonoW32HandleBase *handle_data;
476 int fd_index, fd_offset;
478 g_assert (!shutting_down);
480 g_assert(type_is_fd(type));
482 if (fd >= mono_w32handle_fd_reserve) {
483 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));
485 return(GUINT_TO_POINTER (INVALID_HANDLE_VALUE));
488 fd_index = SLOT_INDEX (fd);
489 fd_offset = SLOT_OFFSET (fd);
491 /* Initialize the array entries on demand */
492 if (!private_handles [fd_index]) {
493 mono_os_mutex_lock (&scan_mutex);
495 if (!private_handles [fd_index])
496 private_handles [fd_index] = g_new0 (MonoW32HandleBase, HANDLE_PER_SLOT);
498 mono_os_mutex_unlock (&scan_mutex);
501 handle_data = &private_handles [fd_index][fd_offset];
503 if (handle_data->type != MONO_W32HANDLE_UNUSED) {
504 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));
505 /* FIXME: clean up this handle? We can't do anything
506 * with the fd, cos thats the new one
508 return(GUINT_TO_POINTER (INVALID_HANDLE_VALUE));
511 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: create %s handle %p", __func__, mono_w32handle_ops_typename (type), GUINT_TO_POINTER(fd));
513 mono_w32handle_init_handle (handle_data, type, handle_specific);
515 return(GUINT_TO_POINTER(fd));
519 mono_w32handle_lookup (gpointer handle, MonoW32HandleType type,
520 gpointer *handle_specific)
522 MonoW32HandleBase *handle_data;
524 g_assert (handle_specific);
526 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
530 if (handle_data->type != type) {
534 *handle_specific = handle_data->specific;
540 mono_w32handle_ref_core (gpointer handle, MonoW32HandleBase *handle_data);
543 mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data, guint minimum);
546 mono_w32handle_foreach (gboolean (*on_each)(gpointer handle, gpointer data, gpointer user_data), gpointer user_data)
550 mono_os_mutex_lock (&scan_mutex);
552 for (i = SLOT_INDEX (0); i < private_handles_slots_count; i++) {
553 if (!private_handles [i])
555 for (k = SLOT_OFFSET (0); k < HANDLE_PER_SLOT; k++) {
556 MonoW32HandleBase *handle_data = NULL;
558 gboolean destroy, finished;
560 handle_data = &private_handles [i][k];
561 if (handle_data->type == MONO_W32HANDLE_UNUSED)
564 handle = GUINT_TO_POINTER (i * HANDLE_PER_SLOT + k);
566 if (!mono_w32handle_ref_core (handle, handle_data)) {
567 /* we are racing with mono_w32handle_unref:
568 * the handle ref has been decremented, but it
569 * hasn't yet been destroyed. */
573 finished = on_each (handle, handle_data->specific, user_data);
575 /* we do not want to have to destroy the handle here,
576 * as it would means the ref/unref are unbalanced */
577 destroy = mono_w32handle_unref_core (handle, handle_data, 2);
586 mono_os_mutex_unlock (&scan_mutex);
590 mono_w32handle_ref_core (gpointer handle, MonoW32HandleBase *handle_data)
595 old = handle_data->ref;
600 } while (InterlockedCompareExchange ((gint32*) &handle_data->ref, new, old) != old);
602 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: ref %s handle %p, ref: %d -> %d",
603 __func__, mono_w32handle_ops_typename (handle_data->type), handle, old, new);
609 mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data, guint minimum)
611 MonoW32HandleType type;
614 type = handle_data->type;
617 old = handle_data->ref;
618 if (!(old >= minimum))
619 g_error ("%s: handle %p has ref %d, it should be >= %d", __func__, handle, old, minimum);
622 } while (InterlockedCompareExchange ((gint32*) &handle_data->ref, new, old) != old);
624 /* handle_data might contain invalid data from now on, if
625 * another thread is unref'ing this handle at the same time */
627 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: unref %s handle %p, ref: %d -> %d destroy: %s",
628 __func__, mono_w32handle_ops_typename (type), handle, old, new, new == 0 ? "true" : "false");
633 void mono_w32handle_ref (gpointer handle)
635 MonoW32HandleBase *handle_data;
637 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
638 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to ref handle %p, unknown handle", __func__, handle);
642 if (!mono_w32handle_ref_core (handle, handle_data))
643 g_error ("%s: failed to ref handle %p", __func__, handle);
646 static void (*_wapi_handle_ops_get_close_func (MonoW32HandleType type))(gpointer, gpointer);
648 /* The handle must not be locked on entry to this function */
650 mono_w32handle_unref (gpointer handle)
652 MonoW32HandleBase *handle_data;
655 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
656 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to unref handle %p, unknown handle",
661 destroy = mono_w32handle_unref_core (handle, handle_data, 1);
664 /* Need to copy the handle info, reset the slot in the
665 * array, and _only then_ call the close function to
666 * avoid race conditions (eg file descriptors being
667 * closed, and another file being opened getting the
668 * same fd racing the memset())
670 MonoW32HandleType type;
671 gpointer handle_specific;
672 void (*close_func)(gpointer, gpointer);
674 type = handle_data->type;
675 handle_specific = handle_data->specific;
677 mono_os_mutex_lock (&scan_mutex);
679 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: destroy %s handle %p", __func__, mono_w32handle_ops_typename (type), handle);
681 mono_os_mutex_destroy (&handle_data->signal_mutex);
682 mono_os_cond_destroy (&handle_data->signal_cond);
684 memset (handle_data, 0, sizeof (MonoW32HandleBase));
686 mono_os_mutex_unlock (&scan_mutex);
688 close_func = _wapi_handle_ops_get_close_func (type);
689 if (close_func != NULL) {
690 close_func (handle, handle_specific);
693 g_free (handle_specific);
698 mono_w32handle_register_ops (MonoW32HandleType type, MonoW32HandleOps *ops)
700 handle_ops [type] = ops;
703 void mono_w32handle_register_capabilities (MonoW32HandleType type,
704 MonoW32HandleCapability caps)
706 handle_caps[type] = caps;
709 gboolean mono_w32handle_test_capabilities (gpointer handle,
710 MonoW32HandleCapability caps)
712 MonoW32HandleBase *handle_data;
713 MonoW32HandleType type;
715 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
719 type = handle_data->type;
721 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: testing 0x%x against 0x%x (%d)", __func__,
722 handle_caps[type], caps, handle_caps[type] & caps);
724 return((handle_caps[type] & caps) != 0);
727 static void (*_wapi_handle_ops_get_close_func (MonoW32HandleType type))(gpointer, gpointer)
729 if (handle_ops[type] != NULL &&
730 handle_ops[type]->close != NULL) {
731 return (handle_ops[type]->close);
737 void mono_w32handle_ops_close (gpointer handle, gpointer data)
739 MonoW32HandleBase *handle_data;
740 MonoW32HandleType type;
742 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
746 type = handle_data->type;
748 if (handle_ops[type] != NULL &&
749 handle_ops[type]->close != NULL) {
750 handle_ops[type]->close (handle, data);
754 void mono_w32handle_ops_details (MonoW32HandleType type, gpointer data)
756 if (handle_ops[type] != NULL &&
757 handle_ops[type]->details != NULL) {
758 handle_ops[type]->details (data);
762 const gchar* mono_w32handle_ops_typename (MonoW32HandleType type)
764 g_assert (handle_ops [type]);
765 g_assert (handle_ops [type]->typename);
766 return handle_ops [type]->typename ();
769 gsize mono_w32handle_ops_typesize (MonoW32HandleType type)
771 g_assert (handle_ops [type]);
772 g_assert (handle_ops [type]->typesize);
773 return handle_ops [type]->typesize ();
776 void mono_w32handle_ops_signal (gpointer handle)
778 MonoW32HandleBase *handle_data;
779 MonoW32HandleType type;
781 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
785 type = handle_data->type;
787 if (handle_ops[type] != NULL && handle_ops[type]->signal != NULL) {
788 handle_ops[type]->signal (handle);
792 gboolean mono_w32handle_ops_own (gpointer handle, guint32 *statuscode)
794 MonoW32HandleBase *handle_data;
795 MonoW32HandleType type;
797 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
801 type = handle_data->type;
803 if (handle_ops[type] != NULL && handle_ops[type]->own_handle != NULL) {
804 return(handle_ops[type]->own_handle (handle, statuscode));
810 gboolean mono_w32handle_ops_isowned (gpointer handle)
812 MonoW32HandleBase *handle_data;
813 MonoW32HandleType type;
815 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
819 type = handle_data->type;
821 if (handle_ops[type] != NULL && handle_ops[type]->is_owned != NULL) {
822 return(handle_ops[type]->is_owned (handle));
829 mono_w32handle_ops_specialwait (gpointer handle, guint32 timeout, gboolean *alerted)
831 MonoW32HandleBase *handle_data;
832 MonoW32HandleType type;
834 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
838 type = handle_data->type;
840 if (handle_ops[type] != NULL &&
841 handle_ops[type]->special_wait != NULL) {
842 return(handle_ops[type]->special_wait (handle, timeout, alerted));
848 void mono_w32handle_ops_prewait (gpointer handle)
850 MonoW32HandleBase *handle_data;
851 MonoW32HandleType type;
853 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
857 type = handle_data->type;
859 if (handle_ops[type] != NULL &&
860 handle_ops[type]->prewait != NULL) {
861 handle_ops[type]->prewait (handle);
868 struct timespec sleepytime;
870 g_assert (ms < 1000);
872 sleepytime.tv_sec = 0;
873 sleepytime.tv_nsec = ms * 1000000;
874 nanosleep (&sleepytime, NULL);
878 mono_w32handle_lock_handles (gpointer *handles, gsize numhandles)
883 /* Lock all the handles, with backoff */
885 for(i=0; i<numhandles; i++) {
886 gpointer handle = handles[i];
888 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: attempting to lock %p", __func__, handle);
890 thr_ret = mono_w32handle_trylock_handle (handle);
895 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: attempt failed for %p: %s", __func__,
896 handle, strerror (thr_ret));
901 thr_ret = mono_w32handle_unlock_handle (handle);
902 g_assert (thr_ret == 0);
905 /* If iter ever reaches 100 the nanosleep will
906 * return EINVAL immediately, but we have a
907 * design flaw if that happens.
911 g_warning ("%s: iteration overflow!",
916 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: Backing off for %d ms", __func__,
924 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: Locked all handles", __func__);
928 mono_w32handle_unlock_handles (gpointer *handles, gsize numhandles)
933 for(i=0; i<numhandles; i++) {
934 gpointer handle = handles[i];
936 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: unlocking handle %p", __func__, handle);
938 thr_ret = mono_w32handle_unlock_handle (handle);
939 g_assert (thr_ret == 0);
944 mono_w32handle_timedwait_signal_naked (mono_cond_t *cond, mono_mutex_t *mutex, guint32 timeout, gboolean poll, gboolean *alerted)
949 res = mono_os_cond_timedwait (cond, mutex, timeout);
951 /* This is needed when waiting for process handles */
954 * pthread_cond_(timed)wait() can return 0 even if the condition was not
955 * signalled. This happens at least on Darwin. We surface this, i.e., we
956 * get spurious wake-ups.
958 * http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html
960 res = mono_os_cond_timedwait (cond, mutex, timeout);
963 /* Real timeout is less than 100ms time */
964 res = mono_os_cond_timedwait (cond, mutex, timeout);
966 res = mono_os_cond_timedwait (cond, mutex, 100);
968 /* Mask the fake timeout, this will cause
969 * another poll if the cond was not really signaled
981 signal_global (gpointer unused)
983 /* If we reach here, then interrupt token is set to the flag value, which
984 * means that the target thread is either
985 * - before the first CAS in timedwait, which means it won't enter the wait.
986 * - it is after the first CAS, so it is already waiting, or it will enter
987 * the wait, and it will be interrupted by the broadcast. */
988 mono_os_mutex_lock (&global_signal_mutex);
989 mono_os_cond_broadcast (&global_signal_cond);
990 mono_os_mutex_unlock (&global_signal_mutex);
994 mono_w32handle_timedwait_signal (guint32 timeout, gboolean poll, gboolean *alerted)
998 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: waiting for global", __func__);
1004 mono_thread_info_install_interrupt (signal_global, NULL, alerted);
1009 res = mono_w32handle_timedwait_signal_naked (&global_signal_cond, &global_signal_mutex, timeout, poll, alerted);
1012 mono_thread_info_uninstall_interrupt (alerted);
1018 signal_handle_and_unref (gpointer handle)
1020 MonoW32HandleBase *handle_data;
1022 mono_mutex_t *mutex;
1024 if (!mono_w32handle_lookup_data (handle, &handle_data))
1025 g_error ("cannot signal unknown handle %p", handle);
1027 /* If we reach here, then interrupt token is set to the flag value, which
1028 * means that the target thread is either
1029 * - before the first CAS in timedwait, which means it won't enter the wait.
1030 * - it is after the first CAS, so it is already waiting, or it will enter
1031 * the wait, and it will be interrupted by the broadcast. */
1032 cond = &handle_data->signal_cond;
1033 mutex = &handle_data->signal_mutex;
1035 mono_os_mutex_lock (mutex);
1036 mono_os_cond_broadcast (cond);
1037 mono_os_mutex_unlock (mutex);
1039 mono_w32handle_unref (handle);
1043 mono_w32handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboolean poll, gboolean *alerted)
1045 MonoW32HandleBase *handle_data;
1048 if (!mono_w32handle_lookup_data (handle, &handle_data))
1049 g_error ("cannot wait on unknown handle %p", handle);
1051 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: waiting for %p (type %s)", __func__, handle,
1052 mono_w32handle_ops_typename (mono_w32handle_get_type (handle)));
1058 mono_thread_info_install_interrupt (signal_handle_and_unref, handle, alerted);
1061 mono_w32handle_ref (handle);
1064 res = mono_w32handle_timedwait_signal_naked (&handle_data->signal_cond, &handle_data->signal_mutex, timeout, poll, alerted);
1067 mono_thread_info_uninstall_interrupt (alerted);
1069 /* if it is alerted, then the handle is unref in the interrupt callback */
1070 mono_w32handle_unref (handle);
1078 dump_callback (gpointer handle, gpointer handle_specific, gpointer user_data)
1080 MonoW32HandleBase *handle_data;
1082 if (!mono_w32handle_lookup_data (handle, &handle_data))
1083 g_error ("cannot dump unknown handle %p", handle);
1085 g_print ("%p [%7s] signalled: %5s ref: %3d ",
1086 handle, mono_w32handle_ops_typename (handle_data->type), handle_data->signalled ? "true" : "false", handle_data->ref);
1087 mono_w32handle_ops_details (handle_data->type, handle_data->specific);
1093 void mono_w32handle_dump (void)
1095 mono_w32handle_foreach (dump_callback, NULL);
1099 own_if_signalled (gpointer handle, guint32 *statuscode)
1101 if (!mono_w32handle_issignalled (handle))
1104 *statuscode = WAIT_OBJECT_0;
1105 mono_w32handle_ops_own (handle, statuscode);
1110 own_if_owned( gpointer handle, guint32 *statuscode)
1112 if (!mono_w32handle_ops_isowned (handle))
1115 *statuscode = WAIT_OBJECT_0;
1116 mono_w32handle_ops_own (handle, statuscode);
1120 MonoW32HandleWaitRet
1121 mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable)
1123 MonoW32HandleWaitRet ret;
1127 guint32 statuscode = 0;
1131 if (mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_SPECIAL_WAIT)) {
1132 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p has special wait",
1135 return mono_w32handle_ops_specialwait (handle, timeout, alertable ? &alerted : NULL);
1138 if (!mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_WAIT)) {
1139 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p can't be waited for",
1142 return MONO_W32HANDLE_WAIT_RET_FAILED;
1145 thr_ret = mono_w32handle_lock_handle (handle);
1146 g_assert (thr_ret == 0);
1148 if (mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_OWN)) {
1149 if (own_if_owned (handle, &statuscode)) {
1150 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p already owned",
1153 ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1158 if (timeout != INFINITE)
1159 start = mono_msec_ticks ();
1164 if (own_if_signalled (handle, &statuscode)) {
1165 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p signalled",
1168 ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1172 mono_w32handle_ops_prewait (handle);
1174 if (timeout == INFINITE) {
1175 waited = mono_w32handle_timedwait_signal_handle (handle, INFINITE, FALSE, alertable ? &alerted : NULL);
1179 elapsed = mono_msec_ticks () - start;
1180 if (elapsed > timeout) {
1181 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1185 waited = mono_w32handle_timedwait_signal_handle (handle, timeout - elapsed, FALSE, alertable ? &alerted : NULL);
1189 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1194 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1200 thr_ret = mono_w32handle_unlock_handle (handle);
1201 g_assert (thr_ret == 0);
1206 MonoW32HandleWaitRet
1207 mono_w32handle_wait_multiple (gpointer *handles, gsize nhandles, gboolean waitall, guint32 timeout, gboolean alertable)
1209 MonoW32HandleWaitRet ret;
1210 gboolean alerted, poll;
1213 gpointer handles_sorted [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS];
1214 guint32 statuscodes [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS] = {0};
1217 return MONO_W32HANDLE_WAIT_RET_FAILED;
1220 return mono_w32handle_wait_one (handles [0], timeout, alertable);
1224 if (nhandles > MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS) {
1225 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: too many handles: %zd",
1226 __func__, nhandles);
1228 return MONO_W32HANDLE_WAIT_RET_FAILED;
1231 for (i = 0; i < nhandles; ++i) {
1232 if (!mono_w32handle_test_capabilities (handles[i], MONO_W32HANDLE_CAP_WAIT)
1233 && !mono_w32handle_test_capabilities (handles[i], MONO_W32HANDLE_CAP_SPECIAL_WAIT))
1235 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p can't be waited for",
1236 __func__, handles [i]);
1238 return MONO_W32HANDLE_WAIT_RET_FAILED;
1241 handles_sorted [i] = handles [i];
1244 qsort (handles_sorted, nhandles, sizeof (gpointer), g_direct_equal);
1245 for (i = 1; i < nhandles; ++i) {
1246 if (handles_sorted [i - 1] == handles_sorted [i]) {
1247 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p is duplicated",
1248 __func__, handles_sorted [i]);
1250 return MONO_W32HANDLE_WAIT_RET_FAILED;
1255 for (i = 0; i < nhandles; ++i) {
1256 if (mono_w32handle_get_type (handles [i]) == MONO_W32HANDLE_PROCESS) {
1257 /* Can't wait for a process handle + another handle without polling */
1262 if (timeout != INFINITE)
1263 start = mono_msec_ticks ();
1265 for (i = 0; i < nhandles; ++i) {
1266 /* Add a reference, as we need to ensure the handle wont
1267 * disappear from under us while we're waiting in the loop
1268 * (not lock, as we don't want exclusive access here) */
1269 mono_w32handle_ref (handles [i]);
1273 gsize count, lowest;
1280 mono_w32handle_lock_handles (handles, nhandles);
1282 for (i = 0; i < nhandles; i++) {
1283 if ((mono_w32handle_test_capabilities (handles [i], MONO_W32HANDLE_CAP_OWN) && mono_w32handle_ops_isowned (handles [i]))
1284 || mono_w32handle_issignalled (handles [i]))
1293 signalled = (waitall && count == nhandles) || (!waitall && count > 0);
1296 for (i = 0; i < nhandles; i++)
1297 own_if_signalled (handles [i], &statuscodes [i]);
1300 mono_w32handle_unlock_handles (handles, nhandles);
1303 ret = MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + lowest;
1304 for (i = lowest; i < nhandles; i++) {
1305 if (statuscodes [i] == WAIT_ABANDONED_0) {
1306 ret = MONO_W32HANDLE_WAIT_RET_ABANDONED_0 + lowest;
1313 for (i = 0; i < nhandles; i++) {
1314 mono_w32handle_ops_prewait (handles[i]);
1316 if (mono_w32handle_test_capabilities (handles [i], MONO_W32HANDLE_CAP_SPECIAL_WAIT)
1317 && !mono_w32handle_issignalled (handles [i]))
1319 mono_w32handle_ops_specialwait (handles [i], 0, alertable ? &alerted : NULL);
1323 thr_ret = mono_w32handle_lock_signal_mutex ();
1324 g_assert (thr_ret == 0);
1328 for (i = 0; i < nhandles; ++i) {
1329 if (!mono_w32handle_issignalled (handles [i])) {
1336 for (i = 0; i < nhandles; ++i) {
1337 if (mono_w32handle_issignalled (handles [i])) {
1347 if (timeout == INFINITE) {
1348 waited = mono_w32handle_timedwait_signal (INFINITE, poll, alertable ? &alerted : NULL);
1352 elapsed = mono_msec_ticks () - start;
1353 if (elapsed > timeout) {
1354 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1356 thr_ret = mono_w32handle_unlock_signal_mutex ();
1357 g_assert (thr_ret == 0);
1362 waited = mono_w32handle_timedwait_signal (timeout - elapsed, poll, alertable ? &alerted : NULL);
1366 thr_ret = mono_w32handle_unlock_signal_mutex ();
1367 g_assert (thr_ret == 0);
1370 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1375 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1381 for (i = 0; i < nhandles; i++) {
1382 /* Unref everything we reffed above */
1383 mono_w32handle_unref (handles [i]);
1389 MonoW32HandleWaitRet
1390 mono_w32handle_signal_and_wait (gpointer signal_handle, gpointer wait_handle, guint32 timeout, gboolean alertable)
1392 MonoW32HandleWaitRet ret;
1396 guint32 statuscode = 0;
1400 if (!mono_w32handle_test_capabilities (signal_handle, MONO_W32HANDLE_CAP_SIGNAL))
1401 return MONO_W32HANDLE_WAIT_RET_FAILED;
1402 if (!mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_WAIT))
1403 return MONO_W32HANDLE_WAIT_RET_FAILED;
1405 if (mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_SPECIAL_WAIT)) {
1406 g_warning ("%s: handle %p has special wait, implement me!!", __func__, wait_handle);
1407 return MONO_W32HANDLE_WAIT_RET_FAILED;
1410 thr_ret = mono_w32handle_lock_handle (wait_handle);
1411 g_assert (thr_ret == 0);
1413 mono_w32handle_ops_signal (signal_handle);
1415 if (mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_OWN)) {
1416 if (own_if_owned (wait_handle, &statuscode)) {
1417 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p already owned",
1418 __func__, wait_handle);
1420 ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1425 if (timeout != INFINITE)
1426 start = mono_msec_ticks ();
1431 if (own_if_signalled (wait_handle, &statuscode)) {
1432 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p signalled",
1433 __func__, wait_handle);
1435 ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1439 mono_w32handle_ops_prewait (wait_handle);
1441 if (timeout == INFINITE) {
1442 waited = mono_w32handle_timedwait_signal_handle (wait_handle, INFINITE, FALSE, alertable ? &alerted : NULL);
1446 elapsed = mono_msec_ticks () - start;
1447 if (elapsed > timeout) {
1448 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1452 waited = mono_w32handle_timedwait_signal_handle (wait_handle, timeout - elapsed, FALSE, alertable ? &alerted : NULL);
1456 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1461 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1467 thr_ret = mono_w32handle_unlock_handle (wait_handle);
1468 g_assert (thr_ret == 0);
1473 #endif /* !defined(HOST_WIN32) */