[io-layer] Extract error (#4279)
[mono.git] / mono / metadata / w32event-unix.c
index 6fc2a223214e81f4e2799e225f8b6e320e8b28bc..50e9a6abad471f205cca5cef0cc66dd9a39bcaa2 100644 (file)
@@ -9,11 +9,13 @@
 
 #include "w32event.h"
 
+#include "w32error.h"
 #include "w32handle-namespace.h"
-#include "mono/io-layer/io-layer.h"
 #include "mono/utils/mono-logger-internals.h"
 #include "mono/metadata/w32handle.h"
 
+#define MAX_PATH 260
+
 typedef struct {
        gboolean manual;
        guint32 set_count;
@@ -24,22 +26,22 @@ struct MonoW32HandleNamedEvent {
        MonoW32HandleNamespace sharedns;
 };
 
-static gboolean event_handle_own (gpointer handle, MonoW32HandleType type, guint32 *statuscode)
+static gboolean event_handle_own (gpointer handle, MonoW32HandleType type, gboolean *abandoned)
 {
        MonoW32HandleEvent *event_handle;
        gboolean ok;
 
-       *statuscode = WAIT_OBJECT_0;
+       *abandoned = FALSE;
 
        ok = mono_w32handle_lookup (handle, type, (gpointer *)&event_handle);
        if (!ok) {
                g_warning ("%s: error looking up %s handle %p",
-                       __func__, mono_w32handle_ops_typename (type), handle);
+                       __func__, mono_w32handle_get_typename (type), handle);
                return FALSE;
        }
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: owning %s handle %p",
-               __func__, mono_w32handle_ops_typename (type), handle);
+               __func__, mono_w32handle_get_typename (type), handle);
 
        if (!event_handle->manual) {
                g_assert (event_handle->set_count > 0);
@@ -57,9 +59,9 @@ static void event_signal(gpointer handle)
        ves_icall_System_Threading_Events_SetEvent_internal (handle);
 }
 
-static gboolean event_own (gpointer handle, guint32 *statuscode)
+static gboolean event_own (gpointer handle, gboolean *abandoned)
 {
-       return event_handle_own (handle, MONO_W32HANDLE_EVENT, statuscode);
+       return event_handle_own (handle, MONO_W32HANDLE_EVENT, abandoned);
 }
 
 static void namedevent_signal (gpointer handle)
@@ -68,9 +70,9 @@ static void namedevent_signal (gpointer handle)
 }
 
 /* NB, always called with the shared handle lock held */
-static gboolean namedevent_own (gpointer handle, guint32 *statuscode)
+static gboolean namedevent_own (gpointer handle, gboolean *abandoned)
 {
-       return event_handle_own (handle, MONO_W32HANDLE_NAMEDEVENT, statuscode);
+       return event_handle_own (handle, MONO_W32HANDLE_NAMEDEVENT, abandoned);
 }
 
 static void event_details (gpointer data)
@@ -156,6 +158,12 @@ mono_w32event_create (gboolean manual, gboolean initial)
        return handle;
 }
 
+gboolean
+mono_w32event_close (gpointer handle)
+{
+       return mono_w32handle_close (handle);
+}
+
 void
 mono_w32event_set (gpointer handle)
 {
@@ -178,8 +186,8 @@ static gpointer event_handle_create (MonoW32HandleEvent *event_handle, MonoW32Ha
        handle = mono_w32handle_new (type, event_handle);
        if (handle == INVALID_HANDLE_VALUE) {
                g_warning ("%s: error creating %s handle",
-                       __func__, mono_w32handle_ops_typename (type));
-               SetLastError (ERROR_GEN_FAILURE);
+                       __func__, mono_w32handle_get_typename (type));
+               mono_w32error_set_last (ERROR_GEN_FAILURE);
                return NULL;
        }
 
@@ -191,7 +199,7 @@ static gpointer event_handle_create (MonoW32HandleEvent *event_handle, MonoW32Ha
        mono_w32handle_unlock_handle (handle);
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: created %s handle %p",
-               __func__, mono_w32handle_ops_typename (type), handle);
+               __func__, mono_w32handle_get_typename (type), handle);
 
        return handle;
 }
@@ -200,7 +208,7 @@ static gpointer event_create (gboolean manual, gboolean initial)
 {
        MonoW32HandleEvent event_handle;
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
-               __func__, mono_w32handle_ops_typename (MONO_W32HANDLE_EVENT));
+               __func__, mono_w32handle_get_typename (MONO_W32HANDLE_EVENT));
        return event_handle_create (&event_handle, MONO_W32HANDLE_EVENT, manual, initial);
 }
 
