Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
fd_poll_write.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2026 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
21
23 /* Pops timeout_ms:int, fd:int; pushes 1 if writable, 0 on timeout, -1 on error */
24 Value to = pop_value(vm);
25 Value fdv = pop_value(vm);
26 int rc = -1;
27#ifdef __unix__
28 if (fdv.type != VAL_INT || to.type != VAL_INT) {
29 fprintf(stderr, "Runtime type error: fd_poll_write expects (int fd, int timeout_ms)\n");
30 rc = -1;
31 } else {
32 int fd = (int)fdv.i;
33 int timeout_ms = (int)to.i;
34 struct pollfd pfd;
35 pfd.fd = fd;
36 pfd.events = POLLOUT;
37 pfd.revents = 0;
38 int pr = poll(&pfd, 1, timeout_ms);
39 if (pr > 0) {
40 if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
41 rc = 0; /* treat as not writable */
42 } else if (pfd.revents & POLLOUT) {
43 rc = 1;
44 } else {
45 rc = 0;
46 }
47 } else if (pr == 0) {
48 rc = 0; /* timeout */
49 } else {
50 rc = -1; /* error */
51 }
52 }
53#else
54 (void)to; (void)fdv;
55#endif
59 break;
60}
@ OP_FD_POLL_WRITE
Definition bytecode.h:232
void to
Value fdv
int rc
free_value(to)
push_value(vm, make_int(rc))
int fd
Definition serial_open.c:92
Tagged union representing a Fun value.
Definition value.h:68
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_INT
Definition value.h:51
#define fprintf
Definition vm.c:200