Skip to content

Commit d179363

Browse files
authored
Port polling operations to wasip2 (#608)
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 b33c5fc commit d179363

File tree

13 files changed

+459
-283
lines changed

13 files changed

+459
-283
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/cloudlibc/src/libc/sys/ioctl/ioctl.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ int ioctl(int fildes, int request, ...) {
5757
return -1;
5858
}
5959
}
60-
60+
// TODO: In particular, this doesn't support using FIONREAD
61+
// with file descriptors
6162
default:
6263
errno = ENOPROTOOPT;
6364
return -1;
@@ -111,11 +112,6 @@ int ioctl(int fildes, int request, ...) {
111112
return 0;
112113
}
113114
case FIONBIO: {
114-
#ifdef __wasilibc_use_wasip2
115-
// wasip2 doesn't support setting the non-blocking flag
116-
errno = ENOTSUP;
117-
return -1;
118-
#else
119115
// Obtain the current file descriptor flags.
120116
__wasi_fdstat_t fds;
121117
__wasi_errno_t error = __wasi_fd_fdstat_get(fildes, &fds);
@@ -140,7 +136,6 @@ int ioctl(int fildes, int request, ...) {
140136
return -1;
141137
}
142138
return 0;
143-
#endif
144139
}
145140
default:
146141
// Invalid request.

libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,22 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
2323
if ((flags & ~TIMER_ABSTIME) != 0)
2424
return EINVAL;
2525

26-
// Prepare polling subscription.
27-
__wasi_subscription_t sub = {
28-
.u.tag = __WASI_EVENTTYPE_CLOCK,
29-
.u.u.clock.id = clock_id->id,
30-
.u.u.clock.flags = flags,
31-
};
32-
// Convert to wall_clock_datetime_t
33-
wall_clock_datetime_t timeout;
34-
if (!timespec_to_timestamp_clamp(rqtp, &timeout))
35-
return EINVAL;
36-
sub.u.u.clock.timeout = (timeout.seconds * NSEC_PER_SEC)
37-
+ timeout.nanoseconds;
26+
// Note: rmtp is ignored
27+
28+
if (clock_id != CLOCK_MONOTONIC) {
29+
// wasip2 only provides a pollable for monotonic clocks
30+
return ENOTSUP;
31+
}
32+
33+
// Prepare pollable
34+
int64_t duration = (rqtp->tv_sec * NSEC_PER_SEC) + rqtp->tv_nsec;
35+
monotonic_clock_own_pollable_t pollable = monotonic_clock_subscribe_duration(duration);
3836

3937
// Block until polling event is triggered.
40-
size_t nevents;
41-
__wasi_event_t ev;
42-
__wasi_errno_t error = __wasi_poll_oneoff(&sub, &ev, 1, &nevents);
43-
return error == 0 && ev.error == 0 ? 0 : ENOTSUP;
38+
poll_method_pollable_block(poll_borrow_pollable(pollable));
39+
40+
poll_pollable_drop_own(pollable);
41+
return 0;
4442
}
4543
#else
4644
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,

libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ off_t __lseek(int fildes, off_t offset, int whence) {
6969
}
7070
}
7171
// Drop the existing streams
72+
if (entry->stream.read_pollable_is_initialized)
73+
poll_pollable_drop_own(entry->stream.read_pollable);
74+
if (entry->stream.write_pollable_is_initialized)
75+
poll_pollable_drop_own(entry->stream.write_pollable);
7276
if (entry->stream.file_info.readable)
7377
streams_input_stream_drop_borrow(entry->stream.read_stream);
74-
if (entry->stream.file_info.writable) {
75-
if (entry->stream.pollable_is_initialized)
76-
poll_pollable_drop_own(entry->stream.pollable);
78+
if (entry->stream.file_info.writable)
7779
streams_output_stream_drop_borrow(entry->stream.write_stream);
78-
}
7980

