Skip to content

Commit ba6e9f2

Browse files
committed
Port poll() to use wasip2 poll method in all cases
At this point, there should be no further uses of the preview1 component adapter for polling methods (__wasi_poll_oneoff()) when __wasilibc_use_wasip2 is defined.
1 parent 9285679 commit ba6e9f2

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

libc-bottom-half/cloudlibc/src/libc/poll/poll.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ int poll(struct pollfd* fds, nfds_t nfds, int timeout)
140140
for (size_t i = 0; i < nfds; ++i) {
141141
descriptor_table_entry_t* entry;
142142
if (descriptor_table_get_ref(fds[i].fd, &entry)) {
143-
found_socket = true;
143+
if (entry->tag == DESCRIPTOR_TABLE_ENTRY_TCP_SOCKET
144+
|| entry->tag == DESCRIPTOR_TABLE_ENTRY_UDP_SOCKET)
145+
found_socket = true;
146+
else
147+
found_non_socket = true;
144148
} else {
145149
found_non_socket = true;
146150
}
@@ -155,16 +159,8 @@ int poll(struct pollfd* fds, nfds_t nfds, int timeout)
155159
errno = ENOTSUP;
156160
return -1;
157161
}
158-
159-
return poll_wasip2(fds, nfds, timeout);
160-
} else if (found_non_socket) {
161-
return poll_wasip1(fds, nfds, timeout);
162-
} else if (timeout >= 0) {
163-
return poll_wasip2(fds, nfds, timeout);
164-
} else {
165-
errno = ENOTSUP;
166-
return -1;
167162
}
163+
return poll_wasip2(fds, nfds, timeout);
168164
}
169165
#else // not __wasilibc_use_wasip2
170166
int poll(struct pollfd* fds, nfds_t nfds, int timeout)

libc-bottom-half/sources/poll-wasip2.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,42 @@ int poll_wasip2(struct pollfd *fds, size_t nfds, int timeout)
141141
}
142142
break;
143143
}
144-
144+
case DESCRIPTOR_TABLE_ENTRY_FILE_STREAM: {
145+
file_stream_t stream = entry->stream;
146+
if ((pollfd->events & POLLRDNORM) != 0) {
147+
if (!stream.file_info.readable) {
148+
errno = EBADF;
149+
return -1;
150+
}
151+
streams_own_pollable_t input_stream_pollable =
152+
streams_method_input_stream_subscribe(stream.read_stream);
153+
states[state_index++] = (state_t) {
154+
.pollable = input_stream_pollable,
155+
.pollfd = pollfd,
156+
.entry = entry,
157+
.events = pollfd->events
158+
};
159+
}
160+
if ((pollfd->events & POLLWRNORM) != 0) {
161+
if (!stream.file_info.writable) {
162+
errno = EBADF;
163+
return -1;
164+
}
165+
streams_own_pollable_t output_stream_pollable =
166+
streams_method_output_stream_subscribe(stream.write_stream);
167+
states[state_index++] = (state_t){
168+
.pollable = output_stream_pollable,
169+
.pollfd = pollfd,
170+
.entry = entry,
171+
.events = pollfd->events
172+
};
173+
}
174+
break;
175+
}
176+
// File must be open
177+
case DESCRIPTOR_TABLE_ENTRY_FILE_HANDLE:
178+
errno = EBADF;
179+
return -1;
145180
default:
146181
errno = ENOTSUP;
147182
return -1;
@@ -238,7 +273,14 @@ int poll_wasip2(struct pollfd *fds, size_t nfds, int timeout)
238273
}
239274
state->pollfd->revents |= state->events;
240275
}
241-
} else {
276+
} else if (state->entry->tag == DESCRIPTOR_TABLE_ENTRY_FILE_STREAM) {
277+
poll_pollable_drop_own(state->pollable);
278+
if (state->pollfd->revents == 0) {
279+
++event_count;
280+
}
281+
state->pollfd->revents |= state->events;
282+
}
283+
else {
242284
if (state->pollfd->revents == 0) {
243285
++event_count;
244286
}

test/src/misc/poll.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! add-flags.py(RUN): --dir fs::/
2+
3+
// Modified from libc-test to not use tmpfile or /dev/null
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <fcntl.h>
8+
#include <poll.h>
9+
#include "test.h"
10+
11+
#define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
12+
13+
int main(void)
14+
{
15+
char tmp[] = "testsuite-XXXXXX";
16+
int fd;
17+
int flags = 0;
18+
19+
// Not a very useful test, but it's hard to test poll() without threads
20+
21+
TEST((fd = open(tmp, flags | O_RDWR | O_CREAT | O_EXCL, 0600)) > 2);
22+
TEST(write(fd, "hello", 6)==6);
23+
24+
struct pollfd poll_fd = { .fd = fd, .events = POLLRDNORM, .revents = 0 };
25+
TEST(poll(&poll_fd, 1, 1)==1);
26+
27+
poll_fd.events = POLLWRNORM;
28+
TEST(poll(&poll_fd, 1, 1)==1);
29+
30+
close(fd);
31+
32+
if (fd > 2)
33+
TEST(unlink(tmp) != -1);
34+
35+
return t_status;
36+
}

0 commit comments

Comments
 (0)