1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/* -----------------------------------------------------------------------------
*
* (c) The GHC Team, 1998-2006
*
* The IO manager thread in THREADED_RTS.
* See also libraries/base/GHC/Conc.hs.
*
* NOTE: This is used by both MIO and WINIO
* ---------------------------------------------------------------------------*/
#include "Rts.h"
#include "ThrIOManager.h"
#include "MIOManager.h"
#include "rts\OSThreads.h"
#include "Prelude.h"
#include <windows.h>
// Here's the Event that we use to wake up the IO manager thread
static HANDLE io_manager_event = INVALID_HANDLE_VALUE;
#define EVENT_BUFSIZ 256
// We lock using OS_ACQUIRE_LOCK the ensure the non-threaded WINIO
// C thread does not race with the scheduler code which can also
// access the event queue via FFI.
Mutex event_buf_mutex;
StgWord32 event_buf[EVENT_BUFSIZ];
uint32_t next_event;
/*Creates the IO Managers event object.
Idempotent after first call.
*/
HANDLE
getIOManagerEvent (void)
{
HANDLE hRes;
OS_ACQUIRE_LOCK(&event_buf_mutex);
if (io_manager_event == INVALID_HANDLE_VALUE) {
hRes = CreateEvent ( NULL, // no security attrs
true, // manual reset
false, // initial state,
NULL ); // event name: NULL for private events
if (hRes == NULL) {
sysErrorBelch("getIOManagerEvent");
stg_exit(EXIT_FAILURE);
}
io_manager_event = hRes;
} else {
hRes = io_manager_event;
}
OS_RELEASE_LOCK(&event_buf_mutex);
return hRes;
}
HsWord32
readIOManagerEvent (void)
{
HsWord32 res;
OS_ACQUIRE_LOCK(&event_buf_mutex);
if (io_manager_event != INVALID_HANDLE_VALUE) {
if (next_event == 0) {
res = 0; // no event to return
} else {
do {
// Dequeue as many wakeup events as possible.
res = (HsWord32)(event_buf[--next_event]);
} while (res == IO_MANAGER_WAKEUP && next_event);
if (next_event == 0) {
if (!ResetEvent(io_manager_event)) {
sysErrorBelch("readIOManagerEvent");
stg_exit(EXIT_FAILURE);
}
}
}
} else {
res = 0;
}
OS_RELEASE_LOCK(&event_buf_mutex);
//debugBelch("readIOManagerEvent: %d\n", res);
return res;
}
void
sendIOManagerEvent (HsWord32 event)
{
OS_ACQUIRE_LOCK(&event_buf_mutex);
//debugBelch("sendIOManagerEvent: %d to %p\n", event, io_manager_event);
if (io_manager_event != INVALID_HANDLE_VALUE) {
if (next_event == EVENT_BUFSIZ) {
errorBelch("event buffer overflowed; event dropped");
} else {
event_buf[next_event++] = (StgWord32)event;
if (!SetEvent(io_manager_event)) {
sysErrorBelch("sendIOManagerEvent: SetEvent");
stg_exit(EXIT_FAILURE);
}
}
}
OS_RELEASE_LOCK(&event_buf_mutex);
}
void
interruptIOManagerEvent (void)
{
if (is_io_mng_native_p ()) {
OS_ACQUIRE_LOCK(&event_buf_mutex);
/* How expensive is this??. */
Capability *cap;
cap = rts_lock();
rts_evalIO(&cap, interruptIOManager_closure, NULL);
rts_unlock(cap);
OS_RELEASE_LOCK(&event_buf_mutex);
}
}
void
ioManagerWakeup (void)
{
sendIOManagerEvent(IO_MANAGER_WAKEUP);
}
void
ioManagerDie (void)
{
sendIOManagerEvent(IO_MANAGER_DIE);
// IO_MANAGER_DIE must be idempotent, as it is called
// repeatedly by shutdownCapability(). Try conc059(threaded1) to
// illustrate the problem.
OS_ACQUIRE_LOCK(&event_buf_mutex);
io_manager_event = INVALID_HANDLE_VALUE;
OS_RELEASE_LOCK(&event_buf_mutex);
// ToDo: wait for the IO manager to pick up the event, and
// then release the Event and Mutex objects we've allocated.
}
void
ioManagerStart (void)
{
#if defined(THREADED_RTS)
initMutex(&event_buf_mutex);
#endif
next_event = 0;
// Make sure the IO manager thread is running
Capability *cap;
if (io_manager_event == INVALID_HANDLE_VALUE) {
cap = rts_lock();
rts_evalIO(&cap, ensureIOManagerIsRunning_closure, NULL);
rts_unlock(cap);
}
}
|