summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/cloudlibc/src/libc/poll/poll.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc-bottom-half/cloudlibc/src/libc/poll/poll.c')
-rw-r--r--libc-bottom-half/cloudlibc/src/libc/poll/poll.c47
1 files changed, 46 insertions, 1 deletions
diff --git a/libc-bottom-half/cloudlibc/src/libc/poll/poll.c b/libc-bottom-half/cloudlibc/src/libc/poll/poll.c
index cde4e81..0ee1479 100644
--- a/libc-bottom-half/cloudlibc/src/libc/poll/poll.c
+++ b/libc-bottom-half/cloudlibc/src/libc/poll/poll.c
@@ -7,7 +7,7 @@
#include <poll.h>
#include <stdbool.h>
-int poll(struct pollfd *fds, size_t nfds, int timeout) {
+static int poll_wasip1(struct pollfd *fds, size_t nfds, int timeout) {
// Construct events for poll().
size_t maxevents = 2 * nfds + 1;
__wasi_subscription_t subscriptions[maxevents];
@@ -127,3 +127,48 @@ int poll(struct pollfd *fds, size_t nfds, int timeout) {
}
return retval;
}
+
+#ifdef __wasilibc_use_wasip2
+#include <wasi/descriptor_table.h>
+
+int poll_wasip2(struct pollfd *fds, size_t nfds, int timeout);
+
+int poll(struct pollfd* fds, nfds_t nfds, int timeout)
+{
+ bool found_socket = false;
+ bool found_non_socket = false;
+ for (size_t i = 0; i < nfds; ++i) {
+ descriptor_table_entry_t* entry;
+ if (descriptor_table_get_ref(fds[i].fd, &entry)) {
+ found_socket = true;
+ } else {
+ found_non_socket = true;
+ }
+ }
+
+ if (found_socket) {
+ if (found_non_socket) {
+ // We currently don't support polling a mix of non-sockets and
+ // sockets here (though you can do it by using the host APIs
+ // directly), and we probably won't until we've migrated entirely to
+ // WASI 0.2.
+ errno = ENOTSUP;
+ return -1;
+ }
+
+ return poll_wasip2(fds, nfds, timeout);
+ } else if (found_non_socket) {
+ return poll_wasip1(fds, nfds, timeout);
+ } else if (timeout >= 0) {
+ return poll_wasip2(fds, nfds, timeout);
+ } else {
+ errno = ENOTSUP;
+ return -1;
+ }
+}
+#else // not __wasilibc_use_wasip2
+int poll(struct pollfd* fds, nfds_t nfds, int timeout)
+{
+ return poll_wasip1(fds, nfds, timeout);
+}
+#endif // not __wasilibc_use_wasip2