a8f73fff7a13260a0c3154040132dfb4ceb39308
[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 #if defined(RTS_SUPPORTS_THREADS)
20 #include "Capability.h"
21 #endif
22
23 void
24 awaitEvent(rtsBool wait)
25 {
26   RELEASE_LOCK(&sched_mutex);
27   do {
28     /* Try to de-queue completed IO requests */
29     if (!awaitRequests(wait)) {
30       return;
31     }
32     ACQUIRE_LOCK(&sched_mutex);
33     /* we were interrupted, return to the scheduler immediately.
34      */
35     if (interrupted) {
36       return; /* still hold the lock */
37     }
38
39     /* If new runnable threads have arrived, stop waiting for
40      * I/O and run them.
41      */
42     if (run_queue_hd != END_TSO_QUEUE) {
43       return; /* still hold the lock */
44     }
45
46 #ifdef RTS_SUPPORTS_THREADS
47     /* If another worker thread wants to take over,
48      * return to the scheduler
49      */
50     if (needToYieldToReturningWorker()) {
51       return; /* still hold the lock */
52     }
53 #endif
54     RELEASE_LOCK(&sched_mutex);
55   } while (wait && !interrupted && run_queue_hd == END_TSO_QUEUE);
56 }
57
58 #ifdef RTS_SUPPORTS_THREADS
59 void
60 wakeBlockedWorkerThread()
61 {
62   abandonRequestWait();
63 }
64 #endif
65