Massive patch for the first months work adding System FC to GHC #35
[ghc-hetmet.git] / rts / win32 / AwaitEvent.c
1 #if !defined(THREADED_RTS) /* to the end */
2 /*
3  * Wait/check for external events. Periodically, the
4  * Scheduler checks for the completion of external operations,
5  * like the expiration of timers, completion of I/O requests
6  * issued by Haskell threads.
7  *
8  * If the Scheduler is otherwise out of work, it'll block
9  * herein waiting for external events to occur.
10  *
11  * This file mirrors the select()-based functionality 
12  * for POSIX / Unix platforms in rts/Select.c, but for
13  * Win32.
14  *
15  */
16 #include "Rts.h"
17 #include "Schedule.h"
18 #include "AwaitEvent.h"
19 #include <windows.h>
20 #include "win32/AsyncIO.h"
21
22 // Used to avoid calling abandonRequestWait() if we don't need to.
23 // Protected by sched_mutex.
24 static nat workerWaitingForRequests = 0;
25
26 void
27 awaitEvent(rtsBool wait)
28 {
29   int ret;
30
31   do {
32     /* Try to de-queue completed IO requests
33      */
34     workerWaitingForRequests = 1;
35     ret = awaitRequests(wait);
36     workerWaitingForRequests = 0;
37     if (!ret) { 
38       return; /* still hold the lock */
39     }
40
41     // Return to the scheduler if:
42     //
43     //  - we were interrupted
44     //  - new threads have arrived
45
46   } while (wait
47            && sched_state == SCHED_RUNNING
48            && emptyRunQueue(&MainCapability)
49       );
50 }
51 #endif