2 * w32handle-namespace.c: namespace for w32handles
5 * Ludovic Henry (luhenry@microsoft.com)
7 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include "w32handle-namespace.h"
17 #include "w32semaphore.h"
19 #include "mono/io-layer/io-layer.h"
20 #include "mono/utils/mono-logger-internals.h"
21 #include "mono/utils/mono-coop-mutex.h"
23 static MonoCoopMutex lock;
26 mono_w32handle_namespace_init (void)
28 mono_coop_mutex_init (&lock);
32 mono_w32handle_namespace_lock (void)
34 mono_coop_mutex_lock (&lock);
38 mono_w32handle_namespace_unlock (void)
40 mono_coop_mutex_unlock (&lock);
44 has_namespace (MonoW32HandleType type)
47 case MONO_W32HANDLE_NAMEDMUTEX:
48 case MONO_W32HANDLE_NAMEDSEM:
49 case MONO_W32HANDLE_NAMEDEVENT:
58 MonoW32HandleType type;
60 } NamespaceSearchHandleData;
63 mono_w32handle_namespace_search_handle_callback (gpointer handle, gpointer data, gpointer user_data)
65 NamespaceSearchHandleData *search_data;
66 MonoW32HandleType type;
67 MonoW32HandleNamespace *sharedns;
69 type = mono_w32handle_get_type (handle);
70 if (!has_namespace (type))
73 search_data = (NamespaceSearchHandleData*) user_data;
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;
80 g_assert_not_reached ();
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;
90 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name and type",
93 /* we do not want the handle to be destroyed before we return it */
94 mono_w32handle_ref (handle);
96 search_data->ret = handle;
106 mono_w32handle_namespace_search_handle (MonoW32HandleType type, gchar *name)
108 NamespaceSearchHandleData search_data;
110 if (!has_namespace (type))
111 g_error ("%s: type %s does not have a namespace", __func__, type);
113 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Lookup for handle named [%s] type %s",
114 __func__, name, mono_w32handle_get_typename (type));
116 search_data.ret = NULL;
117 search_data.type = type;
118 search_data.name = name;
119 mono_w32handle_foreach (mono_w32handle_namespace_search_handle_callback, &search_data);
120 return search_data.ret;