1-- The blocking provider on Windows: reading and writing, the monotonic
2-- clock and its sleep, and running programs, over platform/windows, as
3-- blocking.ldn, time.ldn and programs.ldn give them over platform/posix.
4-- A file is a Windows handle; a pipe end `system.start` made is read and
5-- written by an overlapped call this thread waits for, and every other
6-- handle by a plain one.
7
8import core/io
9import core/mem
10import core/text
11import core/time
12import hosted/system
13import platform/windows
14import platform/windows/sockets
15
16fixed if compiler.os == windows then
17 -- Keep a failure's code in the system handle's cell.
18 record: (self: host, code: u32) -> none =
19 system.record_errno(self.services, code)
20 end record
21
22 -- The end of what a stream gives: a pipe whose writer has gone, or a
23 -- file read at its end.
24 finished: (code: u32) -> (yes: bool) =
25 yes = code == windows.error_broken_pipe
26 or code == windows.error_handle_eof
27 or code == windows.error_pipe_not_connected
28 end finished
29
30 -- How many bytes one call moves at most: what a DWORD counts, and a
31 -- pipe's buffer for a write.
32 most: usize = 0x40000000
33
34 -- Wait for an overlapped call on `opened` that the system took and did
35 -- not finish, and return what it moved. With no event in the record,
36 -- the handle itself is signalled when the call ends.
37 -- `code` is zero when it succeeded, and the reason otherwise.
38 overlapped_wait: (opened: windows.handle, record_of: ptr mut windows.overlapped)
39 -> (moved: u32, code: u32) =
40 mut got: u32 = 0
41 code = 0
42 if windows.overlapped_result(opened, record_of, addr got, 1) == 0 then
43 code = windows.last_error()
44 end if
45 moved = got
46 end overlapped_wait
47
48 -- Close a handle this provider made for one call, whatever the close
49 -- says.
50 let_go: (made: windows.handle) -> none =
51 _ = windows.close_handle(made)
52 end let_go
53
54 -- Read once into `into` from `opened`: the count, zero at the end.
55 read_once: (self: host, opened: io.file, into: []mut u8)
56 -> (count: usize) ! io.io_failed =
57 made: windows.handle = system.handle_of(opened)
58 mut want: usize = lenof into
59 if want > most then want = most end if
60 mut got: u32 = 0
61 count = 0
62 if system.is_socket(opened) then
63 received: i32 = sockets.receive(made, addr into[0], i32(want), 0)
64 if received < 0 then
65 record(self, sockets.socket_last_error())
66 fail io.io_failed
67 end if
68 got = u32(received)
69 elsif system.is_pipe(opened) then
70 mut asked: windows.overlapped = zeroed
71 if windows.read_file(made, addr into[0], u32(want), addr got,
72 usize(addr asked)) == 0 then
73 code: u32 = windows.last_error()
74 return when finished(code)
75 if code <> windows.error_io_pending then
76 record(self, code)
77 fail io.io_failed
78 end if
79 (moved, code: late) := overlapped_wait(made, addr asked)
80 return when finished(late)
81 if late <> 0 then
82 record(self, late)
83 fail io.io_failed
84 end if
85 got = moved
86 end if
87 elsif windows.read_file(made, addr into[0], u32(want), addr got,
88 0) == 0 then
89 code: u32 = windows.last_error()
90 return when finished(code)
91 record(self, code)
92 fail io.io_failed
93 end if
94 fail io.io_failed when usize(got) > lenof into
95 count = usize(got)
96 end read_once
97
98 -- Write some of `bytes` to `opened` once: the count accepted.
99 write_once: (self: host, opened: io.file, bytes: []u8)
100 -> (count: usize) ! io.io_failed =
101 made: windows.handle = system.handle_of(opened)
102 mut want: usize = lenof bytes
103 if want > most then want = most end if
104 mut put: u32 = 0
105 if system.is_socket(opened) then
106 sent: i32 = sockets.send(made, addr bytes[0], i32(want), 0)
107 if sent < 0 then
108 record(self, sockets.socket_last_error())
109 fail io.io_failed
110 end if
111 put = u32(sent)
112 elsif system.is_pipe(opened) then
113 mut asked: windows.overlapped = zeroed
114 if windows.write_file(made, addr bytes[0], u32(want), addr put,
115 usize(addr asked)) == 0 then
116 code: u32 = windows.last_error()
117 if code <> windows.error_io_pending then
118 record(self, code)
119 fail io.io_failed
120 end if
121 (moved, code: late) := overlapped_wait(made, addr asked)
122 if late <> 0 then
123 record(self, late)
124 fail io.io_failed
125 end if
126 put = moved
127 end if
128 elsif windows.write_file(made, addr bytes[0], u32(want), addr put,
129 0) == 0 then
130 record(self, windows.last_error())
131 fail io.io_failed
132 end if
133 fail io.io_failed when usize(put) > want
134 count = usize(put)
135 end write_once
136
137 blocking_read: (self: host, opened: io.file, into: []mut u8)
138 -> (count: usize) ! io.io_failed =
139 cleared(self)
140 count = 0
141 return when lenof into == 0
142 count = try read_once(self, opened, into)
143 end blocking_read
144
145 write_all: (self: host, opened: io.file, bytes: []u8)
146 -> none ! io.io_failed =
147 mut written: usize = 0
148 while written < lenof bytes do
149 accepted: usize = try write_once(self, opened,
150 bytes[written..<lenof bytes])
151 fail io.io_failed when accepted == 0
152 written += accepted
153 end while
154 end write_all
155
156 blocking_write: (self: host, opened: io.file, bytes: []u8)
157 -> none ! io.io_failed =
158 cleared(self)
159 return when lenof bytes == 0
160 try write_all(self, opened, bytes)
161 end blocking_write
162
163 blocking_write_some: (self: host, opened: io.file, bytes: []u8)
164 -> (count: usize) ! io.io_failed =
165 cleared(self)
166 count = 0
167 return when lenof bytes == 0
168 mut requested: usize = lenof bytes
169 if requested > 65536 then requested = 65536 end if
170 count = try write_once(self, opened, bytes[0..<requested])
171 fail io.io_failed when count == 0
172 end blocking_write_some
173
174 -- A second in nanoseconds.
175 second: u64 = 1000000000
176
177 -- The performance counter's rate, which is fixed while the system
178 -- runs; Windows 10 and later never fail to give it.
179 frequency: () -> (rate: u64) =
180 mut found: i64 = 0
181 _ = windows.performance_frequency(addr found)
182 rate = if found > 0 then u64(found) else 1 end if
183 end frequency
184
185 -- The monotonic clock in nanoseconds since the system started.
186 reading: () -> (at: u64) =
187 mut ticks: i64 = 0
188 _ = windows.performance_counter(addr ticks)
189 rate: u64 = frequency()
190 whole: u64 = u64(ticks)
191 at = (whole / rate) * second + ((whole % rate) * second) / rate
192 end reading
193
194 -- The instant `span` after now, the clock's last instant when that is
195 -- past it.
196 deadline_after: (span: time.duration) -> (deadline: u64) =
197 start: u64 = reading()
198 room: u64 = (0 -% 1) - start
199 deadline = if u64(span) > room then 0 -% 1
200 else start + u64(span) end if
201 end deadline_after
202
203 blocking_now: (self: host) -> (at: time.instant) =
204 _ = self.services
205 at = time.instant(reading())
206 end blocking_now
207
208 -- The longest one sleep asks for, in milliseconds, short of `INFINITE`.
209 longest_sleep: u64 = 0x7FFFFFFF
210
211 -- The deadline is taken first, and the system is asked to sleep again
212 -- for what is left, rounded up to its milliseconds, until the clock
213 -- has reached it, so the sleep is never early.
214 blocking_sleep: (self: host, span: time.duration) -> none =
215 _ = self.services
216 deadline: u64 = deadline_after(span)
217 loop do
218 now: u64 = reading()
219 return when now >= deadline
220 mut left: u64 = (deadline - now + 999999) / 1000000
221 if left > longest_sleep then left = longest_sleep end if
222 windows.sleep(u32(left))
223 end loop
224 end blocking_sleep
225
226 host is time.clock (now: blocking_now, sleep: blocking_sleep)
227
228 blocking_spawn: (self: host, path: text.os_string,
229 arguments: []text.os_string,
230 environment: []text.os_variable,
231 streams: io.child_streams)
232 -> (started: io.child)
233 ! mem.not_found | io.no_access | io.io_failed =
234 started = try system.start(self.services, path, arguments,
235 environment, streams)
236 end blocking_spawn
237
238 -- Close one of a child's pipes, which is closed even when the system
239 -- reports a failure.
240 close_pipe: (self: host, opened: io.file) -> none ! io.io_failed =
241 if windows.close_handle(system.handle_of(opened)) == 0 then
242 record(self, windows.last_error())
243 fail io.io_failed
244 end if
245 end close_pipe
246
247 blocking_write_child: (self: host, inout started: io.child, bytes: []u8)
248 -> none ! io.io_failed =
249 cleared(self)
250 fail io.io_failed when not started.input_open
251 try write_all(self, started.input, bytes)
252 end blocking_write_child
253
254 blocking_close_input: (self: host, inout started: io.child)
255 -> none ! io.io_failed =
256 cleared(self)
257 fail io.io_failed when not started.input_open
258 started.input_open = false
259 try close_pipe(self, started.input)
260 end blocking_close_input
261
262 -- Ask the pipe `opened` to say when it has something: a read of no
263 -- bytes, which ends when there is something to read or the pipe ends,
264 -- and reads nothing. True when it already has.
265 ask_ready: (opened: windows.handle, asked: ptr mut windows.overlapped,
266 spare: ptr mut u8) -> (ready: bool) =
267 mut got: u32 = 0
268 ready = windows.read_file(opened, spare, 0, addr got, usize(asked)) <> 0
269 or windows.last_error() <> windows.error_io_pending
270 end ask_ready
271
272 -- Withdraw a read of no bytes that has not ended, and wait until the
273 -- system has let it go.
274 withdraw: (opened: windows.handle, asked: ptr mut windows.overlapped)
275 -> none =
276 _ = windows.cancel_io(opened, usize(asked))
277 mut got: u32 = 0
278 _ = windows.overlapped_result(opened, asked, addr got, 1)
279 end withdraw
280
281 -- Whether the child's errors, and not its output, is what to read
282 -- next: the output when it has anything, its end included, and the
283 -- errors only when they alone do. Each is asked by a read of no bytes,
284 -- with an event of its own, and the wait is for either.
285 errors_first: (self: host, started: io.child) -> (yes: bool)
286 ! io.io_failed =
287 yes = false
288 output: windows.handle = system.handle_of(started.output)
289 errors: windows.handle = system.handle_of(started.errors)
290 mut spare: [2]u8 = zeroed
291 mut events: [2]windows.handle = zeroed
292 events[0] = windows.create_event(0, 1, 0, 0)
293 events[1] = windows.create_event(0, 1, 0, 0)
294 defer let_go(events[0])
295 defer let_go(events[1])
296 if events[0] == windows.no_handle or events[1] == windows.no_handle then
297 record(self, windows.last_error())
298 fail io.io_failed
299 end if
300 mut first: windows.overlapped = zeroed
301 first.event = events[0]
302 return when ask_ready(output, addr first, addr spare[0])
303 mut second: windows.overlapped = zeroed
304 second.event = events[1]
305 if ask_ready(errors, addr second, addr spare[1]) then
306 withdraw(output, addr first)
307 yes = true
308 return
309 end if
310 answer: u32 = windows.wait_for_any(2, addr events[0], 0,
311 windows.infinite)
312 if answer == windows.wait_object_0 then
313 withdraw(errors, addr second)
314 mut got: u32 = 0
315 _ = windows.overlapped_result(output, addr first, addr got, 1)
316 elsif answer == windows.wait_object_0 + 1 then
317 withdraw(output, addr first)
318 mut got: u32 = 0
319 _ = windows.overlapped_result(errors, addr second, addr got, 1)
320 yes = true
321 else
322 code: u32 = windows.last_error()
323 withdraw(output, addr first)
324 withdraw(errors, addr second)
325 record(self, code)
326 fail io.io_failed
327 end if
328 end errors_first
329
330 blocking_read_child: (self: host, inout started: io.child, into: []mut u8)
331 -> (taken: io.child_read) ! io.io_failed =
332 cleared(self)
333 taken = (count: 0, stream: io.child_output,
334 finished: not started.output_open and not started.errors_open)
335 return when lenof into == 0
336 loop do
337 if not started.output_open and not started.errors_open then
338 taken = (count: 0, stream: io.child_output, finished: true)
339 return
340 end if
341 mut from_errors: bool = not started.output_open
342 if started.output_open and started.errors_open then
343 from_errors = try errors_first(self, started)
344 end if
345 opened: io.file = if from_errors then started.errors
346 else started.output end if
347 count: usize = try read_once(self, opened, into)
348 if count > 0 then
349 taken = (count: count,
350 stream: if from_errors then io.child_errors
351 else io.child_output end if,
352 finished: false)
353 return
354 end if
355 if from_errors then
356 started.errors_open = false
357 else
358 started.output_open = false
359 end if
360 try close_pipe(self, opened)
361 end loop
362 end blocking_read_child
363
364 -- Close every pipe the child still has, keeping the first failure.
365 close_pipes: (self: host, started: io.child) -> (closed: bool) =
366 closed = true
367 if started.input_open then
368 close_pipe(self, started.input) else (problem)
369 _ = problem
370 closed = false
371 end
372 end if
373 if started.output_open then
374 close_pipe(self, started.output) else (problem)
375 _ = problem
376 closed = false
377 end
378 end if
379 if started.errors_open then
380 close_pipe(self, started.errors) else (problem)
381 _ = problem
382 closed = false
383 end
384 end if
385 end close_pipes
386
387 -- The pipes are closed before the wait, so a child still writing to
388 -- one meets a closed pipe rather than waiting for a reader that never
389 -- comes. The child's handle is closed once its code is read.
390 blocking_wait: (self: host, sink started: io.child)
391 -> (ended: io.ending) ! io.io_failed =
392 cleared(self)
393 closed: bool = close_pipes(self, started)
394 child: windows.handle = system.process_of(started)
395 defer let_go(child)
396 if windows.wait_for(child, windows.infinite) == windows.wait_failed then
397 record(self, windows.last_error())
398 fail io.io_failed
399 end if
400 mut code: u32 = 0
401 if windows.exit_code_of(child, addr code) == 0 then
402 record(self, windows.last_error())
403 fail io.io_failed
404 end if
405 ended = system.ended(self.services, code)
406 fail io.io_failed when not closed
407 end blocking_wait
408
409 host is io.programs
410 (spawn: blocking_spawn, write_child: blocking_write_child,
411 close_input: blocking_close_input, read_child: blocking_read_child,
412 wait: blocking_wait)
413end if