Skip to content

Commit 50b4720

Browse files
authored
Merge pull request #3681 from youknowone/wasi-select
Add wasi select, FD_SET, FD_ZERO, FD_ISSET
2 parents bcd5546 + e9da8a0 commit 50b4720

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

libc-test/semver/wasi.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fd_set
2+
FD_SET
3+
FD_ZERO
4+
FD_ISSET
5+
select

src/wasi.rs

+35
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ s! {
178178
pub st_ctim: timespec,
179179
__reserved: [c_longlong; 3],
180180
}
181+
182+
pub struct fd_set {
183+
__nfds: usize,
184+
__fds: [c_int; FD_SETSIZE as usize],
185+
}
181186
}
182187

183188
// Declare dirent outside of s! so that it doesn't implement Copy, Eq, Hash,
@@ -446,6 +451,28 @@ pub const NOEXPR: ::nl_item = 0x50001;
446451
pub const YESSTR: ::nl_item = 0x50002;
447452
pub const NOSTR: ::nl_item = 0x50003;
448453

454+
f! {
455+
pub fn FD_ISSET(fd: ::c_int, set: *const fd_set) -> bool {
456+
let set = &*set;
457+
let n = set.__nfds;
458+
return set.__fds[..n].iter().any(|p| *p == fd)
459+
}
460+
461+
pub fn FD_SET(fd: ::c_int, set: *mut fd_set) -> () {
462+
let set = &mut *set;
463+
let n = set.__nfds;
464+
if !set.__fds[..n].iter().any(|p| *p == fd) {
465+
set.__nfds = n + 1;
466+
set.__fds[n] = fd;
467+
}
468+
}
469+
470+
pub fn FD_ZERO(set: *mut fd_set) -> () {
471+
(*set).__nfds = 0;
472+
return
473+
}
474+
}
475+
449476
#[cfg_attr(
450477
feature = "rustc-dep-of-std",
451478
link(
@@ -741,6 +768,14 @@ extern "C" {
741768
pub fn nl_langinfo(item: ::nl_item) -> *mut ::c_char;
742769
pub fn nl_langinfo_l(item: ::nl_item, loc: ::locale_t) -> *mut ::c_char;
743770

771+
pub fn select(
772+
nfds: c_int,
773+
readfds: *mut fd_set,
774+
writefds: *mut fd_set,
775+
errorfds: *mut fd_set,
776+
timeout: *const timeval,
777+
) -> c_int;
778+
744779
pub fn __wasilibc_register_preopened_fd(fd: c_int, path: *const c_char) -> c_int;
745780
pub fn __wasilibc_fd_renumber(fd: c_int, newfd: c_int) -> c_int;
746781
pub fn __wasilibc_unlinkat(fd: c_int, path: *const c_char) -> c_int;

0 commit comments

Comments
 (0)