8081
// Open a new stream with the right offset
8182
if (entry->stream.file_info.readable) {
@@ -106,9 +107,10 @@ off_t __lseek(int fildes, off_t offset, int whence) {
106107

107108
// Update output_stream.stream with the new stream
108109
entry->stream.write_stream = streams_borrow_output_stream(new_stream);
109-
entry->stream.pollable_is_initialized = false;
110110
}
111111

112+
entry->stream.read_pollable_is_initialized = false;
113+
entry->stream.write_pollable_is_initialized = false;
112114
// Update offset
113115
entry->stream.offset = offset_to_use;
114116
} else {

libc-bottom-half/cloudlibc/src/libc/unistd/read.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ssize_t read(int fildes, void *buf, size_t nbyte) {
3131
// Get the input stream
3232
filesystem_error_code_t error_code;
3333
streams_own_input_stream_t input_stream_own;
34+
3435
ok = filesystem_method_descriptor_read_via_stream(entry->file.file_handle,
3536
0,
3637
&input_stream_own,
@@ -62,7 +63,8 @@ ssize_t read(int fildes, void *buf, size_t nbyte) {
6263
// for this file
6364
new_entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
6465
new_entry.stream.read_stream = input_stream;
65-
new_entry.stream.pollable_is_initialized = false;
66+
new_entry.stream.read_pollable_is_initialized = false;
67+
new_entry.stream.write_pollable_is_initialized = false;
6668
new_entry.stream.offset = 0;
6769
new_entry.stream.file_info.readable = entry->file.readable;
6870
new_entry.stream.file_info.writable = entry->file.writable;

libc-bottom-half/cloudlibc/src/libc/unistd/write.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) {
5353
return -1;
5454
}
5555
output_stream = entry->stream.write_stream;
56-
if (!entry->stream.pollable_is_initialized) {
57-
pollable = entry->stream.pollable = streams_method_output_stream_subscribe(output_stream);
58-
entry->stream.pollable_is_initialized = true;
56+
if (!entry->stream.write_pollable_is_initialized) {
57+
pollable = entry->stream.write_pollable = streams_method_output_stream_subscribe(output_stream);
58+
entry->stream.write_pollable_is_initialized = true;
5959
} else
60-
pollable = entry->stream.pollable;
60+
pollable = entry->stream.write_pollable;
6161
} else {
6262
errno = EBADF;
6363
return -1;
@@ -114,8 +114,9 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) {
114114
}
115115
new_entry.stream.read_stream = streams_borrow_input_stream(read_stream);
116116
}
117-
new_entry.stream.pollable = pollable;
118-
new_entry.stream.pollable_is_initialized = true;
117+
new_entry.stream.write_pollable = pollable;
118+
new_entry.stream.read_pollable_is_initialized = false;
119+
new_entry.stream.write_pollable_is_initialized = true;
119120
new_entry.stream.write_stream = output_stream;
120121
new_entry.stream.offset = contents.len;
121122
new_entry.stream.file_info.readable = entry->file.readable;

libc-bottom-half/headers/private/wasi/descriptor_table.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ typedef struct {
128128
streams_borrow_output_stream_t write_stream;
129129
// Current position in stream, relative to the beginning of the *file*, measured in bytes
130130
off_t offset;
131-
// Used for checking readiness to write to stream. Lazily initialized.
132-
streams_own_pollable_t pollable;
133-
bool pollable_is_initialized;
131+
// Used for checking readiness to read/write to stream. Lazily initialized
132+
streams_own_pollable_t read_pollable;
133+
streams_own_pollable_t write_pollable;
134+
bool read_pollable_is_initialized;
135+
bool write_pollable_is_initialized;
134136
// When the stream is closed, the caller should
135137
// replace this entry in the table with the file handle
136138
file_t file_info;

libc-bottom-half/headers/private/wasi/file_utils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ static bool init_stdin() {
6868
entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
6969
entry.stream.read_stream = streams_borrow_input_stream(stdin_get_stdin());
7070
entry.stream.offset = 0;
71+
entry.stream.read_pollable_is_initialized = false;
72+
entry.stream.write_pollable_is_initialized = false;
7173
entry.stream.file_info.readable = true;
7274
entry.stream.file_info.writable = false;
7375
// entry.stream.file_info.file_handle is uninitialized, but it will never be used
@@ -81,7 +83,8 @@ static bool init_stdout() {
8183
entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
8284
entry.stream.write_stream = streams_borrow_output_stream(stdout_get_stdout());
8385
entry.stream.offset = 0;
84-
entry.stream.pollable = streams_method_output_stream_subscribe(entry.stream.write_stream);
86+
entry.stream.read_pollable_is_initialized = false;
87+
entry.stream.write_pollable_is_initialized = false;
8588
entry.stream.file_info.readable = false;
8689
entry.stream.file_info.writable = true;
8790
// entry.stream.file_info.file_handle is uninitialized, but it will never be used
@@ -95,7 +98,8 @@ static bool init_stderr() {
9598
entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
9699
entry.stream.write_stream = streams_borrow_output_stream(stderr_get_stderr());
97100
entry.stream.offset = 0;
98-
entry.stream.pollable = streams_method_output_stream_subscribe(entry.stream.write_stream);
101+
entry.stream.read_pollable_is_initialized = false;
102+
entry.stream.write_pollable_is_initialized = false;
99103
entry.stream.file_info.readable = false;
100104
entry.stream.file_info.writable = true;
101105
// entry.stream.file_info.file_handle is uninitialized, but it will never be used

libc-bottom-half/sources/__wasilibc_fd_renumber.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ int close(int fd) {
122122
drop_directory_stream(entry.directory_stream_info.directory_stream);
123123
break;
124124
case DESCRIPTOR_TABLE_ENTRY_FILE_STREAM:
125-
if (entry.stream.pollable_is_initialized)
126-
poll_pollable_drop_own(entry.stream.pollable);
125+
if (entry.stream.read_pollable_is_initialized)
126+
poll_pollable_drop_own(entry.stream.read_pollable);
127+
if (entry.stream.write_pollable_is_initialized)
128+
poll_pollable_drop_own(entry.stream.write_pollable);
127129
if (entry.stream.file_info.readable)
128130
streams_input_stream_drop_borrow(entry.stream.read_stream);
129131
if (entry.stream.file_info.writable)

0 commit comments

Comments
 (0)