@@ -210,7 +218,7 @@ static gpointer namedevent_create (gboolean manual, gboolean initial, const guni
        gchar *utf8_name;
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: creating %s handle",
-               __func__, mono_w32handle_ops_typename (MONO_W32HANDLE_NAMEDEVENT));
+               __func__, mono_w32handle_get_typename (MONO_W32HANDLE_NAMEDEVENT));
 
        /* w32 seems to guarantee that opening named objects can't race each other */
        mono_w32handle_namespace_lock ();
@@ -221,10 +229,10 @@ static gpointer namedevent_create (gboolean manual, gboolean initial, const guni
        if (handle == INVALID_HANDLE_VALUE) {
                /* The name has already been used for a different object. */
                handle = NULL;
-               SetLastError (ERROR_INVALID_HANDLE);
+               mono_w32error_set_last (ERROR_INVALID_HANDLE);
        } else if (handle) {
                /* Not an error, but this is how the caller is informed that the event wasn't freshly created */
-               SetLastError (ERROR_ALREADY_EXISTS);
+               mono_w32error_set_last (ERROR_ALREADY_EXISTS);
 
                /* mono_w32handle_namespace_search_handle already adds a ref to the handle */
        } else {
@@ -252,11 +260,11 @@ ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, Mono
        /* Need to blow away any old errors here, because code tests
         * for ERROR_ALREADY_EXISTS on success (!) to see if an event
         * was freshly created */
-       SetLastError (ERROR_SUCCESS);
+       mono_w32error_set_last (ERROR_SUCCESS);
 
        event = name ? namedevent_create (manual, initial, mono_string_chars (name)) : event_create (manual, initial);
 
-       *error = GetLastError ();
+       *error = mono_w32error_get_last ();
 
        return event;
 }
@@ -268,7 +276,7 @@ ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
        MonoW32HandleEvent *event_handle;
 
        if (handle == NULL) {
-               SetLastError (ERROR_INVALID_HANDLE);
+               mono_w32error_set_last (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
 
@@ -277,18 +285,18 @@ ves_icall_System_Threading_Events_SetEvent_internal (gpointer handle)
        case MONO_W32HANDLE_NAMEDEVENT:
                break;
        default:
-               SetLastError (ERROR_INVALID_HANDLE);
+               mono_w32error_set_last (ERROR_INVALID_HANDLE);
                return FALSE;
        }
 
        if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
                g_warning ("%s: error looking up %s handle %p",
-                       __func__, mono_w32handle_ops_typename (type), handle);
+                       __func__, mono_w32handle_get_typename (type), handle);
                return FALSE;
        }
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: setting %s handle %p",
-               __func__, mono_w32handle_ops_typename (type), handle);
+               __func__, mono_w32handle_get_typename (type), handle);
 
        mono_w32handle_lock_handle (handle);
 
@@ -310,10 +318,10 @@ ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
        MonoW32HandleType type;
        MonoW32HandleEvent *event_handle;
 
-       SetLastError (ERROR_SUCCESS);
+       mono_w32error_set_last (ERROR_SUCCESS);
 
        if (handle == NULL) {
-               SetLastError (ERROR_INVALID_HANDLE);
+               mono_w32error_set_last (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
 
@@ -322,27 +330,27 @@ ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
        case MONO_W32HANDLE_NAMEDEVENT:
                break;
        default:
-               SetLastError (ERROR_INVALID_HANDLE);
+               mono_w32error_set_last (ERROR_INVALID_HANDLE);
                return FALSE;
        }
 
        if (!mono_w32handle_lookup (handle, type, (gpointer *)&event_handle)) {
                g_warning ("%s: error looking up %s handle %p",
-                       __func__, mono_w32handle_ops_typename (type), handle);
+                       __func__, mono_w32handle_get_typename (type), handle);
                return FALSE;
        }
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: resetting %s handle %p",
-               __func__, mono_w32handle_ops_typename (type), handle);
+               __func__, mono_w32handle_get_typename (type), handle);
 
        mono_w32handle_lock_handle (handle);
 
        if (!mono_w32handle_issignalled (handle)) {
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: no need to reset %s handle %p",
-                       __func__, mono_w32handle_ops_typename (type), handle);
+                       __func__, mono_w32handle_get_typename (type), handle);
        } else {
                mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: obtained write lock on %s handle %p",
-                       __func__, mono_w32handle_ops_typename (type), handle);
+                       __func__, mono_w32handle_get_typename (type), handle);
 
                mono_w32handle_set_signal_state (handle, FALSE, FALSE);
        }
@@ -357,7 +365,7 @@ ves_icall_System_Threading_Events_ResetEvent_internal (gpointer handle)
 void
 ves_icall_System_Threading_Events_CloseEvent_internal (gpointer handle)
 {
-       CloseHandle (handle);
+       mono_w32handle_close (handle);
 }
 
 gpointer