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