[io-layer] Extract error (#4279)
[mono.git] / mono / metadata / w32handle-namespace.c
1 /*
2  * w32handle-namespace.c: namespace for w32handles
3  *
4  * Author:
5  *      Ludovic Henry (luhenry@microsoft.com)
6  *
7  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8  */
9
10 #include <config.h>
11
12 #ifndef HOST_WIN32
13
14 #include "w32handle-namespace.h"
15
16 #include "w32mutex.h"
17 #include "w32semaphore.h"
18 #include "w32event.h"
19 #include "mono/utils/mono-logger-internals.h"
20 #include "mono/utils/mono-coop-mutex.h"
21
22 static MonoCoopMutex lock;
23
24 void
25 mono_w32handle_namespace_init (void)
26 {
27         mono_coop_mutex_init (&lock);
28 }
29
30 void
31 mono_w32handle_namespace_lock (void)
32 {
33         mono_coop_mutex_lock (&lock);
34 }
35
36 void
37 mono_w32handle_namespace_unlock (void)
38 {
39         mono_coop_mutex_unlock (&lock);
40 }
41
42 static gboolean
43 has_namespace (MonoW32HandleType type)
44 {
45         switch (type) {
46         case MONO_W32HANDLE_NAMEDMUTEX:
47         case MONO_W32HANDLE_NAMEDSEM:
48         case MONO_W32HANDLE_NAMEDEVENT:
49                 return TRUE;
50         default:
51                 return FALSE;
52         }
53 }
54
55 typedef struct {
56         gpointer ret;
57         MonoW32HandleType type;
58         gchar *name;
59 } NamespaceSearchHandleData;
60
61 static gboolean
62 mono_w32handle_namespace_search_handle_callback (gpointer handle, gpointer data, gpointer user_data)
63 {
64         NamespaceSearchHandleData *search_data;
65         MonoW32HandleType type;
66         MonoW32HandleNamespace *sharedns;
67
68         type = mono_w32handle_get_type (handle);
69         if (!has_namespace (type))
70                 return FALSE;
71
72         search_data = (NamespaceSearchHandleData*) user_data;
73
74         switch (type) {
75         case MONO_W32HANDLE_NAMEDMUTEX: sharedns = mono_w32mutex_get_namespace ((MonoW32HandleNamedMutex*) data); break;
76         case MONO_W32HANDLE_NAMEDSEM:   sharedns = mono_w32semaphore_get_namespace ((MonoW32HandleNamedSemaphore*) data); break;
77         case MONO_W32HANDLE_NAMEDEVENT: sharedns = mono_w32event_get_namespace ((MonoW32HandleNamedEvent*) data); break;
78         default:
79                 g_assert_not_reached ();
80         }
81
82         if (strcmp (sharedns->name, search_data->name) == 0) {
83                 if (type != search_data->type) {
84                         /* Its the wrong type, so fail now */
85                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name but is wrong type: %s",
86                                 __func__, handle, mono_w32handle_get_typename (type));
87                         search_data->ret = INVALID_HANDLE_VALUE;
88                 } else {
89                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name and type",
90                                 __func__, handle);
91
92                         /* we do not want the handle to be destroyed before we return it  */
93                         mono_w32handle_ref (handle);
94
95                         search_data->ret = handle;
96                 }
97
98                 return TRUE;
99         }
100
101         return FALSE;
102 }
103
104 gpointer
105 mono_w32handle_namespace_search_handle (MonoW32HandleType type, gchar *name)
106 {
107         NamespaceSearchHandleData search_data;
108
109         if (!has_namespace (type))
110                 g_error ("%s: type %s does not have a namespace", __func__, type);
111
112         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Lookup for handle named [%s] type %s",
113                 __func__, name, mono_w32handle_get_typename (type));
114
115         search_data.ret = NULL;
116         search_data.type = type;
117         search_data.name = name;
118         mono_w32handle_foreach (mono_w32handle_namespace_search_handle_callback, &search_data);
119         return search_data.ret;
120 }
121
122 #endif