hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

+freebsd.ha (1418B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use errors;
      5 use io;
      6 use rt;
      7 use time;
      8 
      9 // Events bitfield for the events and revents field of [[pollfd]].
     10 export type event = enum i16 {
     11 	POLLIN = 1,
     12 	POLLPRI = 2,
     13 	POLLOUT = 4,
     14 	POLLERR = 8,
     15 	POLLHUP = 16,
     16 };
     17 
     18 // A single file descriptor to be polled.
     19 export type pollfd = struct {
     20 	fd: io::file,
     21 	events: i16,
     22 	revents: i16,
     23 };
     24 
     25 // Pass this [[time::duration]] to [[poll]] to cause it wait indefinitely for
     26 // the next event.
     27 export def INDEF: time::duration = -1;
     28 
     29 // Pass this [[time::duration]] to [[poll]] to cause it to return immediately if
     30 // no events are available.
     31 export def NONBLOCK: time::duration = 0;
     32 
     33 // Polls for the desired events on a slice of [[pollfd]]s, blocking until an
     34 // event is available, or the timeout expires. Set the timeout to [[INDEF]] to
     35 // block forever, or [[NONBLOCK]] to return immediately if no events are
     36 // available. Returns the number of [[pollfd]] items which have events, i.e.
     37 // those which have revents set to a nonzero value.
     38 export fn poll(
     39 	fds: []pollfd,
     40 	timeout: time::duration,
     41 ) (uint | error) = {
     42 	let ts = time::duration_to_timespec(timeout);
     43 	let ts = if (timeout == INDEF) null else &ts;
     44 	match (rt::ppoll(fds: *[*]pollfd: *[*]rt::pollfd, len(fds): rt::nfds_t, ts, null)) {
     45 	case let err: rt::errno =>
     46 		return errors::errno(err);
     47 	case let n: int =>
     48 		return n: uint;
     49 	};
     50 };