commit 716ca86255b01b725613fb366fed13364668f422
parent 84c28db4dc003377b02e2f0272c816fd94c2c7b3
Author: Lorenz (xha) <me@xha.li>
Date: Sat, 25 Nov 2023 15:18:16 +0100
OpenBSD: add unix::poll
Signed-off-by: Lorenz (xha) <me@xha.li>
Diffstat:
1 file changed, 51 insertions(+), 0 deletions(-)
diff --git a/unix/poll/+openbsd.ha b/unix/poll/+openbsd.ha
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: MPL-2.0
+// (c) Hare authors <https://harelang.org>
+
+use errors;
+use io;
+use rt;
+use time;
+
+// Events bitfield for the events and revents field of [[pollfd]].
+export type event = enum i16 {
+ POLLIN = 1,
+ POLLPRI = 2,
+ POLLOUT = 4,
+ POLLERR = 8,
+ POLLHUP = 16,
+};
+
+// A single file descriptor to be polled.
+export type pollfd = struct {
+ fd: io::file,
+ events: i16,
+ revents: i16,
+};
+
+// Pass this [[time::duration]] to [[poll]] to cause it wait indefinitely for
+// the next event.
+export def INDEF: time::duration = -1;
+
+// Pass this [[time::duration]] to [[poll]] to cause it to return immediately if
+// no events are available.
+export def NONBLOCK: time::duration = 0;
+
+// Polls for the desired events on a slice of [[pollfd]]s, blocking until an
+// event is available, or the timeout expires. Set the timeout to [[INDEF]] to
+// block forever, or [[NONBLOCK]] to return immediately if no events are
+// available. Returns the number of [[pollfd]] items which have events, i.e.
+// those which have revents set to a nonzero value.
+export fn poll(
+ fds: []pollfd,
+ timeout: time::duration,
+) (uint | error) = {
+ let ts = rt::timespec { ... };
+ time::duration_to_timespec(timeout, &ts);
+ let ts = if (timeout == INDEF) null else &ts;
+ match (rt::ppoll(fds: *[*]pollfd: *[*]rt::pollfd, len(fds): rt::nfds_t, ts, null)) {
+ case let err: rt::errno =>
+ return errors::errno(err);
+ case let n: int =>
+ return n: uint;
+ };
+};