[io-layer] Extract socket (#4241)
[mono.git] / mono / io-layer / wapi.c
1
2 #include "wapi.h"
3
4 #include "io-trace.h"
5 #include "io.h"
6
7 #include "mono/utils/mono-lazy-init.h"
8 #include "mono/metadata/w32handle.h"
9
10 gboolean _wapi_has_shut_down = FALSE;
11
12 void
13 wapi_init (void)
14 {
15         _wapi_io_init ();
16 }
17
18 void
19 wapi_cleanup (void)
20 {
21         g_assert (_wapi_has_shut_down == FALSE);
22         _wapi_has_shut_down = TRUE;
23
24         _wapi_error_cleanup ();
25         _wapi_io_cleanup ();
26 }
27
28 /* Use this instead of getpid(), to cope with linuxthreads.  It's a
29  * function rather than a variable lookup because we need to get at
30  * this before share_init() might have been called. */
31 static mono_lazy_init_t _wapi_pid_init_lazy = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
32 static pid_t _wapi_pid;
33
34 static void
35 _wapi_pid_init (void)
36 {
37         _wapi_pid = getpid ();
38 }
39
40 pid_t
41 wapi_getpid (void)
42 {
43         mono_lazy_initialize (&_wapi_pid_init_lazy, _wapi_pid_init);
44         return _wapi_pid;
45 }
46
47 /**
48  * CloseHandle:
49  * @handle: The handle to release
50  *
51  * Closes and invalidates @handle, releasing any resources it
52  * consumes.  When the last handle to a temporary or non-persistent
53  * object is closed, that object can be deleted.  Closing the same
54  * handle twice is an error.
55  *
56  * Return value: %TRUE on success, %FALSE otherwise.
57  */
58 gboolean CloseHandle(gpointer handle)
59 {
60         if (handle == INVALID_HANDLE_VALUE){
61                 SetLastError (ERROR_INVALID_PARAMETER);
62                 return FALSE;
63         }
64         if (handle == (gpointer)0 && mono_w32handle_get_type (handle) != MONO_W32HANDLE_CONSOLE) {
65                 /* Problem: because we map file descriptors to the
66                  * same-numbered handle we can't tell the difference
67                  * between a bogus handle and the handle to stdin.
68                  * Assume that it's the console handle if that handle
69                  * exists...
70                  */
71                 SetLastError (ERROR_INVALID_PARAMETER);
72                 return FALSE;
73         }
74
75         mono_w32handle_unref (handle);
76         return TRUE;
77 }