Skip to content
← Back

src/analysis/request_reader.ghul

1
@@IF.dotnet()
2
3
namespace Analysis is
4
use System.Exception
5
use System.Threading.Monitor
6
7
// A frame the reader thread lifted off the client's pipe, or the
8
// reason it could not. Deserialization happens on the reader thread,
9
// so a malformed line becomes a queued PARSE_ERROR rather than an
10
// exception raised at whatever point the despatcher happened to read.
11
//
12
// TIMED_OUT is not read off the pipe at all: it is what a bounded
13
// wait returns when nothing arrived, which is what the idle timeout
14
// is decided from.
15
union QueuedFrame is
16
REQUEST(request: Protocol.Request)
17
PARSE_ERROR(message: string)
18
END_OF_INPUT
19
TIMED_OUT
20
si
21
22
// Reads request frames off the client's pipe on a thread of its own
23
// and queues them, so the despatcher can see what is waiting instead
24
// of only what it has already asked for.
25
//
26
// That is what lets a query be answered during a compile: a handler
27
// running on the main thread can ask whether anything is queued and
28
// take it. Nothing else about the analyser becomes concurrent -
29
// every request is still handled on the main thread, one at a time,
30
// and this class touches no compiler state.
31
class REQUEST_READER(_reader: IO.TextReader) is
32
_gate: object
33
_frames: Collections.LIST[QueuedFrame]
34
35
init(..) is
36
_gate = object()
37
_frames = Collections.LIST[QueuedFrame]()
38
si
39
40
start() is
41
let start: System.Threading.ThreadStart = () is _read_loop(); si
42
43
let thread = System.Threading.Thread(start)
44
45
// A background thread does not hold the process open, which
46
// is what makes the idle exit and the watchdog recycle able
47
// to end the process while this thread sits in read_line.
48
thread.is_background = true
49
thread.name = "ghul-analysis-reader"
50
51
thread.start()
52
si
53
54
_read_loop() is
55
do
56
let line: string? mut = null
57
58
try
59
line = _reader.read_line()
60
catch ex: Exception
61
// The pipe went away under us; that is end of input
62
// by another name.
63
line = null
64
yrt
65
66
if !line? then
67
_push(QueuedFrame.END_OF_INPUT)
68
69
return
70
fi
71
72
if string.is_null_or_white_space(line) then
73
continue
74
fi
75
76
_push(_deserialize(line))
77
od
78
si
79
80
_deserialize(line: string) -> QueuedFrame static is
81
try
82
let request = Protocol.JSON_PROTOCOL.deserialize_request(line)
83
84
if !request? then
85
return QueuedFrame.PARSE_ERROR("request line deserialized to nothing")
86
fi
87
88
return QueuedFrame.REQUEST(request)
89
catch ex: Exception
90
return QueuedFrame.PARSE_ERROR("{ex.get_type().name}: {ex.message}")
91
yrt
92
si
93
94
_push(frame: QueuedFrame) is
95
Monitor.enter(_gate)
96
97
try
98
_frames.add(frame)
99
100
Monitor.pulse_all(_gate)
101
finally
102
Monitor.exit(_gate)
103
yrt
104
si
105
106
// Wait for the next frame, giving up after timeout_ms with
107
// TIMED_OUT; a timeout of zero or less waits indefinitely.
108
//
109
// END_OF_INPUT is answered to every later call as well as to the
110
// one that reached it, so a caller that keeps asking keeps being
111
// told the same thing rather than blocking forever.
112
take(timeout_ms: int) -> QueuedFrame is
113
Monitor.enter(_gate)
114
115
try
116
while _frames.count == 0 do
117
if timeout_ms <= 0 then
118
Monitor.wait(_gate)
119
elif !Monitor.wait(_gate, timeout_ms) then
120
return QueuedFrame.TIMED_OUT
121
fi
122
od
123
124
let head = _frames[0]
125
126
if isa QueuedFrame.END_OF_INPUT(head) then
127
return head
128
fi
129
130
_frames.remove_at(0)
131
132
return head
133
finally
134
Monitor.exit(_gate)
135
yrt
136
si
137
138
// Take the queued request at the head of the queue when there is
139
// one and `wanted` accepts it, and nothing otherwise. Never
140
// waits, and never reorders: a frame the caller does not want
141
// stops the scan, so requests are still handled in the order the
142
// client sent them.
143
take_if(wanted: (Protocol.Request) -> bool) -> Protocol.Request? is
144
Monitor.enter(_gate)
145
146
try
147
if _frames.count == 0 then
148
return null
149
fi
150
151
let head = _frames[0]
152
153
if let queued: QueuedFrame.REQUEST = head /\ wanted(queued.request) then
154
_frames.remove_at(0)
155
156
return queued.request
157
fi
158
159
return null
160
finally
161
Monitor.exit(_gate)
162
yrt
163
si
164
165
// Whether anything `wanted` accepts is queued anywhere, not only at
166
// the head. Never waits and takes nothing: this answers "is there
167
// something behind me that changes what I am doing", which a
168
// long-running handler asks without wanting to handle it yet.
169
has_pending(wanted: (Protocol.Request) -> bool) -> bool is
170
Monitor.enter(_gate)
171
172
try
173
for frame in _frames do
174
if let queued: QueuedFrame.REQUEST = frame /\ wanted(queued.request) then
175
return true
176
fi
177
od
178
179
return false
180
finally
181
Monitor.exit(_gate)
182
yrt
183
si
184
si
185
si