[os2]: Implement pipe_has_data procedure

This commit is contained in:
flysand7
2024-09-24 08:12:21 +11:00
parent 2c5be4e054
commit dbad23385d
6 changed files with 111 additions and 1 deletions
+25
View File
@@ -44,3 +44,28 @@ _pipe :: proc() -> (r, w: ^File, err: Error) {
return
}
@(require_results)
_pipe_has_data :: proc(r: ^File) -> (ok: bool, err: Error) {
if r == nil || r.impl == nil {
return false, nil
}
fd := posix.FD((^File_Impl)(r.impl).fd)
poll_fds := []posix.pollfd {
posix.pollfd {
fd = fd,
events = {.IN, .HUP},
},
}
n := posix.poll(raw_data(poll_fds), u32(len(poll_fds)), 0)
if n != 1 {
return false, _get_platform_error()
}
pipe_events := poll_fds[0].revents
if pipe_events >= {.IN} {
return true, nil
}
if pipe_events >= {.HUP} {
return false, .Broken_Pipe
}
return false, nil
}