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