hare

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

+linux.ha (1467B)


      1 // License: MPL-2.0
      2 // (c) 2021 Drew DeVault <sir@cmpwn.com>
      3 // (c) 2021 Eyal Sawady <ecs@d2evs.net>
      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: event,
     22 	revents: event,
     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 | errors::error) = {
     42 	let ts = rt::timespec { ... };
     43 	time::duration_to_timespec(timeout, &ts);
     44 	let ts = if (timeout == INDEF) null else &ts;
     45 	match (rt::ppoll(fds: *[*]pollfd: *[*]rt::pollfd, len(fds), ts, null)) {
     46 	case let err: rt::errno =>
     47 		return errors::errno(err);
     48 	case let n: int =>
     49 		return n: uint;
     50 	};
     51 };