2 * events.c: Event handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
15 #include <mono/io-layer/wapi.h>
16 #include <mono/io-layer/wapi-private.h>
17 #include <mono/io-layer/handles-private.h>
18 #include <mono/io-layer/misc-private.h>
20 #include <mono/io-layer/mono-mutex.h>
22 #include <mono/io-layer/event-private.h>
26 static void event_close_shared (gpointer handle);
27 static void event_signal(gpointer handle);
28 static void event_own (gpointer handle);
30 struct _WapiHandleOps _wapi_event_ops = {
31 event_close_shared, /* close_shared */
32 NULL, /* close_private */
33 event_signal, /* signal */
38 static mono_once_t event_ops_once=MONO_ONCE_INIT;
40 static void event_ops_init (void)
42 _wapi_handle_register_capabilities (WAPI_HANDLE_EVENT,
43 WAPI_HANDLE_CAP_WAIT |
44 WAPI_HANDLE_CAP_SIGNAL);
47 static void event_close_shared(gpointer handle)
49 struct _WapiHandle_event *event_handle;
52 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
53 (gpointer *)&event_handle, NULL);
55 g_warning (G_GNUC_PRETTY_FUNCTION
56 ": error looking up event handle %p", handle);
61 g_message(G_GNUC_PRETTY_FUNCTION ": closing event handle %p", handle);
65 static void event_signal(gpointer handle)
70 static void event_own (gpointer handle)
72 struct _WapiHandle_event *event_handle;
75 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
76 (gpointer *)&event_handle, NULL);
78 g_warning (G_GNUC_PRETTY_FUNCTION
79 ": error looking up event handle %p", handle);
84 g_message(G_GNUC_PRETTY_FUNCTION ": owning event handle %p", handle);
87 if(event_handle->manual==FALSE) {
88 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
94 * @security: Ignored for now.
95 * @manual: Specifies whether the new event handle has manual or auto
97 * @initial: Specifies whether the new event handle is initially
99 * @name:Pointer to a string specifying the name of this name, or
100 * %NULL. Currently ignored.
102 * Creates a new event handle.
104 * An event handle is signalled with SetEvent(). If the new handle is
105 * a manual reset event handle, it remains signalled until it is reset
106 * with ResetEvent(). An auto reset event remains signalled until a
107 * single thread has waited for it, at which time the event handle is
108 * automatically reset to unsignalled.
110 * Return value: A new handle, or %NULL on error.
112 gpointer CreateEvent(WapiSecurityAttributes *security G_GNUC_UNUSED, gboolean manual,
113 gboolean initial, const guchar *name G_GNUC_UNUSED)
115 struct _WapiHandle_event *event_handle;
119 mono_once (&event_ops_once, event_ops_init);
121 handle=_wapi_handle_new (WAPI_HANDLE_EVENT);
122 if(handle==_WAPI_HANDLE_INVALID) {
123 g_warning (G_GNUC_PRETTY_FUNCTION
124 ": error creating event handle");
128 _wapi_handle_lock_handle (handle);
130 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
131 (gpointer *)&event_handle, NULL);
133 g_warning (G_GNUC_PRETTY_FUNCTION
134 ": error looking up event handle %p", handle);
135 _wapi_handle_unlock_handle (handle);
139 event_handle->manual=manual;
142 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
146 g_message(G_GNUC_PRETTY_FUNCTION ": created new event handle %p",
150 _wapi_handle_unlock_handle (handle);
157 * @handle: The event handle.
159 * Sets the event handle @handle to the signalled state, and then
160 * resets it to unsignalled after informing any waiting threads.
162 * If @handle is a manual reset event, all waiting threads that can be
163 * released immediately are released. @handle is then reset. If
164 * @handle is an auto reset event, one waiting thread is released even
165 * if multiple threads are waiting.
167 * Return value: %TRUE on success, %FALSE otherwise. (Currently only
168 * ever returns %TRUE).
170 gboolean PulseEvent(gpointer handle)
172 struct _WapiHandle_event *event_handle;
175 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
176 (gpointer *)&event_handle, NULL);
178 g_warning (G_GNUC_PRETTY_FUNCTION
179 ": error looking up event handle %p", handle);
183 _wapi_handle_lock_handle (handle);
186 g_message(G_GNUC_PRETTY_FUNCTION ": Pulsing event handle %p", handle);
189 if(event_handle->manual==TRUE) {
190 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
192 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
195 _wapi_handle_unlock_handle (handle);
197 if(event_handle->manual==TRUE) {
198 /* For a manual-reset event, we're about to try and
199 * get the handle lock again, so give other threads a
204 /* Reset the handle signal state */
205 /* I'm not sure whether or not we need a barrier here
206 * to make sure that all threads waiting on the event
207 * have proceeded. Currently we rely on broadcasting
211 g_message(G_GNUC_PRETTY_FUNCTION
212 ": Obtained write lock on event handle %p", handle);
215 _wapi_handle_lock_handle (handle);
216 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
217 _wapi_handle_unlock_handle (handle);
225 * @handle: The event handle.
227 * Resets the event handle @handle to the unsignalled state.
229 * Return value: %TRUE on success, %FALSE otherwise. (Currently only
230 * ever returns %TRUE).
232 gboolean ResetEvent(gpointer handle)
234 struct _WapiHandle_event *event_handle;
237 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
238 (gpointer *)&event_handle, NULL);
240 g_warning (G_GNUC_PRETTY_FUNCTION
241 ": error looking up event handle %p", handle);
246 g_message(G_GNUC_PRETTY_FUNCTION ": Resetting event handle %p",
250 _wapi_handle_lock_handle (handle);
251 if(_wapi_handle_issignalled (handle)==FALSE) {
254 g_message(G_GNUC_PRETTY_FUNCTION
255 ": No need to reset event handle %p", handle);
258 _wapi_handle_unlock_handle (handle);
263 g_message(G_GNUC_PRETTY_FUNCTION
264 ": Obtained write lock on event handle %p", handle);
267 _wapi_handle_set_signal_state (handle, FALSE, FALSE);
269 _wapi_handle_unlock_handle (handle);
276 * @handle: The event handle
278 * Sets the event handle @handle to the signalled state.
280 * If @handle is a manual reset event, it remains signalled until it
281 * is reset with ResetEvent(). An auto reset event remains signalled
282 * until a single thread has waited for it, at which time @handle is
283 * automatically reset to unsignalled.
285 * Return value: %TRUE on success, %FALSE otherwise. (Currently only
286 * ever returns %TRUE).
288 gboolean SetEvent(gpointer handle)
290 struct _WapiHandle_event *event_handle;
293 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_EVENT,
294 (gpointer *)&event_handle, NULL);
296 g_warning (G_GNUC_PRETTY_FUNCTION
297 ": error looking up event handle %p", handle);
301 _wapi_handle_lock_handle (handle);
304 g_message(G_GNUC_PRETTY_FUNCTION ": Setting event handle %p", handle);
307 if(event_handle->manual==TRUE) {
308 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
310 _wapi_handle_set_signal_state (handle, TRUE, FALSE);
313 _wapi_handle_unlock_handle (handle);