[2020-02] Fix leak in assembly-specific dllmap lookups (#21053)
[mono-project.git] / mono / metadata / w32handle.h
blob215606314f385b72a0d798a197eca9caf8b86c0d
1 /**
2 * \file
3 */
5 #ifndef _MONO_METADATA_W32HANDLE_H_
6 #define _MONO_METADATA_W32HANDLE_H_
8 #include <config.h>
9 #include <glib.h>
11 #ifdef HOST_WIN32
12 #include <windows.h>
13 #else
14 #define INVALID_HANDLE_VALUE ((gpointer)-1)
15 #endif
17 #include "mono/utils/mono-coop-mutex.h"
18 #include "mono/utils/mono-error.h"
20 #define MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS 64
21 #define MONO_INFINITE_WAIT ((guint32) 0xFFFFFFFF)
23 typedef enum {
24 MONO_W32TYPE_UNUSED = 0,
25 MONO_W32TYPE_SEM,
26 MONO_W32TYPE_MUTEX,
27 MONO_W32TYPE_EVENT,
28 MONO_W32TYPE_PROCESS,
29 MONO_W32TYPE_NAMEDMUTEX,
30 MONO_W32TYPE_NAMEDSEM,
31 MONO_W32TYPE_NAMEDEVENT,
32 MONO_W32TYPE_COUNT
33 } MonoW32Type;
35 typedef struct {
36 MonoW32Type type;
37 guint ref;
38 gboolean signalled;
39 gboolean in_use;
40 MonoCoopMutex signal_mutex;
41 MonoCoopCond signal_cond;
42 gpointer specific;
43 } MonoW32Handle;
45 typedef enum {
46 MONO_W32HANDLE_WAIT_RET_SUCCESS_0 = 0,
47 MONO_W32HANDLE_WAIT_RET_ABANDONED_0 = MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS,
48 MONO_W32HANDLE_WAIT_RET_ALERTED = -1,
49 MONO_W32HANDLE_WAIT_RET_TIMEOUT = -2,
50 MONO_W32HANDLE_WAIT_RET_FAILED = -3,
51 MONO_W32HANDLE_WAIT_RET_TOO_MANY_POSTS = -4,
52 MONO_W32HANDLE_WAIT_RET_NOT_OWNED_BY_CALLER = -5
53 } MonoW32HandleWaitRet;
55 typedef struct
57 void (*close)(gpointer data);
59 /* mono_w32handle_signal_and_wait */
60 gint32 (*signal)(MonoW32Handle *handle_data);
62 /* Called by mono_w32handle_wait_one and mono_w32handle_wait_multiple,
63 * with the handle locked (shared handles aren't locked.)
64 * Returns TRUE if ownership was established, false otherwise.
65 * If TRUE, *abandoned contains a status code such as
66 * WAIT_OBJECT_0 or WAIT_ABANDONED_0.
68 gboolean (*own_handle)(MonoW32Handle *handle_data, gboolean *abandoned);
70 /* Called by mono_w32handle_wait_one and mono_w32handle_wait_multiple, if the
71 * handle in question is "ownable" (ie mutexes), to see if the current
72 * thread already owns this handle
74 gboolean (*is_owned)(MonoW32Handle *handle_data);
76 /* Called by mono_w32handle_wait_one and mono_w32handle_wait_multiple,
77 * if the handle in question needs a special wait function
78 * instead of using the normal handle signal mechanism.
79 * Returns the mono_w32handle_wait_one return code.
81 MonoW32HandleWaitRet (*special_wait)(MonoW32Handle *handle_data, guint32 timeout, gboolean *alerted);
83 /* Called by mono_w32handle_wait_one and mono_w32handle_wait_multiple,
84 * if the handle in question needs some preprocessing before the
85 * signal wait.
87 void (*prewait)(MonoW32Handle *handle_data);
89 /* Called when dumping the handles */
90 void (*details)(MonoW32Handle *handle_data);
92 /* Called to get the name of the handle type */
93 const char* (*type_name) (void);
95 /* Called to get the size of the handle type */
96 gsize (*typesize) (void);
97 } MonoW32HandleOps;
99 typedef enum {
100 MONO_W32HANDLE_CAP_WAIT = 0x01,
101 MONO_W32HANDLE_CAP_SIGNAL = 0x02,
102 MONO_W32HANDLE_CAP_OWN = 0x04,
103 MONO_W32HANDLE_CAP_SPECIAL_WAIT = 0x08,
104 } MonoW32HandleCapability;
106 void
107 mono_w32handle_init (void);
109 void
110 mono_w32handle_cleanup (void);
112 void
113 mono_w32handle_register_ops (MonoW32Type type, const MonoW32HandleOps *ops);
115 gpointer
116 mono_w32handle_new (MonoW32Type type, gpointer handle_specific);
118 gpointer
119 mono_w32handle_duplicate (MonoW32Handle *handle_data);
121 gboolean
122 mono_w32handle_close (gpointer handle);
124 const gchar*
125 mono_w32handle_get_typename (MonoW32Type type);
127 gboolean
128 mono_w32handle_lookup_and_ref (gpointer handle, MonoW32Handle **handle_data);
130 void
131 mono_w32handle_unref (MonoW32Handle *handle_data);
133 void
134 mono_w32handle_foreach (gboolean (*on_each)(MonoW32Handle *handle_data, gpointer user_data), gpointer user_data);
136 void
137 mono_w32handle_dump (void);
139 void
140 mono_w32handle_register_capabilities (MonoW32Type type, MonoW32HandleCapability caps);
142 void
143 mono_w32handle_set_signal_state (MonoW32Handle *handle_data, gboolean state, gboolean broadcast);
145 gboolean
146 mono_w32handle_issignalled (MonoW32Handle *handle_data);
148 void
149 mono_w32handle_lock (MonoW32Handle *handle_data);
151 gboolean
152 mono_w32handle_trylock (MonoW32Handle *handle_data);
154 void
155 mono_w32handle_unlock (MonoW32Handle *handle_data);
157 MonoW32HandleWaitRet
158 mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable);
160 MonoW32HandleWaitRet
161 mono_w32handle_wait_multiple (gpointer *handles, gsize nhandles, gboolean waitall, guint32 timeout, gboolean alertable, MonoError *error);
163 MonoW32HandleWaitRet
164 mono_w32handle_signal_and_wait (gpointer signal_handle, gpointer wait_handle, guint32 timeout, gboolean alertable);
166 #ifdef HOST_WIN32
167 static inline MonoW32HandleWaitRet
168 mono_w32handle_convert_wait_ret (guint32 res, guint32 numobjects)
170 if (res >= WAIT_OBJECT_0 && res <= WAIT_OBJECT_0 + numobjects - 1)
171 return (MonoW32HandleWaitRet)(MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + (res - WAIT_OBJECT_0));
172 else if (res >= WAIT_ABANDONED_0 && res <= WAIT_ABANDONED_0 + numobjects - 1)
173 return (MonoW32HandleWaitRet)(MONO_W32HANDLE_WAIT_RET_ABANDONED_0 + (res - WAIT_ABANDONED_0));
174 else if (res == WAIT_IO_COMPLETION)
175 return MONO_W32HANDLE_WAIT_RET_ALERTED;
176 else if (res == WAIT_TIMEOUT)
177 return MONO_W32HANDLE_WAIT_RET_TIMEOUT;
178 else if (res == WAIT_FAILED)
179 return MONO_W32HANDLE_WAIT_RET_FAILED;
180 else
181 g_error ("%s: unknown res value %d", __func__, res);
183 #endif
186 #endif /* _MONO_METADATA_W32HANDLE_H_ */