Revert "[w32handle] Remove use of w32handle for File, Console, Pipe and Socket (...
[mono.git] / mono / metadata / w32handle.c
index 88895e7fb5e3624bdd1db59faaf69f1a3895f2ab..35a5dd93d87663884e7cb43bd2cfdba27547b8dc 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * w32handle.c:  Generic and internal operations on handles
+/**
+ * \file
+ * Generic and internal operations on handles
  *
  * Author:
  *     Dick Porter (dick@ximian.com)
@@ -24,7 +25,7 @@
 
 #undef DEBUG_REFS
 
-#define SLOT_MAX               (1024 * 16)
+#define SLOT_MAX               (1024 * 32)
 
 /* must be a power of 2 */
 #define HANDLE_PER_SLOT        (256)
@@ -33,6 +34,7 @@ typedef struct {
        MonoW32HandleType type;
        guint ref;
        gboolean signalled;
+       gboolean in_use;
        mono_mutex_t signal_mutex;
        mono_cond_t signal_cond;
        gpointer specific;
@@ -177,6 +179,17 @@ mono_w32handle_issignalled (gpointer handle)
        return handle_data->signalled;
 }
 
+static void
+mono_w32handle_set_in_use (gpointer handle, gboolean in_use)
+{
+       MonoW32HandleBase *handle_data;
+
+       if (!mono_w32handle_lookup_data (handle, &handle_data))
+               g_assert_not_reached ();
+
+       handle_data->in_use = in_use;
+}
+
 static void
 mono_w32handle_lock_signal_mutex (void)
 {
@@ -197,6 +210,12 @@ mono_w32handle_unlock_signal_mutex (void)
        mono_os_mutex_unlock (&global_signal_mutex);
 }
 
+static void
+mono_w32handle_ref (gpointer handle);
+
+static void
+mono_w32handle_unref (gpointer handle);
+
 void
 mono_w32handle_lock_handle (gpointer handle)
 {
@@ -249,11 +268,6 @@ mono_w32handle_unlock_handle (gpointer handle)
        mono_w32handle_unref (handle);
 }
 
