3 * namespace for w32handles
6 * Ludovic Henry (luhenry@microsoft.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #include "w32handle-namespace.h"
18 #include "w32semaphore.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 (MonoW32Type type
)
47 case MONO_W32TYPE_NAMEDMUTEX
:
48 case MONO_W32TYPE_NAMEDSEM
:
49 case MONO_W32TYPE_NAMEDEVENT
:
60 } NamespaceSearchHandleData
;
63 mono_w32handle_namespace_search_handle_callback (MonoW32Handle
*handle_data
, gpointer user_data
)
65 NamespaceSearchHandleData
*search_data
;
66 MonoW32HandleNamespace
*sharedns
;
68 if (!has_namespace (handle_data
->type
))
71 search_data
= (NamespaceSearchHandleData
*) user_data
;
73 switch (handle_data
->type
) {
74 case MONO_W32TYPE_NAMEDMUTEX
: sharedns
= mono_w32mutex_get_namespace ((MonoW32HandleNamedMutex
*) handle_data
->specific
); break;
75 case MONO_W32TYPE_NAMEDSEM
: sharedns
= mono_w32semaphore_get_namespace ((MonoW32HandleNamedSemaphore
*) handle_data
->specific
); break;
76 case MONO_W32TYPE_NAMEDEVENT
: sharedns
= mono_w32event_get_namespace ((MonoW32HandleNamedEvent
*) handle_data
->specific
); break;
78 g_assert_not_reached ();
81 if (strcmp (sharedns
->name
, search_data
->name
) == 0) {
82 if (handle_data
->type
!= search_data
->type
) {
83 /* Its the wrong type, so fail now */
84 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER_HANDLE
, "%s: handle %p matches name but is wrong type: %s",
85 __func__
, handle_data
, mono_w32handle_get_typename (handle_data
->type
));
86 search_data
->ret
= INVALID_HANDLE_VALUE
;
88 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER_HANDLE
, "%s: handle %p matches name and type",
89 __func__
, handle_data
);
91 /* we do not want the handle to be destroyed before we return it */
92 search_data
->ret
= mono_w32handle_duplicate (handle_data
);
102 mono_w32handle_namespace_search_handle (MonoW32Type type
, const gchar
*name
)
104 NamespaceSearchHandleData search_data
;
106 if (!has_namespace (type
))
107 g_error ("%s: type %s does not have a namespace", __func__
, mono_w32handle_get_typename (type
));
109 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_IO_LAYER_HANDLE
, "%s: Lookup for handle named [%s] type %s",
110 __func__
, name
, mono_w32handle_get_typename (type
));
112 search_data
.ret
= NULL
;
113 search_data
.type
= type
;
114 search_data
.name
= name
;
115 mono_w32handle_foreach (mono_w32handle_namespace_search_handle_callback
, &search_data
);
116 return search_data
.ret
;