[w32handle] Replace mono_w32handle_{ref,unref} by mono_w32handle_{duplicate, close...
[mono-project.git] / mono / metadata / w32handle.h
blobb7df9a8810e50fbd9a1400cbdf9e443e0cbf732e
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 #ifndef INVALID_HANDLE_VALUE
16 #define INVALID_HANDLE_VALUE (gpointer)-1
17 #endif
19 #define MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS 64
21 #ifndef MONO_INFINITE_WAIT
22 #define MONO_INFINITE_WAIT ((guint32) 0xFFFFFFFF)
23 #endif
25 typedef enum {
26 MONO_W32HANDLE_UNUSED = 0,
27 MONO_W32HANDLE_FILE,
28 MONO_W32HANDLE_CONSOLE,
29 MONO_W32HANDLE_THREAD,
30 MONO_W32HANDLE_SEM,
31 MONO_W32HANDLE_MUTEX,
32 MONO_W32HANDLE_EVENT,
33 MONO_W32HANDLE_SOCKET,
34 MONO_W32HANDLE_FIND,
35 MONO_W32HANDLE_PROCESS,
36 MONO_W32HANDLE_PIPE,
37 MONO_W32HANDLE_NAMEDMUTEX,
38 MONO_W32HANDLE_NAMEDSEM,
39 MONO_W32HANDLE_NAMEDEVENT,
40 MONO_W32HANDLE_COUNT
41 } MonoW32HandleType;
43 typedef enum {
44 MONO_W32HANDLE_WAIT_RET_SUCCESS_0 = 0,
45 MONO_W32HANDLE_WAIT_RET_ABANDONED_0 = MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS,
46 MONO_W32HANDLE_WAIT_RET_ALERTED = -1,
47 MONO_W32HANDLE_WAIT_RET_TIMEOUT = -2,
48 MONO_W32HANDLE_WAIT_RET_FAILED = -3,
49 } MonoW32HandleWaitRet;
51 typedef struct
53 void (*close)(gpointer handle, gpointer data);
55 /* mono_w32handle_signal_and_wait */
56 void (*signal)(gpointer signal, gpointer data);
58 /* Called by mono_w32handle_wait_one and mono_w32handle_wait_multiple,
59 * with the handle locked (shared handles aren't locked.)
60 * Returns TRUE if ownership was established, false otherwise.
61 * If TRUE, *abandoned contains a status code such as
62 * WAIT_OBJECT_0 or WAIT_ABANDONED_0.
64 gboolean (*own_handle)(gpointer handle, gboolean *abandoned);
66 /* Called by mono_w32handle_wait_one and mono_w32handle_wait_multiple, if the
67 * handle in question is "ownable" (ie mutexes), to see if the current
68 * thread already owns this handle
70 gboolean (*is_owned)(gpointer handle);
72 /* Called by mono_w32handle_wait_one and mono_w32handle_wait_multiple,
73 * if the handle in question needs a special wait function
74 * instead of using the normal handle signal mechanism.
75 * Returns the mono_w32handle_wait_one return code.
77 MonoW32HandleWaitRet (*special_wait)(gpointer handle, guint32 timeout, gboolean *alerted);
79 /* Called by mono_w32handle_wait_one and mono_w32handle_wait_multiple,
80 * if the handle in question needs some preprocessing before the
81 * signal wait.
83 void (*prewait)(gpointer handle);
85 /* Called when dumping the handles */
86 void (*details)(gpointer data);
88 /* Called to get the name of the handle type */
89 const gchar* (*typename) (void);
91 /* Called to get the size of the handle type */
92 gsize (*typesize) (void);
93 } MonoW32HandleOps;
95 typedef enum {
96 MONO_W32HANDLE_CAP_WAIT = 0x01,
97 MONO_W32HANDLE_CAP_SIGNAL = 0x02,
98 MONO_W32HANDLE_CAP_OWN = 0x04,
99 MONO_W32HANDLE_CAP_SPECIAL_WAIT = 0x08,
100 } MonoW32HandleCapability;
102 extern guint32 mono_w32handle_fd_reserve;
104 void
105 mono_w32handle_init (void);
107 void
108 mono_w32handle_cleanup (void);
110 void
111 mono_w32handle_register_ops (MonoW32HandleType type, MonoW32HandleOps *ops);
113 gpointer
114 mono_w32handle_new (MonoW32HandleType type, gpointer handle_specific);
116 gpointer
117 mono_w32handle_new_fd (MonoW32HandleType type, int fd, gpointer handle_specific);
119 gpointer
120 mono_w32handle_duplicate (gpointer handle);
122 gboolean
123 mono_w32handle_close (gpointer handle);
125 MonoW32HandleType
126 mono_w32handle_get_type (gpointer handle);
128 const gchar*
129 mono_w32handle_get_typename (MonoW32HandleType type);
131 gboolean
132 mono_w32handle_lookup (gpointer handle, MonoW32HandleType type, gpointer *handle_specific);
134 void
135 mono_w32handle_foreach (gboolean (*on_each)(gpointer handle, gpointer data, gpointer user_data), gpointer user_data);
137 void
138 mono_w32handle_dump (void);
140 void
141 mono_w32handle_register_capabilities (MonoW32HandleType type, MonoW32HandleCapability caps);
143 gboolean
144 mono_w32handle_test_capabilities (gpointer handle, MonoW32HandleCapability caps);
146 void
147 mono_w32handle_force_close (gpointer handle, gpointer data);
149 void
150 mono_w32handle_set_signal_state (gpointer handle, gboolean state, gboolean broadcast);
152 gboolean
153 mono_w32handle_issignalled (gpointer handle);
155 void
156 mono_w32handle_lock_handle (gpointer handle);
158 gboolean
159 mono_w32handle_trylock_handle (gpointer handle);
161 void
162 mono_w32handle_unlock_handle (gpointer handle);
164 MonoW32HandleWaitRet
165 mono_w32handle_wait_one (gpointer handle, guint32 timeout, gboolean alertable);
167 MonoW32HandleWaitRet
168 mono_w32handle_wait_multiple (gpointer *handles, gsize nhandles, gboolean waitall, guint32 timeout, gboolean alertable);
170 MonoW32HandleWaitRet
171 mono_w32handle_signal_and_wait (gpointer signal_handle, gpointer wait_handle, guint32 timeout, gboolean alertable);
173 #ifdef HOST_WIN32
174 static inline MonoW32HandleWaitRet
175 mono_w32handle_convert_wait_ret (guint32 res, guint32 numobjects)
177 if (res >= WAIT_OBJECT_0 && res <= WAIT_OBJECT_0 + numobjects - 1)
178 return MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + (res - WAIT_OBJECT_0);
179 else if (res >= WAIT_ABANDONED_0 && res <= WAIT_ABANDONED_0 + numobjects - 1)
180 return MONO_W32HANDLE_WAIT_RET_ABANDONED_0 + (res - WAIT_ABANDONED_0);
181 else if (res == WAIT_IO_COMPLETION)
182 return MONO_W32HANDLE_WAIT_RET_ALERTED;
183 else if (res == WAIT_TIMEOUT)
184 return MONO_W32HANDLE_WAIT_RET_TIMEOUT;
185 else if (res == WAIT_FAILED)
186 return MONO_W32HANDLE_WAIT_RET_FAILED;
187 else
188 g_error ("%s: unknown res value %d", __func__, res);
190 #endif
193 #endif /* _MONO_METADATA_W32HANDLE_H_ */