-/*
- * wapi_init:
- *
- *   Initialize the io-layer.
- */
 void
 mono_w32handle_init (void)
 {
@@ -289,29 +303,11 @@ mono_w32handle_init (void)
 void
 mono_w32handle_cleanup (void)
 {
-       int i, j, k;
+       int i;
 
        g_assert (!shutting_down);
        shutting_down = TRUE;
 
-       /* Every shared handle we were using ought really to be closed
-        * by now, but to make sure just blow them all away.  The
-        * exiting finalizer thread in particular races us to the
-        * program exit and doesn't always win, so it can be left
-        * cluttering up the shared file.  Anything else left over is
-        * really a bug.
-        */
-       for(i = SLOT_INDEX (0); private_handles[i] != NULL; i++) {
-               for(j = SLOT_OFFSET (0); j < HANDLE_PER_SLOT; j++) {
-                       MonoW32HandleBase *handle_data = &private_handles[i][j];
-                       gpointer handle = GINT_TO_POINTER (i*HANDLE_PER_SLOT+j);
-
-                       for(k = handle_data->ref; k > 0; k--) {
-                               mono_w32handle_unref (handle);
-                       }
-               }
-       }
-
        for (i = 0; i < SLOT_MAX; ++i)
                g_free (private_handles [i]);
 }
@@ -455,19 +451,17 @@ gpointer mono_w32handle_new_fd (MonoW32HandleType type, int fd,
        fd_index = SLOT_INDEX (fd);
        fd_offset = SLOT_OFFSET (fd);
 
+       mono_os_mutex_lock (&scan_mutex);
        /* Initialize the array entries on demand */
        if (!private_handles [fd_index]) {
-               mono_os_mutex_lock (&scan_mutex);
-
                if (!private_handles [fd_index])
                        private_handles [fd_index] = g_new0 (MonoW32HandleBase, HANDLE_PER_SLOT);
-
-               mono_os_mutex_unlock (&scan_mutex);
        }
 
        handle_data = &private_handles [fd_index][fd_offset];
 
        if (handle_data->type != MONO_W32HANDLE_UNUSED) {
+               mono_os_mutex_unlock (&scan_mutex);
                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));
                /* FIXME: clean up this handle?  We can't do anything
                 * with the fd, cos thats the new one
@@ -479,9 +473,64 @@ gpointer mono_w32handle_new_fd (MonoW32HandleType type, int fd,
 
        mono_w32handle_init_handle (handle_data, type, handle_specific);
 
+       mono_os_mutex_unlock (&scan_mutex);
+
        return(GUINT_TO_POINTER(fd));
 }
 
+static gboolean
+mono_w32handle_ref_core (gpointer handle, MonoW32HandleBase *handle_data);
+
+static gboolean
+mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data);
+
+static void
+w32handle_destroy (gpointer handle);
+
+gpointer
+mono_w32handle_duplicate (gpointer handle)
+{
+       MonoW32HandleBase *handle_data;
+
+       if (handle == INVALID_HANDLE_VALUE)
+               return handle;
+       if (!mono_w32handle_lookup_data (handle, &handle_data))
+               return INVALID_HANDLE_VALUE;
+       if (handle == (gpointer) 0 && handle_data->type != MONO_W32HANDLE_CONSOLE)
+               return handle;
+
+       if (!mono_w32handle_ref_core (handle, handle_data))
+               g_error ("%s: failed to ref handle %p", __func__, handle);
+
+       return handle;
+}
+
+gboolean
+mono_w32handle_close (gpointer handle)
+{
+       MonoW32HandleBase *handle_data;
+       gboolean destroy;
+
+       if (handle == INVALID_HANDLE_VALUE)
+               return FALSE;
+       if (!mono_w32handle_lookup_data (handle, &handle_data))
+               return FALSE;
+       if (handle == (gpointer) 0 && handle_data->type != MONO_W32HANDLE_CONSOLE) {
+               /* Problem: because we map file descriptors to the
+                * same-numbered handle we can't tell the difference
+                * between a bogus handle and the handle to stdin.
+                * Assume that it's the console handle if that handle
+                * exists... */
+               return FALSE;
+       }
+
+       destroy = mono_w32handle_unref_core (handle, handle_data);
+       if (destroy)
+               w32handle_destroy (handle);
+
+       return TRUE;
+}
+
 gboolean
 mono_w32handle_lookup (gpointer handle, MonoW32HandleType type,
                              gpointer *handle_specific)
@@ -503,17 +552,14 @@ mono_w32handle_lookup (gpointer handle, MonoW32HandleType type,
        return(TRUE);
 }
 
