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/utils/mono-logger-internals.h"
20 #include "mono/utils/mono-coop-mutex.h"
22 static MonoCoopMutex lock;
25 mono_w32handle_namespace_init (void)
27 mono_coop_mutex_init (&lock);
31 mono_w32handle_namespace_lock (void)
33 mono_coop_mutex_lock (&lock);
37 mono_w32handle_namespace_unlock (void)
39 mono_coop_mutex_unlock (&lock);
43 has_namespace (MonoW32HandleType type)
46 case MONO_W32HANDLE_NAMEDMUTEX:
47 case MONO_W32HANDLE_NAMEDSEM:
48 case MONO_W32HANDLE_NAMEDEVENT:
57 MonoW32HandleType type;
59 } NamespaceSearchHandleData;
62 mono_w32handle_namespace_search_handle_callback (gpointer handle, gpointer data, gpointer user_data)
64 NamespaceSearchHandleData *search_data;
65 MonoW32HandleType type;
66 MonoW32HandleNamespace *sharedns;
68 type = mono_w32handle_get_type (handle);
69 if (!has_namespace (type))
72 search_data = (NamespaceSearchHandleData*) user_data;
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;
79 g_assert_not_reached ();
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;
89 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name and type",
92 /* we do not want the handle to be destroyed before we return it */
93 mono_w32handle_ref (handle);
95 search_data->ret = handle;
105 mono_w32handle_namespace_search_handle (MonoW32HandleType type, gchar *name)
107 NamespaceSearchHandleData search_data;
109 if (!has_namespace (type))
110 g_error ("%s: type %s does not have a namespace", __func__, type);
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));
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;