|
| 1 | +/* |
| 2 | + * Copyright 2025 The Emscripten Authors. All rights reserved. |
| 3 | + * Emscripten is available under two separate licenses, the MIT license and the |
| 4 | + * University of Illinois/NCSA Open Source License. Both these licenses can be |
| 5 | + * found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +// Duplicate of test_poll_blocking.c using ppoll() instead of poll() |
| 9 | + |
| 10 | +#include <poll.h> |
| 11 | +#include <time.h> |
| 12 | +#include <assert.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <pthread.h> |
| 16 | +#include <string.h> |
| 17 | +#include <sys/time.h> |
| 18 | + |
| 19 | +#define TIMEOUT_MS 300 |
| 20 | +#define TIMEOUT_NS (TIMEOUT_MS * 1000000) |
| 21 | + |
| 22 | +// It is possible for the node timers (such as setTimeout or Atomics.wait) to wake up |
| 23 | +// slightly earlier than requested. Because we measure times accurately using |
| 24 | +// clock_gettime, we give tests a 5 milliseconds error margin to avoid flaky timeouts. |
| 25 | +#define TIMEOUT_MARGIN_MS 5 |
| 26 | + |
| 27 | +void sleep_ms(int ms) { |
| 28 | + usleep(ms * 1000); |
| 29 | +} |
| 30 | + |
| 31 | +int64_t timespec_delta_ms(struct timespec* begin, struct timespec* end) { |
| 32 | + int64_t delta_sec = end->tv_sec - begin->tv_sec; |
| 33 | + int64_t delta_nsec = end->tv_nsec - begin->tv_nsec; |
| 34 | + |
| 35 | + assert(delta_sec >= 0); |
| 36 | + assert(delta_nsec > -1000000000 && delta_nsec < 1000000000); |
| 37 | + |
| 38 | + int64_t delta_ms = (delta_sec * 1000) + (delta_nsec / 1000000); |
| 39 | + assert(delta_ms >= 0); |
| 40 | + return delta_ms; |
| 41 | +} |
| 42 | + |
| 43 | +// Check if timeout works without fds |
| 44 | +void test_timeout_without_fds() { |
| 45 | + printf("test_timeout_without_fds\n"); |
| 46 | + struct timespec begin, end; |
| 47 | + |
| 48 | + clock_gettime(CLOCK_MONOTONIC, &begin); |
| 49 | + struct timespec timeout; |
| 50 | + timeout.tv_sec = 0; |
| 51 | + timeout.tv_nsec = TIMEOUT_NS; |
| 52 | + assert(ppoll(NULL, 0, &timeout, NULL) == 0); |
| 53 | + clock_gettime(CLOCK_MONOTONIC, &end); |
| 54 | + |
| 55 | + int64_t duration = timespec_delta_ms(&begin, &end); |
| 56 | + printf(" -> duration: %lld ms\n", duration); |
| 57 | + assert(duration >= TIMEOUT_MS - TIMEOUT_MARGIN_MS); |
| 58 | +} |
| 59 | + |
| 60 | +// Check if timeout works with fds without events |
| 61 | +void test_timeout_with_fds_without_events() { |
| 62 | + printf("test_timeout_with_fds_without_events\n"); |
| 63 | + struct timespec begin, end; |
| 64 | + int pipe_a[2]; |
| 65 | + |
| 66 | + assert(pipe(pipe_a) == 0); |
| 67 | + |
| 68 | + clock_gettime(CLOCK_MONOTONIC, &begin); |
| 69 | + struct pollfd fds = {pipe_a[0], 0, 0}; |
| 70 | + struct timespec timeout; |
| 71 | + timeout.tv_sec = 0; |
| 72 | + timeout.tv_nsec = TIMEOUT_NS; |
| 73 | + assert(ppoll(&fds, 1, &timeout, NULL) == 0); |
| 74 | + clock_gettime(CLOCK_MONOTONIC, &end); |
| 75 | + |
| 76 | + int64_t duration = timespec_delta_ms(&begin, &end); |
| 77 | + printf(" -> duration: %lld ms\n", duration); |
| 78 | + assert(duration >= TIMEOUT_MS - TIMEOUT_MARGIN_MS); |
| 79 | + |
| 80 | + close(pipe_a[0]); close(pipe_a[1]); |
| 81 | +} |
| 82 | + |
| 83 | +int pipe_shared[2]; |
| 84 | + |
| 85 | +void *write_after_sleep(void * arg) { |
| 86 | + const char *t = "test\n"; |
| 87 | + |
| 88 | + sleep_ms(TIMEOUT_MS); |
| 89 | + write(pipe_shared[1], t, strlen(t)); |
| 90 | + |
| 91 | + return NULL; |
| 92 | +} |
| 93 | + |
| 94 | +// Check if ppoll can unblock on an event |
| 95 | +void test_unblock_ppoll() { |
| 96 | + printf("test_unblock_ppoll\n"); |
| 97 | + struct timespec begin, end; |
| 98 | + pthread_t tid; |
| 99 | + int pipe_a[2]; |
| 100 | + |
| 101 | + assert(pipe(pipe_a) == 0); |
| 102 | + assert(pipe(pipe_shared) == 0); |
| 103 | + |
| 104 | + struct pollfd fds[2] = { |
| 105 | + {pipe_a[0], POLLIN, 0}, |
| 106 | + {pipe_shared[0], POLLIN, 0}, |
| 107 | + }; |
| 108 | + clock_gettime(CLOCK_MONOTONIC, &begin); |
| 109 | + assert(pthread_create(&tid, NULL, write_after_sleep, NULL) == 0); |
| 110 | + assert(ppoll(fds, 2, NULL, NULL) == 1); |
| 111 | + clock_gettime(CLOCK_MONOTONIC, &end); |
| 112 | + assert(fds[1].revents & POLLIN); |
| 113 | + |
| 114 | + int64_t duration = timespec_delta_ms(&begin, &end); |
| 115 | + printf(" -> duration: %lld ms\n", duration); |
| 116 | + assert(duration >= TIMEOUT_MS - TIMEOUT_MARGIN_MS); |
| 117 | + |
| 118 | + pthread_join(tid, NULL); |
| 119 | + |
| 120 | + close(pipe_a[0]); close(pipe_a[1]); |
| 121 | + close(pipe_shared[0]); close(pipe_shared[1]); |
| 122 | +} |
| 123 | + |
| 124 | +int threads_running = 0; |
| 125 | +pthread_mutex_t running_lock = PTHREAD_MUTEX_INITIALIZER; |
| 126 | +pthread_cond_t running_cv = PTHREAD_COND_INITIALIZER; |
| 127 | + |
| 128 | +void *do_ppoll_in_thread(void * arg) { |
| 129 | + struct timespec begin, end; |
| 130 | + |
| 131 | + clock_gettime(CLOCK_MONOTONIC, &begin); |
| 132 | + struct pollfd fds = {pipe_shared[0], POLLIN, 0}; |
| 133 | + pthread_mutex_lock(&running_lock); |
| 134 | + threads_running++; |
| 135 | + pthread_cond_signal(&running_cv); |
| 136 | + pthread_mutex_unlock(&running_lock); |
| 137 | + struct timespec timeout; |
| 138 | + timeout.tv_sec = 4; |
| 139 | + timeout.tv_nsec = 0; |
| 140 | + assert(ppoll(&fds, 1, &timeout, NULL) == 1); |
| 141 | + clock_gettime(CLOCK_MONOTONIC, &end); |
| 142 | + assert(fds.revents & POLLIN); |
| 143 | + |
| 144 | + int64_t duration = timespec_delta_ms(&begin, &end); |
| 145 | + printf(" -> duration: %lld ms\n", duration); |
| 146 | + assert((duration >= TIMEOUT_MS - TIMEOUT_MARGIN_MS) && (duration < 4000)); |
| 147 | + |
| 148 | + return NULL; |
| 149 | +} |
| 150 | + |
| 151 | +// Check if ppoll works in threads |
| 152 | +void test_ppoll_in_threads() { |
| 153 | + printf("test_ppoll_in_threads\n"); |
| 154 | + pthread_t tid1, tid2; |
| 155 | + const char *t = "test\n"; |
| 156 | + |
| 157 | + assert(pipe(pipe_shared) == 0); |
| 158 | + |
| 159 | + assert(pthread_create(&tid1, NULL, do_ppoll_in_thread, NULL) == 0); |
| 160 | + assert(pthread_create(&tid2, NULL, do_ppoll_in_thread, NULL) == 0); |
| 161 | + pthread_mutex_lock(&running_lock); |
| 162 | + while (threads_running != 2) { |
| 163 | + pthread_cond_wait(&running_cv, &running_lock); |
| 164 | + } |
| 165 | + pthread_mutex_unlock(&running_lock); |
| 166 | + |
| 167 | + sleep_ms(2 * TIMEOUT_MS); |
| 168 | + write(pipe_shared[1], t, strlen(t)); |
| 169 | + |
| 170 | + pthread_join(tid1, NULL); |
| 171 | + pthread_join(tid2, NULL); |
| 172 | + |
| 173 | + close(pipe_shared[0]); close(pipe_shared[1]); |
| 174 | +} |
| 175 | + |
| 176 | +// Check if ppoll works with ready fds |
| 177 | +void test_ready_fds() { |
| 178 | + printf("test_ready_fds\n"); |
| 179 | + struct timespec zero_timeout; |
| 180 | + zero_timeout.tv_sec = 0; |
| 181 | + zero_timeout.tv_nsec = 0; |
| 182 | + fd_set readfds; |
| 183 | + const char *t = "test\n"; |
| 184 | + int pipe_c[2]; |
| 185 | + int pipe_d[2]; |
| 186 | + |
| 187 | + assert(pipe(pipe_c) == 0); |
| 188 | + assert(pipe(pipe_d) == 0); |
| 189 | + |
| 190 | + write(pipe_c[1], t, strlen(t)); |
| 191 | + write(pipe_d[1], t, strlen(t)); |
| 192 | + |
| 193 | + struct pollfd fds[2] = { |
| 194 | + {pipe_c[0], POLLIN, 0}, |
| 195 | + {pipe_d[0], POLLIN, 0}, |
| 196 | + }; |
| 197 | + |
| 198 | + assert(ppoll(fds, 2, &zero_timeout, NULL) == 2); |
| 199 | + assert(fds[0].revents & POLLIN); |
| 200 | + assert(fds[1].revents & POLLIN); |
| 201 | + |
| 202 | + fds[0].revents = 0; |
| 203 | + fds[1].revents = 0; |
| 204 | + |
| 205 | + assert(ppoll(fds, 2, &zero_timeout, NULL) == 2); |
| 206 | + assert(fds[0].revents & POLLIN); |
| 207 | + assert(fds[1].revents & POLLIN); |
| 208 | + |
| 209 | + close(pipe_c[0]); close(pipe_c[1]); |
| 210 | + close(pipe_d[0]); close(pipe_d[1]); |
| 211 | +} |
| 212 | + |
| 213 | +int main() { |
| 214 | + test_ppoll_in_threads(); |
| 215 | + test_timeout_without_fds(); |
| 216 | + test_timeout_with_fds_without_events(); |
| 217 | + test_unblock_ppoll(); |
| 218 | + test_ready_fds(); |
| 219 | + printf("done\n"); |
| 220 | + return 0; |
| 221 | +} |
0 commit comments