-static gboolean
-mono_w32handle_ref_core (gpointer handle, MonoW32HandleBase *handle_data);
-
-static gboolean
-mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data, guint minimum);
-
 void
 mono_w32handle_foreach (gboolean (*on_each)(gpointer handle, gpointer data, gpointer user_data), gpointer user_data)
 {
+       GPtrArray *handles_to_destroy;
        guint32 i, k;
 
+       handles_to_destroy = NULL;
+
        mono_os_mutex_lock (&scan_mutex);
 
        for (i = SLOT_INDEX (0); i < private_handles_slots_count; i++) {
@@ -539,10 +585,18 @@ mono_w32handle_foreach (gboolean (*on_each)(gpointer handle, gpointer data, gpoi
 
                        finished = on_each (handle, handle_data->specific, user_data);
 
-                       /* we do not want to have to destroy the handle here,
-                        * as it would means the ref/unref are unbalanced */
-                       destroy = mono_w32handle_unref_core (handle, handle_data, 2);
-                       g_assert (!destroy);
+                       /* we might have to destroy the handle here, as
+                        * it could have been unrefed in another thread */
+                       destroy = mono_w32handle_unref_core (handle, handle_data);
+                       if (destroy) {
+                               /* we do not destroy it while holding the scan_mutex
+                                * lock, because w32handle_destroy also needs to take
+                                * the lock, and it calls user code which might lead
+                                * to a deadlock */
+                               if (!handles_to_destroy)
+                                       handles_to_destroy = g_ptr_array_sized_new (4);
+                               g_ptr_array_add (handles_to_destroy, handle);
+                       }
 
                        if (finished)
                                goto done;
@@ -551,6 +605,13 @@ mono_w32handle_foreach (gboolean (*on_each)(gpointer handle, gpointer data, gpoi
 
 done:
        mono_os_mutex_unlock (&scan_mutex);
+
+       if (handles_to_destroy) {
+               for (i = 0; i < handles_to_destroy->len; ++i)
+                       w32handle_destroy (handles_to_destroy->pdata [i]);
+
+               g_ptr_array_free (handles_to_destroy, TRUE);
+       }
 }
 
 static gboolean
@@ -573,7 +634,7 @@ mono_w32handle_ref_core (gpointer handle, MonoW32HandleBase *handle_data)
 }
 
 static gboolean
-mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data, guint minimum)
+mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data)
 {
        MonoW32HandleType type;
        guint old, new;
@@ -582,8 +643,8 @@ mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data, guin
 
        do {
                old = handle_data->ref;
-               if (!(old >= minimum))
-                       g_error ("%s: handle %p has ref %d, it should be >= %d", __func__, handle, old, minimum);
+               if (!(old >= 1))
+                       g_error ("%s: handle %p has ref %d, it should be >= 1", __func__, handle, old);
 
                new = old - 1;
        } while (InterlockedCompareExchange ((gint32*) &handle_data->ref, new, old) != old);
@@ -597,14 +658,13 @@ mono_w32handle_unref_core (gpointer handle, MonoW32HandleBase *handle_data, guin
        return new == 0;
 }
 
-void mono_w32handle_ref (gpointer handle)
+static void
+mono_w32handle_ref (gpointer handle)
 {
        MonoW32HandleBase *handle_data;
 
-       if (!mono_w32handle_lookup_data (handle, &handle_data)) {
-               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to ref handle %p, unknown handle", __func__, handle);
-               return;
-       }
+       if (!mono_w32handle_lookup_data (handle, &handle_data))
+               g_error ("%s: failed to ref handle %p, unknown handle", __func__, handle);
 
        if (!mono_w32handle_ref_core (handle, handle_data))
                g_error ("%s: failed to ref handle %p", __func__, handle);
@@ -612,53 +672,62 @@ void mono_w32handle_ref (gpointer handle)
 
 static void (*_wapi_handle_ops_get_close_func (MonoW32HandleType type))(gpointer, gpointer);
 
-/* The handle must not be locked on entry to this function */
-void
-mono_w32handle_unref (gpointer handle)
+static void
+w32handle_destroy (gpointer handle)
 {
+       /* Need to copy the handle info, reset the slot in the
+        * array, and _only then_ call the close function to
+        * avoid race conditions (eg file descriptors being
+        * closed, and another file being opened getting the
+        * same fd racing the memset())
+        */
        MonoW32HandleBase *handle_data;
-       gboolean destroy;
+       MonoW32HandleType type;
+       gpointer handle_specific;
+       void (*close_func)(gpointer, gpointer);
 
-       if (!mono_w32handle_lookup_data (handle, &handle_data)) {
-               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: failed to unref handle %p, unknown handle",
-                       __func__, handle);
-               return;
-       }
+       if (!mono_w32handle_lookup_data (handle, &handle_data))
+               g_error ("%s: unknown handle %p", __func__, handle);
 
-       destroy = mono_w32handle_unref_core (handle, handle_data, 1);
+       g_assert (!handle_data->in_use);
 
-       if (destroy) {
-               /* Need to copy the handle info, reset the slot in the
-                * array, and _only then_ call the close function to
-                * avoid race conditions (eg file descriptors being
-                * closed, and another file being opened getting the
-                * same fd racing the memset())
-                */
-               MonoW32HandleType type;
-               gpointer handle_specific;
-               void (*close_func)(gpointer, gpointer);
+       type = handle_data->type;
+       handle_specific = handle_data->specific;
 
-               type = handle_data->type;
-               handle_specific = handle_data->specific;
+       mono_os_mutex_lock (&scan_mutex);
 
-               mono_os_mutex_lock (&scan_mutex);
+       mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: destroy %s handle %p", __func__, mono_w32handle_ops_typename (type), handle);
 
-               mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: destroy %s handle %p", __func__, mono_w32handle_ops_typename (type), handle);
+       mono_os_mutex_destroy (&handle_data->signal_mutex);
+       mono_os_cond_destroy (&handle_data->signal_cond);
 
-               mono_os_mutex_destroy (&handle_data->signal_mutex);
-               mono_os_cond_destroy (&handle_data->signal_cond);
+       memset (handle_data, 0, sizeof (MonoW32HandleBase));
 
-               memset (handle_data, 0, sizeof (MonoW32HandleBase));
+       mono_os_mutex_unlock (&scan_mutex);
 
-               mono_os_mutex_unlock (&scan_mutex);
+       close_func = _wapi_handle_ops_get_close_func (type);
+       if (close_func != NULL) {
+               close_func (handle, handle_specific);
+       }
 
-               close_func = _wapi_handle_ops_get_close_func (type);
-               if (close_func != NULL) {
-                       close_func (handle, handle_specific);
-               }
+       memset (handle_specific, 0, mono_w32handle_ops_typesize (type));
 
-               g_free (handle_specific);
-       }
+       g_free (handle_specific);
+}
+
+/* The handle must not be locked on entry to this function */
+static void
+mono_w32handle_unref (gpointer handle)
+{
+       MonoW32HandleBase *handle_data;
+       gboolean destroy;
+
+       if (!mono_w32handle_lookup_data (handle, &handle_data))
+               g_error ("%s: failed to unref handle %p, unknown handle", __func__, handle);
+
+       destroy = mono_w32handle_unref_core (handle, handle_data);
+       if (destroy)
+               w32handle_destroy (handle);
 }
 
 static void
@@ -766,12 +835,12 @@ mono_w32handle_ops_signal (gpointer handle)
        type = handle_data->type;
 
        if (handle_ops[type] != NULL && handle_ops[type]->signal != NULL) {
-               handle_ops[type]->signal (handle);
+               handle_ops[type]->signal (handle, handle_data->specific);
        }
 }
 
 static gboolean
-mono_w32handle_ops_own (gpointer handle, guint32 *statuscode)
+mono_w32handle_ops_own (gpointer handle, gboolean *abandoned)
 {
        MonoW32HandleBase *handle_data;
        MonoW32HandleType type;
@@ -783,7 +852,7 @@ mono_w32handle_ops_own (gpointer handle, guint32 *statuscode)
        type = handle_data->type;
 
        if (handle_ops[type] != NULL && handle_ops[type]->own_handle != NULL) {
-               return(handle_ops[type]->own_handle (handle, statuscode));
+               return(handle_ops[type]->own_handle (handle, abandoned));
        } else {
                return(FALSE);
        }
@@ -815,7 +884,7 @@ mono_w32handle_ops_specialwait (gpointer handle, guint32 timeout, gboolean *aler
        MonoW32HandleType type;
 
        if (!mono_w32handle_lookup_data (handle, &handle_data)) {
-               return(WAIT_FAILED);
+               return MONO_W32HANDLE_WAIT_RET_FAILED;
        }
 
        type = handle_data->type;
@@ -824,7 +893,7 @@ mono_w32handle_ops_specialwait (gpointer handle, guint32 timeout, gboolean *aler
            handle_ops[type]->special_wait != NULL) {
                return(handle_ops[type]->special_wait (handle, timeout, alerted));
        } else {
-               return(WAIT_FAILED);
+               return MONO_W32HANDLE_WAIT_RET_FAILED;
        }
 }
 
@@ -1018,13 +1087,14 @@ signal_handle_and_unref (gpointer handle)
        mono_os_cond_broadcast (cond);
        mono_os_mutex_unlock (mutex);
 
-       mono_w32handle_unref (handle);
+       mono_w32handle_close (handle);
 }
 
 static int
 mono_w32handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboolean poll, gboolean *alerted)
 {
        MonoW32HandleBase *handle_data;
+       gpointer handle_duplicate;
        int res;
 
        if (!mono_w32handle_lookup_data (handle, &handle_data))
@@ -1037,10 +1107,11 @@ mono_w32handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboole
                *alerted = FALSE;
 
        if (alerted) {
-               mono_thread_info_install_interrupt (signal_handle_and_unref, handle, alerted);
-               if (*alerted)
+               mono_thread_info_install_interrupt (signal_handle_and_unref, handle_duplicate = mono_w32handle_duplicate (handle), alerted);
+               if (*alerted) {
+                       mono_w32handle_close (handle_duplicate);
                        return 0;
-               mono_w32handle_ref (handle);
+               }
        }
 
        res = mono_w32handle_timedwait_signal_naked (&handle_data->signal_cond, &handle_data->signal_mutex, timeout, poll, alerted);
@@ -1048,8 +1119,8 @@ mono_w32handle_timedwait_signal_handle (gpointer handle, guint32 timeout, gboole
        if (alerted) {
                mono_thread_info_uninstall_interrupt (alerted);
                if (!*alerted) {
-                       /* if it is alerted, then the handle is unref in the interrupt callback */
-                       mono_w32handle_unref (handle);
+                       /* if it is alerted, then the handle_duplicate is closed in the interrupt callback */
+                       mono_w32handle_close (handle_duplicate);
                }
        }
 
@@ -1065,7 +1136,7 @@ dump_callback (gpointer handle, gpointer handle_specific, gpointer user_data)
                g_error ("cannot dump unknown handle %p", handle);
 
        g_print ("%p [%7s] signalled: %5s ref: %3d ",
-               handle, mono_w32handle_ops_typename (handle_data->type), handle_data->signalled ? "true" : "false", handle_data->ref);
+               handle, mono_w32handle_ops_typename (handle_data->type), handle_data->signalled ? "true" : "false", handle_data->ref - 1 /* foreach increase ref by 1 */);
        mono_w32handle_ops_details (handle_data->type, handle_data->specific);
        g_print ("\n");
 
@@ -1078,24 +1149,24 @@ void mono_w32handle_dump (void)
 }
 
 static gboolean
-own_if_signalled (gpointer handle, guint32 *statuscode)
+own_if_signalled (gpointer handle, gboolean *abandoned)
 {
        if (!mono_w32handle_issignalled (handle))
                return FALSE;
 
-       *statuscode = WAIT_OBJECT_0;
-       mono_w32handle_ops_own (handle, statuscode);
+       *abandoned = FALSE;
+       mono_w32handle_ops_own (handle, abandoned);
        return TRUE;
 }
 
 static gboolean
-own_if_owned( gpointer handle, guint32 *statuscode)
+own_if_owned( gpointer handle, gboolean *abandoned)
 {
        if (!mono_w32handle_ops_isowned (handle))
                return FALSE;
 
-       *statuscode = WAIT_OBJECT_0;
-       mono_w32handle_ops_own (handle, statuscode);
+       *abandoned = FALSE;
+       mono_w32handle_ops_own (handle, abandoned);
        return TRUE;
 }
 
@@ -1105,7 +1176,7 @@ mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable)
        MonoW32HandleWaitRet ret;
        gboolean alerted;
        gint64 start;
