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