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