[project @ 2003-02-22 04:51:50 by sof]
[ghc-hetmet.git] / ghc / rts / win32 / AwaitEvent.c
1 /*
2  * Wait/check for external events. Periodically, the
3  * Scheduler checks for the completion of external operations,
4  * like the expiration of timers, completion of I/O requests
5  * issued by Haskell threads.
6  *
7  * If the Scheduler is otherwise out of work, it'll block
8  * herein waiting for external events to occur.
9  *
10  * This file mirrors the select()-based functionality 
11  * for POSIX / Unix platforms in rts/Select.c, but for
12  * Win32.
13  *
14  */
15 #include "Rts.h"
16 #include "Schedule.h"
17 #include <windows.h>
18 #include "win32/AsyncIO.h"
19
20 void
21 awaitEvent(rtsBool wait)
22 {
23   RELEASE_LOCK(&sched_mutex);
24   do {
25     /* Try to de-queue completed IO requests */
26     if (!awaitRequests(wait)) {
27       return;
28     }
29     ACQUIRE_LOCK(&sched_mutex);
30     /* we were interrupted, return to the scheduler immediately.
31      */
32     if (interrupted) {
33       return; /* still hold the lock */
34     }
35
36     /* If new runnable threads have arrived, stop waiting for
37      * I/O and run them.
38      */
39     if (run_queue_hd != END_TSO_QUEUE) {
40       return; /* still hold the lock */
41     }
42
43 #ifdef RTS_SUPPORTS_THREADS
44     /* If another worker thread wants to take over,
45      * return to the scheduler
46      */
47     if (needToYieldToReturningWorker()) {
48       return; /* still hold the lock */
49     }
50 #endif
51     RELEASE_LOCK(&sched_mutex);
52   } while (wait && !interrupted && run_queue_hd == END_TSO_QUEUE);
53 }
54
55 #ifdef RTS_SUPPORTS_THREADS
56 void
57 wakeBlockedWorkerThread()
58 {
59   abandonRequestWait();
60 }
61 #endif
62