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);
253 * mono_w32handle_init:
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);
513 w32handle_destroy (gpointer handle);
516 mono_w32handle_foreach (gboolean (*on_each)(gpointer handle, gpointer data, gpointer user_data), gpointer user_data)
518 GPtrArray *handles_to_destroy;
521 handles_to_destroy = NULL;
523 mono_os_mutex_lock (&scan_mutex);
525 for (i = SLOT_INDEX (0); i < private_handles_slots_count; i++) {
526 if (!private_handles [i])
528 for (k = SLOT_OFFSET (0); k < HANDLE_PER_SLOT; k++) {
529 MonoW32HandleBase *handle_data = NULL;
531 gboolean destroy, finished;
533 handle_data = &private_handles [i][k];
534 if (handle_data->type == MONO_W32HANDLE_UNUSED)
537 handle = GUINT_TO_POINTER (i * HANDLE_PER_SLOT + k);
539 if (!mono_w32handle_ref_core (handle, handle_data)) {
540 /* we are racing with mono_w32handle_unref:
541 * the handle ref has been decremented, but it
542 * hasn't yet been destroyed. */
546 finished = on_each (handle, handle_data->specific, user_data);
548 /* we might have to destroy the handle here, as
549 * it could have been unrefed in another thread */
550 destroy = mono_w32handle_unref_core (handle, handle_data);
552 /* we do not destroy it while holding the scan_mutex
553 * lock, because w32handle_destroy also needs to take
554 * the lock, and it calls user code which might lead
556 if (!handles_to_destroy)
557 handles_to_destroy = g_ptr_array_sized_new (4);
558 g_ptr_array_add (handles_to_destroy, handle);
567 mono_os_mutex_unlock (&scan_mutex);
569 if (handles_to_destroy) {
570 for (i = 0; i < handles_to_destroy->len; ++i)
571 w32handle_destroy (handles_to_destroy->pdata [i]);
573 g_ptr_array_free (handles_to_destroy, TRUE);
578 mono_w32handle_ref_core (gpointer handle, MonoW32HandleBase *handle_data)
583 old = handle_data->ref;
588 } while (InterlockedCompareExchange ((gint32*) &handle_data->ref, new, old) != old);
590 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: ref %s handle %p, ref: %d -> %d",
591 __func__, mono_w32handle_ops_typename (handle_data->type), handle, old, new);
597 mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data)
599 MonoW32HandleType type;
602 type = handle_data->type;
605 old = handle_data->ref;
607 g_error ("%s: handle %p has ref %d, it should be >= 1", __func__, handle, old);
610 } while (InterlockedCompareExchange ((gint32*) &handle_data->ref, new, old) != old);
612 /* handle_data might contain invalid data from now on, if
613 * another thread is unref'ing this handle at the same time */
615 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: unref %s handle %p, ref: %d -> %d destroy: %s",
616 __func__, mono_w32handle_ops_typename (type), handle, old, new, new == 0 ? "true" : "false");
621 void mono_w32handle_ref (gpointer handle)
623 MonoW32HandleBase *handle_data;
625 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
626 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to ref handle %p, unknown handle", __func__, handle);
630 if (!mono_w32handle_ref_core (handle, handle_data))
631 g_error ("%s: failed to ref handle %p", __func__, handle);
634 static void (*_wapi_handle_ops_get_close_func (MonoW32HandleType type))(gpointer, gpointer);
637 w32handle_destroy (gpointer handle)
639 /* Need to copy the handle info, reset the slot in the
640 * array, and _only then_ call the close function to
641 * avoid race conditions (eg file descriptors being
642 * closed, and another file being opened getting the
643 * same fd racing the memset())
645 MonoW32HandleBase *handle_data;
646 MonoW32HandleType type;
647 gpointer handle_specific;
648 void (*close_func)(gpointer, gpointer);
650 if (!mono_w32handle_lookup_data (handle, &handle_data))
651 g_error ("%s: unknown handle %p", __func__, handle);
653 type = handle_data->type;
654 handle_specific = handle_data->specific;
656 mono_os_mutex_lock (&scan_mutex);
658 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: destroy %s handle %p", __func__, mono_w32handle_ops_typename (type), handle);
660 mono_os_mutex_destroy (&handle_data->signal_mutex);
661 mono_os_cond_destroy (&handle_data->signal_cond);
663 memset (handle_data, 0, sizeof (MonoW32HandleBase));
665 mono_os_mutex_unlock (&scan_mutex);
667 close_func = _wapi_handle_ops_get_close_func (type);
668 if (close_func != NULL) {
669 close_func (handle, handle_specific);
672 g_free (handle_specific);
675 /* The handle must not be locked on entry to this function */
677 mono_w32handle_unref (gpointer handle)
679 MonoW32HandleBase *handle_data;
682 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
683 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to unref handle %p, unknown handle",
688 destroy = mono_w32handle_unref_core (handle, handle_data);
690 w32handle_destroy (handle);
694 mono_w32handle_ops_close (gpointer handle, gpointer data);
697 mono_w32handle_force_close (gpointer handle, gpointer data)
699 mono_w32handle_ops_close (handle, data);
703 mono_w32handle_register_ops (MonoW32HandleType type, MonoW32HandleOps *ops)
705 handle_ops [type] = ops;
708 void mono_w32handle_register_capabilities (MonoW32HandleType type,
709 MonoW32HandleCapability caps)
711 handle_caps[type] = caps;
714 gboolean mono_w32handle_test_capabilities (gpointer handle,
715 MonoW32HandleCapability caps)
717 MonoW32HandleBase *handle_data;
718 MonoW32HandleType type;
720 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
724 type = handle_data->type;
726 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: testing 0x%x against 0x%x (%d)", __func__,
727 handle_caps[type], caps, handle_caps[type] & caps);
729 return((handle_caps[type] & caps) != 0);
732 static void (*_wapi_handle_ops_get_close_func (MonoW32HandleType type))(gpointer, gpointer)
734 if (handle_ops[type] != NULL &&
735 handle_ops[type]->close != NULL) {
736 return (handle_ops[type]->close);
743 mono_w32handle_ops_close (gpointer handle, gpointer data)
745 MonoW32HandleBase *handle_data;
746 MonoW32HandleType type;
748 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
752 type = handle_data->type;
754 if (handle_ops[type] != NULL &&
755 handle_ops[type]->close != NULL) {
756 handle_ops[type]->close (handle, data);
761 mono_w32handle_ops_details (MonoW32HandleType type, gpointer data)
763 if (handle_ops[type] != NULL &&
764 handle_ops[type]->details != NULL) {
765 handle_ops[type]->details (data);
770 mono_w32handle_ops_typename (MonoW32HandleType type)
772 g_assert (handle_ops [type]);
773 g_assert (handle_ops [type]->typename);
774 return handle_ops [type]->typename ();
778 mono_w32handle_ops_typesize (MonoW32HandleType type)
780 g_assert (handle_ops [type]);
781 g_assert (handle_ops [type]->typesize);
782 return handle_ops [type]->typesize ();
786 mono_w32handle_ops_signal (gpointer handle)
788 MonoW32HandleBase *handle_data;
789 MonoW32HandleType type;
791 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
795 type = handle_data->type;
797 if (handle_ops[type] != NULL && handle_ops[type]->signal != NULL) {
798 handle_ops[type]->signal (handle);
803 mono_w32handle_ops_own (gpointer handle, gboolean *abandoned)
805 MonoW32HandleBase *handle_data;
806 MonoW32HandleType type;
808 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
812 type = handle_data->type;
814 if (handle_ops[type] != NULL && handle_ops[type]->own_handle != NULL) {
815 return(handle_ops[type]->own_handle (handle, abandoned));
822 mono_w32handle_ops_isowned (gpointer handle)
824 MonoW32HandleBase *handle_data;
825 MonoW32HandleType type;
827 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
831 type = handle_data->type;
833 if (handle_ops[type] != NULL && handle_ops[type]->is_owned != NULL) {
834 return(handle_ops[type]->is_owned (handle));
840 static MonoW32HandleWaitRet
841 mono_w32handle_ops_specialwait (gpointer handle, guint32 timeout, gboolean *alerted)
843 MonoW32HandleBase *handle_data;
844 MonoW32HandleType type;
846 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
847 return MONO_W32HANDLE_WAIT_RET_FAILED;
850 type = handle_data->type;
852 if (handle_ops[type] != NULL &&
853 handle_ops[type]->special_wait != NULL) {
854 return(handle_ops[type]->special_wait (handle, timeout, alerted));
856 return MONO_W32HANDLE_WAIT_RET_FAILED;
861 mono_w32handle_ops_prewait (gpointer handle)
863 MonoW32HandleBase *handle_data;
864 MonoW32HandleType type;
866 if (!mono_w32handle_lookup_data (handle, &handle_data)) {
870 type = handle_data->type;
872 if (handle_ops[type] != NULL &&
873 handle_ops[type]->prewait != NULL) {
874 handle_ops[type]->prewait (handle);
884 struct timespec sleepytime;
886 g_assert (ms < 1000);
888 sleepytime.tv_sec = 0;
889 sleepytime.tv_nsec = ms * 1000000;
890 nanosleep (&sleepytime, NULL);
891 #endif /* HOST_WIN32 */
895 mono_w32handle_lock_handles (gpointer *handles, gsize numhandles)
899 /* Lock all the handles, with backoff */
901 for(i=0; i<numhandles; i++) {
902 gpointer handle = handles[i];
904 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: attempting to lock %p", __func__, handle);
906 if (!mono_w32handle_trylock_handle (handle)) {
909 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: attempt failed for %p.", __func__,
915 mono_w32handle_unlock_handle (handle);
918 /* If iter ever reaches 100 the nanosleep will
919 * return EINVAL immediately, but we have a
920 * design flaw if that happens.
924 g_warning ("%s: iteration overflow!",
929 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: Backing off for %d ms", __func__,
937 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: Locked all handles", __func__);
941 mono_w32handle_unlock_handles (gpointer *handles, gsize numhandles)
945 for(i=0; i<numhandles; i++) {
946 gpointer handle = handles[i];
948 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: unlocking handle %p", __func__, handle);
950 mono_w32handle_unlock_handle (handle);
955 mono_w32handle_timedwait_signal_naked (mono_cond_t *cond, mono_mutex_t *mutex, guint32 timeout, gboolean poll, gboolean *alerted)
960 res = mono_os_cond_timedwait (cond, mutex, timeout);
962 /* This is needed when waiting for process handles */
965 * pthread_cond_(timed)wait() can return 0 even if the condition was not
966 * signalled. This happens at least on Darwin. We surface this, i.e., we
967 * get spurious wake-ups.
969 * http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_cond_wait.html
971 res = mono_os_cond_timedwait (cond, mutex, timeout);
974 /* Real timeout is less than 100ms time */
975 res = mono_os_cond_timedwait (cond, mutex, timeout);
977 res = mono_os_cond_timedwait (cond, mutex, 100);
979 /* Mask the fake timeout, this will cause
980 * another poll if the cond was not really signaled
992 signal_global (gpointer unused)
994 /* If we reach here, then interrupt token is set to the flag value, which
995 * means that the target thread is either
996 * - before the first CAS in timedwait, which means it won't enter the wait.
997 * - it is after the first CAS, so it is already waiting, or it will enter
998 * the wait, and it will be interrupted by the broadcast. */
999 mono_os_mutex_lock (&global_signal_mutex);
1000 mono_os_cond_broadcast (&global_signal_cond);
1001 mono_os_mutex_unlock (&global_signal_mutex);
1005 mono_w32handle_timedwait_signal (guint32 timeout, gboolean poll, gboolean *alerted)
1009 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: waiting for global", __func__);
1015 mono_thread_info_install_interrupt (signal_global, NULL, alerted);
1020 res = mono_w32handle_timedwait_signal_naked (&global_signal_cond, &global_signal_mutex, timeout, poll, alerted);
1023 mono_thread_info_uninstall_interrupt (alerted);
1029 signal_handle_and_unref (gpointer handle)
1031 MonoW32HandleBase *handle_data;
1033 mono_mutex_t *mutex;
1035 if (!mono_w32handle_lookup_data (handle, &handle_data))
1036 g_error ("cannot signal unknown handle %p", handle);
1038 /* If we reach here, then interrupt token is set to the flag value, which
1039 * means that the target thread is either
1040 * - before the first CAS in timedwait, which means it won't enter the wait.
1041 * - it is after the first CAS, so it is already waiting, or it will enter
1042 * the wait, and it will be interrupted by the broadcast. */
1043 cond = &handle_data->signal_cond;
1044 mutex = &handle_data->signal_mutex;
1046 mono_os_mutex_lock (mutex);
1047 mono_os_cond_broadcast (cond);
1048 mono_os_mutex_unlock (mutex);
1050 mono_w32handle_unref (handle);
1054 mono_w32handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboolean poll, gboolean *alerted)
1056 MonoW32HandleBase *handle_data;
1059 if (!mono_w32handle_lookup_data (handle, &handle_data))
1060 g_error ("cannot wait on unknown handle %p", handle);
1062 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: waiting for %p (type %s)", __func__, handle,
1063 mono_w32handle_ops_typename (mono_w32handle_get_type (handle)));
1069 mono_thread_info_install_interrupt (signal_handle_and_unref, handle, alerted);
1072 mono_w32handle_ref (handle);
1075 res = mono_w32handle_timedwait_signal_naked (&handle_data->signal_cond, &handle_data->signal_mutex, timeout, poll, alerted);
1078 mono_thread_info_uninstall_interrupt (alerted);
1080 /* if it is alerted, then the handle is unref in the interrupt callback */
1081 mono_w32handle_unref (handle);
1089 dump_callback (gpointer handle, gpointer handle_specific, gpointer user_data)
1091 MonoW32HandleBase *handle_data;
1093 if (!mono_w32handle_lookup_data (handle, &handle_data))
1094 g_error ("cannot dump unknown handle %p", handle);
1096 g_print ("%p [%7s] signalled: %5s ref: %3d ",
1097 handle, mono_w32handle_ops_typename (handle_data->type), handle_data->signalled ? "true" : "false", handle_data->ref);
1098 mono_w32handle_ops_details (handle_data->type, handle_data->specific);
1104 void mono_w32handle_dump (void)
1106 mono_w32handle_foreach (dump_callback, NULL);
1110 own_if_signalled (gpointer handle, gboolean *abandoned)
1112 if (!mono_w32handle_issignalled (handle))
1116 mono_w32handle_ops_own (handle, abandoned);
1121 own_if_owned( gpointer handle, gboolean *abandoned)
1123 if (!mono_w32handle_ops_isowned (handle))
1127 mono_w32handle_ops_own (handle, abandoned);
1131 MonoW32HandleWaitRet
1132 mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable)
1134 MonoW32HandleWaitRet ret;
1137 gboolean abandoned = FALSE;
1141 if (mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_SPECIAL_WAIT)) {
1142 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p has special wait",
1145 return mono_w32handle_ops_specialwait (handle, timeout, alertable ? &alerted : NULL);
1148 if (!mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_WAIT)) {
1149 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p can't be waited for",
1152 return MONO_W32HANDLE_WAIT_RET_FAILED;
1155 mono_w32handle_lock_handle (handle);
1157 if (mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_OWN)) {
1158 if (own_if_owned (handle, &abandoned)) {
1159 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p already owned",
1162 ret = abandoned ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1167 if (timeout != MONO_INFINITE_WAIT)
1168 start = mono_msec_ticks ();
1173 if (own_if_signalled (handle, &abandoned)) {
1174 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p signalled",
1177 ret = abandoned ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1181 mono_w32handle_ops_prewait (handle);
1183 if (timeout == MONO_INFINITE_WAIT) {
1184 waited = mono_w32handle_timedwait_signal_handle (handle, MONO_INFINITE_WAIT, FALSE, alertable ? &alerted : NULL);
1188 elapsed = mono_msec_ticks () - start;
1189 if (elapsed > timeout) {
1190 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1194 waited = mono_w32handle_timedwait_signal_handle (handle, timeout - elapsed, FALSE, alertable ? &alerted : NULL);
1198 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1203 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1209 mono_w32handle_unlock_handle (handle);
1214 MonoW32HandleWaitRet
1215 mono_w32handle_wait_multiple (gpointer *handles, gsize nhandles, gboolean waitall, guint32 timeout, gboolean alertable)
1217 MonoW32HandleWaitRet ret;
1218 gboolean alerted, poll;
1221 gpointer handles_sorted [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS];
1222 gboolean abandoned [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS] = {0};
1225 return MONO_W32HANDLE_WAIT_RET_FAILED;
1228 return mono_w32handle_wait_one (handles [0], timeout, alertable);
1232 if (nhandles > MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS) {
1233 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: too many handles: %zd",
1234 __func__, nhandles);
1236 return MONO_W32HANDLE_WAIT_RET_FAILED;
1239 for (i = 0; i < nhandles; ++i) {
1240 if (!mono_w32handle_test_capabilities (handles[i], MONO_W32HANDLE_CAP_WAIT)
1241 && !mono_w32handle_test_capabilities (handles[i], MONO_W32HANDLE_CAP_SPECIAL_WAIT))
1243 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p can't be waited for",
1244 __func__, handles [i]);
1246 return MONO_W32HANDLE_WAIT_RET_FAILED;
1249 handles_sorted [i] = handles [i];
1252 qsort (handles_sorted, nhandles, sizeof (gpointer), g_direct_equal);
1253 for (i = 1; i < nhandles; ++i) {
1254 if (handles_sorted [i - 1] == handles_sorted [i]) {
1255 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p is duplicated",
1256 __func__, handles_sorted [i]);
1258 return MONO_W32HANDLE_WAIT_RET_FAILED;
1263 for (i = 0; i < nhandles; ++i) {
1264 if (mono_w32handle_get_type (handles [i]) == MONO_W32HANDLE_PROCESS) {
1265 /* Can't wait for a process handle + another handle without polling */
1270 if (timeout != MONO_INFINITE_WAIT)
1271 start = mono_msec_ticks ();
1273 for (i = 0; i < nhandles; ++i) {
1274 /* Add a reference, as we need to ensure the handle wont
1275 * disappear from under us while we're waiting in the loop
1276 * (not lock, as we don't want exclusive access here) */
1277 mono_w32handle_ref (handles [i]);
1281 gsize count, lowest;
1288 mono_w32handle_lock_handles (handles, nhandles);
1290 for (i = 0; i < nhandles; i++) {
1291 if ((mono_w32handle_test_capabilities (handles [i], MONO_W32HANDLE_CAP_OWN) && mono_w32handle_ops_isowned (handles [i]))
1292 || mono_w32handle_issignalled (handles [i]))
1301 signalled = (waitall && count == nhandles) || (!waitall && count > 0);
1304 for (i = 0; i < nhandles; i++)
1305 own_if_signalled (handles [i], &abandoned [i]);
1308 mono_w32handle_unlock_handles (handles, nhandles);
1311 ret = MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + lowest;
1312 for (i = lowest; i < nhandles; i++) {
1313 if (abandoned [i]) {
1314 ret = MONO_W32HANDLE_WAIT_RET_ABANDONED_0 + lowest;
1321 for (i = 0; i < nhandles; i++) {
1322 mono_w32handle_ops_prewait (handles[i]);
1324 if (mono_w32handle_test_capabilities (handles [i], MONO_W32HANDLE_CAP_SPECIAL_WAIT)
1325 && !mono_w32handle_issignalled (handles [i]))
1327 mono_w32handle_ops_specialwait (handles [i], 0, alertable ? &alerted : NULL);
1331 mono_w32handle_lock_signal_mutex ();
1335 for (i = 0; i < nhandles; ++i) {
1336 if (!mono_w32handle_issignalled (handles [i])) {
1343 for (i = 0; i < nhandles; ++i) {
1344 if (mono_w32handle_issignalled (handles [i])) {
1354 if (timeout == MONO_INFINITE_WAIT) {
1355 waited = mono_w32handle_timedwait_signal (MONO_INFINITE_WAIT, poll, alertable ? &alerted : NULL);
1359 elapsed = mono_msec_ticks () - start;
1360 if (elapsed > timeout) {
1361 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1363 mono_w32handle_unlock_signal_mutex ();
1368 waited = mono_w32handle_timedwait_signal (timeout - elapsed, poll, alertable ? &alerted : NULL);
1372 mono_w32handle_unlock_signal_mutex ();
1375 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1380 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1386 for (i = 0; i < nhandles; i++) {
1387 /* Unref everything we reffed above */
1388 mono_w32handle_unref (handles [i]);
1394 MonoW32HandleWaitRet
1395 mono_w32handle_signal_and_wait (gpointer signal_handle, gpointer wait_handle, guint32 timeout, gboolean alertable)
1397 MonoW32HandleWaitRet ret;
1400 gboolean abandoned = FALSE;
1401 gpointer handles [2];
1405 if (!mono_w32handle_test_capabilities (signal_handle, MONO_W32HANDLE_CAP_SIGNAL))
1406 return MONO_W32HANDLE_WAIT_RET_FAILED;
1407 if (!mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_WAIT))
1408 return MONO_W32HANDLE_WAIT_RET_FAILED;
1410 if (mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_SPECIAL_WAIT)) {
1411 g_warning ("%s: handle %p has special wait, implement me!!", __func__, wait_handle);
1412 return MONO_W32HANDLE_WAIT_RET_FAILED;
1415 handles [0] = wait_handle;
1416 handles [1] = signal_handle;
1418 mono_w32handle_lock_handles (handles, 2);
1420 mono_w32handle_ops_signal (signal_handle);
1422 mono_w32handle_unlock_handle (signal_handle);
1424 if (mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_OWN)) {
1425 if (own_if_owned (wait_handle, &abandoned)) {
1426 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p already owned",
1427 __func__, wait_handle);
1429 ret = abandoned ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1434 if (timeout != MONO_INFINITE_WAIT)
1435 start = mono_msec_ticks ();
1440 if (own_if_signalled (wait_handle, &abandoned)) {
1441 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p signalled",
1442 __func__, wait_handle);
1444 ret = abandoned ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
1448 mono_w32handle_ops_prewait (wait_handle);
1450 if (timeout == MONO_INFINITE_WAIT) {
1451 waited = mono_w32handle_timedwait_signal_handle (wait_handle, MONO_INFINITE_WAIT, FALSE, alertable ? &alerted : NULL);
1455 elapsed = mono_msec_ticks () - start;
1456 if (elapsed > timeout) {
1457 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1461 waited = mono_w32handle_timedwait_signal_handle (wait_handle, timeout - elapsed, FALSE, alertable ? &alerted : NULL);
1465 ret = MONO_W32HANDLE_WAIT_RET_ALERTED;
1470 ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
1476 mono_w32handle_unlock_handle (wait_handle);