[project @ 2005-10-21 15:14:06 by simonmar]
[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 "AwaitEvent.h"
18 #include <windows.h>
19 #include "win32/AsyncIO.h"
20
21 // Used to avoid calling abandonRequestWait() if we don't need to.
22 // Protected by sched_mutex.
23 static nat workerWaitingForRequests = 0;
24
25 void
26 awaitEvent(rtsBool wait)
27 {
28   int ret;
29
30   do {
31     /* Try to de-queue completed IO requests
32      */
33     workerWaitingForRequests = 1;
34     ret = awaitRequests(wait);
35     workerWaitingForRequests = 0;
36     if (!ret) { 
37       return; /* still hold the lock */
38     }
39
40     // Return to the scheduler if:
41     //
42     //  - we were interrupted
43     //  - new threads have arrived
44
45   } while (wait
46            && !interrupted
47            && emptyRunQueue(&MainCapability)
48       );
49 }