forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpoll_oneoff.c
More file actions
269 lines (216 loc) · 7.4 KB
/
poll_oneoff.c
File metadata and controls
269 lines (216 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "uv.h"
#include "poll_oneoff.h"
#include "uv_mapping.h"
#include "uvwasi_alloc.h"
static void poll_cb(uv_poll_t* handle, int status, int events) {
struct uvwasi_poll_oneoff_state_t* state;
struct uvwasi__poll_fdevent_t* event;
uv_poll_stop(handle);
event = uv_handle_get_data((uv_handle_t*) handle);
event->revents = events;
if (status != 0)
event->error = UVWASI_EIO;
state = uv_loop_get_data(handle->loop);
state->result++;
}
static void timeout_cb(uv_timer_t* handle) {
struct uvwasi_poll_oneoff_state_t* state;
uvwasi_size_t i;
state = uv_loop_get_data(handle->loop);
for (i = 0; i < state->handle_cnt; i++)
uv_poll_stop(&state->poll_handles[i]);
}
uvwasi_errno_t uvwasi__poll_oneoff_state_init(
uvwasi_t* uvwasi,
struct uvwasi_poll_oneoff_state_t* state,
uvwasi_size_t max_fds
) {
uvwasi_errno_t err;
int r;
if (uvwasi == NULL || state == NULL)
return UVWASI_EINVAL;
state->uvwasi = NULL;
state->timeout = 0;
state->has_timer = 0;
state->fdevents = NULL;
state->poll_handles = NULL;
state->max_fds = 0;
state->fdevent_cnt = 0;
state->handle_cnt = 0;
state->result = 0;
r = uv_loop_init(&state->loop);
if (r != 0)
return uvwasi__translate_uv_error(r);
if (max_fds > 0) {
state->fdevents = uvwasi__calloc(uvwasi,
max_fds,
sizeof(*state->fdevents));
if (state->fdevents == NULL) {
err = UVWASI_ENOMEM;
goto error_exit;
}
state->poll_handles = uvwasi__calloc(uvwasi,
max_fds,
sizeof(*state->poll_handles));
if (state->poll_handles == NULL) {
err = UVWASI_ENOMEM;
goto error_exit;
}
}
uv_loop_set_data(&state->loop, (void*) state);
state->uvwasi = uvwasi;
state->max_fds = max_fds;
return UVWASI_ESUCCESS;
error_exit:
uv_loop_close(&state->loop);
uvwasi__free(state->uvwasi, state->fdevents);
uvwasi__free(state->uvwasi, state->poll_handles);
return err;
}
uvwasi_errno_t uvwasi__poll_oneoff_state_cleanup(
struct uvwasi_poll_oneoff_state_t* state
) {
struct uvwasi__poll_fdevent_t* event;
uvwasi_size_t i;
int r;
if (state == NULL)
return UVWASI_EINVAL;
if (state->has_timer != 0) {
state->timeout = 0;
state->has_timer = 0;
uv_close((uv_handle_t*) &state->timer, NULL);
}
for (i = 0; i < state->fdevent_cnt; i++) {
event = &state->fdevents[i];
if (event->is_duplicate_fd == 0 && event->wrap != NULL)
uv_mutex_unlock(&event->wrap->mutex);
}
for (i = 0; i < state->handle_cnt; i++)
uv_close((uv_handle_t*) &state->poll_handles[i], NULL);
uv_run(&state->loop, UV_RUN_NOWAIT);
state->max_fds = 0;
state->fdevent_cnt = 0;
state->handle_cnt = 0;
uvwasi__free(state->uvwasi, state->fdevents);
uvwasi__free(state->uvwasi, state->poll_handles);
state->fdevents = NULL;
state->poll_handles = NULL;
state->uvwasi = NULL;
r = uv_loop_close(&state->loop);
if (r != 0)
return uvwasi__translate_uv_error(r);
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi__poll_oneoff_state_set_timer(
struct uvwasi_poll_oneoff_state_t* state,
uvwasi_timestamp_t timeout
) {
int r;
if (state == NULL)
return UVWASI_EINVAL;
r = uv_timer_init(&state->loop, &state->timer);
if (r != 0)
return uvwasi__translate_uv_error(r);
/* Convert WASI timeout from nanoseconds to milliseconds for libuv. */
state->timeout = timeout / 1000000;
state->has_timer = 1;
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi__poll_oneoff_state_add_fdevent(
struct uvwasi_poll_oneoff_state_t* state,
uvwasi_subscription_t* subscription
) {
struct uvwasi__poll_fdevent_t* event;
struct uvwasi__poll_fdevent_t* dup;
uv_poll_t* poll_handle;
uvwasi_eventtype_t type;
uvwasi_rights_t rights;
uvwasi_fd_t fd;
uvwasi_errno_t err;
uvwasi_size_t i;
int r;
if (state == NULL)
return UVWASI_EINVAL;
event = &state->fdevents[state->fdevent_cnt];
fd = subscription->u.fd_readwrite.fd;
type = subscription->type;
if (type == UVWASI_EVENTTYPE_FD_READ) {
event->events = UV_DISCONNECT | UV_READABLE;
rights = UVWASI_RIGHT_POLL_FD_READWRITE | UVWASI_RIGHT_FD_READ;
} else if (type == UVWASI_EVENTTYPE_FD_WRITE) {
event->events = UV_DISCONNECT | UV_WRITABLE;
rights = UVWASI_RIGHT_POLL_FD_READWRITE | UVWASI_RIGHT_FD_WRITE;
} else {
return UVWASI_EINVAL;
}
/* Check if the same file descriptor is already being polled. If so, use the
wrap and poll handle from the first descriptor. The reasons are that libuv
does not support polling the same fd more than once at the same time, and
uvwasi has the fd's mutex locked. */
event->is_duplicate_fd = 0;
for (i = 0; i < state->fdevent_cnt; i++) {
dup = &state->fdevents[i];
if (dup->wrap->id == fd) {
event->is_duplicate_fd = 1;
event->wrap = dup->wrap;
event->poll_handle = dup->poll_handle;
err = event->error;
goto poll_config_done;
}
}
/* Get the file descriptor. If UVWASI_EBADF is returned, continue on, but
don't do any polling with the handle. */
err = uvwasi_fd_table_get(state->uvwasi->fds, fd, &event->wrap, rights, 0);
if (err == UVWASI_EBADF)
event->wrap = NULL;
else if (err != UVWASI_ESUCCESS)
return err;
if (err == UVWASI_ESUCCESS) {
/* The fd is valid, so setup the poll handle. */
poll_handle = &state->poll_handles[state->handle_cnt];
r = uv_poll_init(&state->loop, poll_handle, event->wrap->fd);
if (r != 0) {
/* If uv_poll_init() fails (for example on Windows because only sockets
are supported), set the error for this event to UVWASI_EBADF, but don't
do any polling with the handle. */
uv_mutex_unlock(&event->wrap->mutex);
return uvwasi__translate_uv_error(r);
} else {
r = uv_poll_start(poll_handle,
event->events,
poll_cb);
if (r != 0) {
uv_mutex_unlock(&event->wrap->mutex);
uv_close((uv_handle_t*) poll_handle, NULL);
return uvwasi__translate_uv_error(r);
}
uv_handle_set_data((uv_handle_t*) poll_handle,
(void*) &state->fdevents[state->fdevent_cnt]);
event->poll_handle = poll_handle;
state->handle_cnt++;
}
}
poll_config_done:
event->type = type;
event->userdata = subscription->userdata;
event->error = err;
event->revents = 0;
state->fdevent_cnt++;
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi__poll_oneoff_run(
struct uvwasi_poll_oneoff_state_t* state
) {
int r;
if (state->has_timer == 1) {
r = uv_timer_start(&state->timer, timeout_cb, state->timeout, 0);
if (r != 0)
return uvwasi__translate_uv_error(r);
if (state->fdevent_cnt > 0)
uv_unref((uv_handle_t*) &state->timer);
}
r = uv_run(&state->loop, UV_RUN_DEFAULT);
if (r != 0)
return uvwasi__translate_uv_error(r);
return UVWASI_ESUCCESS;
}