[io-layer] Extract file (#4255)
[mono.git] / mono / io-layer / wapi.c
index 613cce64f754ae9ec54ab7771d9f4502fd826032..14dc672e8ee9ac879a21bb175e3de57a9101d31f 100644 (file)
@@ -1,30 +1,56 @@
 
 #include "wapi.h"
 
-#include "handles-private.h"
-#include "process-private.h"
-#include "thread-private.h"
+#include "mono/utils/mono-lazy-init.h"
+#include "mono/metadata/w32handle.h"
 
-gboolean _wapi_has_shut_down = FALSE;
+/* Use this instead of getpid(), to cope with linuxthreads.  It's a
+ * function rather than a variable lookup because we need to get at
+ * this before share_init() might have been called. */
+static mono_lazy_init_t _wapi_pid_init_lazy = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
+static pid_t _wapi_pid;
 
-void
-wapi_init (void)
+static void
+_wapi_pid_init (void)
 {
-       _wapi_handle_init ();
-       _wapi_shm_semaphores_init ();
-       _wapi_io_init ();
-       wapi_processes_init ();
+       _wapi_pid = getpid ();
 }
 
-void
-wapi_cleanup (void)
+pid_t
+wapi_getpid (void)
 {
-       g_assert (_wapi_has_shut_down == FALSE);
-       _wapi_has_shut_down = TRUE;
+       mono_lazy_initialize (&_wapi_pid_init_lazy, _wapi_pid_init);
+       return _wapi_pid;
+}
+
+/**
+ * CloseHandle:
+ * @handle: The handle to release
+ *
+ * Closes and invalidates @handle, releasing any resources it
+ * consumes.  When the last handle to a temporary or non-persistent
+ * object is closed, that object can be deleted.  Closing the same
+ * handle twice is an error.
+ *
+ * Return value: %TRUE on success, %FALSE otherwise.
+ */
+gboolean CloseHandle(gpointer handle)
+{
+       if (handle == INVALID_HANDLE_VALUE){
+               SetLastError (ERROR_INVALID_PARAMETER);
+               return FALSE;
+       }
+       if (handle == (gpointer)0 && mono_w32handle_get_type (handle) != 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...
+                */
+               SetLastError (ERROR_INVALID_PARAMETER);
+               return FALSE;
+       }
 
-       _wapi_error_cleanup ();
-       _wapi_thread_cleanup ();
-       wapi_processes_cleanup ();
-       _wapi_io_cleanup ();
-       _wapi_handle_cleanup ();
-}
\ No newline at end of file
+       mono_w32handle_unref (handle);
+       return TRUE;
+}