-       guint32 statuscode = 0;
+       gboolean abandoned = FALSE;
 
        alerted = FALSE;
 
@@ -1126,11 +1197,11 @@ mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable)
        mono_w32handle_lock_handle (handle);
 
        if (mono_w32handle_test_capabilities (handle, MONO_W32HANDLE_CAP_OWN)) {
-               if (own_if_owned (handle, &statuscode)) {
+               if (own_if_owned (handle, &abandoned)) {
                        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p already owned",
                                __func__, handle);
 
-                       ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
+                       ret = abandoned ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
                        goto done;
                }
        }
@@ -1138,14 +1209,16 @@ mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable)
        if (timeout != MONO_INFINITE_WAIT)
                start = mono_msec_ticks ();
 
+       mono_w32handle_set_in_use (handle, TRUE);
+
        for (;;) {
                gint waited;
 
-               if (own_if_signalled (handle, &statuscode)) {
+               if (own_if_signalled (handle, &abandoned)) {
                        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p signalled",
                                __func__, handle);
 
-                       ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
+                       ret = abandoned ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
                        goto done;
                }
 
@@ -1177,6 +1250,8 @@ mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable)
        }
 
 done:
+       mono_w32handle_set_in_use (handle, FALSE);
+
        mono_w32handle_unlock_handle (handle);
 
        return ret;
@@ -1190,7 +1265,7 @@ mono_w32handle_wait_multiple (gpointer *handles, gsize nhandles, gboolean waital
        gint i;
        gint64 start;
        gpointer handles_sorted [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS];
-       guint32 statuscodes [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS] = {0};
+       gboolean abandoned [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS] = {0};
 
        if (nhandles == 0)
                return MONO_W32HANDLE_WAIT_RET_FAILED;
@@ -1273,7 +1348,7 @@ mono_w32handle_wait_multiple (gpointer *handles, gsize nhandles, gboolean waital
 
                if (signalled) {
                        for (i = 0; i < nhandles; i++)
-                               own_if_signalled (handles [i], &statuscodes [i]);
+                               own_if_signalled (handles [i], &abandoned [i]);
                }
 
                mono_w32handle_unlock_handles (handles, nhandles);
@@ -1281,7 +1356,7 @@ mono_w32handle_wait_multiple (gpointer *handles, gsize nhandles, gboolean waital
                if (signalled) {
                        ret = MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + lowest;
                        for (i = lowest; i < nhandles; i++) {
-                               if (statuscodes [i] == WAIT_ABANDONED_0) {
+                               if (abandoned [i]) {
                                        ret = MONO_W32HANDLE_WAIT_RET_ABANDONED_0 + lowest;
                                        break;
                                }
@@ -1368,7 +1443,7 @@ mono_w32handle_signal_and_wait (gpointer signal_handle, gpointer wait_handle, gu
        MonoW32HandleWaitRet ret;
        gint64 start;
        gboolean alerted;
-       guint32 statuscode = 0;
+       gboolean abandoned = FALSE;
        gpointer handles [2];
 
        alerted = FALSE;
@@ -1393,11 +1468,11 @@ mono_w32handle_signal_and_wait (gpointer signal_handle, gpointer wait_handle, gu
        mono_w32handle_unlock_handle (signal_handle);
 
        if (mono_w32handle_test_capabilities (wait_handle, MONO_W32HANDLE_CAP_OWN)) {
-               if (own_if_owned (wait_handle, &statuscode)) {
+               if (own_if_owned (wait_handle, &abandoned)) {
                        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p already owned",
                                __func__, wait_handle);
 
-                       ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
+                       ret = abandoned ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
                        goto done;
                }
        }
@@ -1408,11 +1483,11 @@ mono_w32handle_signal_and_wait (gpointer signal_handle, gpointer wait_handle, gu
        for (;;) {
                gint waited;
 
-               if (own_if_signalled (wait_handle, &statuscode)) {
+               if (own_if_signalled (wait_handle, &abandoned)) {
                        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_W32HANDLE, "%s: handle %p signalled",
                                __func__, wait_handle);
 
-                       ret = statuscode == WAIT_ABANDONED_0 ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
+                       ret = abandoned ? MONO_W32HANDLE_WAIT_RET_ABANDONED_0 : MONO_W32HANDLE_WAIT_RET_SUCCESS_0;
                        goto done;
                }