-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Implement ppoll and pselect in terms of poll and select #26482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+485
−10
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| /* | ||
| * Copyright 2025 The Emscripten Authors. All rights reserved. | ||
| * Emscripten is available under two separate licenses, the MIT license and the | ||
| * University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| * found in the LICENSE file. | ||
| */ | ||
|
|
||
| // Duplicate of test_poll_blocking.c using ppoll() instead of poll() | ||
|
|
||
| #include <poll.h> | ||
| #include <time.h> | ||
| #include <assert.h> | ||
| #include <stdio.h> | ||
| #include <unistd.h> | ||
| #include <pthread.h> | ||
| #include <string.h> | ||
| #include <sys/time.h> | ||
|
|
||
| #define TIMEOUT_MS 300 | ||
| #define TIMEOUT_NS (TIMEOUT_MS * 1000000) | ||
|
|
||
| // It is possible for the node timers (such as setTimeout or Atomics.wait) to wake up | ||
| // slightly earlier than requested. Because we measure times accurately using | ||
| // clock_gettime, we give tests a 5 milliseconds error margin to avoid flaky timeouts. | ||
| #define TIMEOUT_MARGIN_MS 5 | ||
|
|
||
| void sleep_ms(int ms) { | ||
| usleep(ms * 1000); | ||
| } | ||
|
|
||
| int64_t timespec_delta_ms(struct timespec* begin, struct timespec* end) { | ||
| int64_t delta_sec = end->tv_sec - begin->tv_sec; | ||
| int64_t delta_nsec = end->tv_nsec - begin->tv_nsec; | ||
|
|
||
| assert(delta_sec >= 0); | ||
| assert(delta_nsec > -1000000000 && delta_nsec < 1000000000); | ||
|
|
||
| int64_t delta_ms = (delta_sec * 1000) + (delta_nsec / 1000000); | ||
| assert(delta_ms >= 0); | ||
| return delta_ms; | ||
| } | ||
|
|
||
| // Check if timeout works without fds | ||
| void test_timeout_without_fds() { | ||
| printf("test_timeout_without_fds\n"); | ||
| struct timespec begin, end; | ||
|
|
||
| clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| struct timespec timeout; | ||
| timeout.tv_sec = 0; | ||
| timeout.tv_nsec = TIMEOUT_NS; | ||
| assert(ppoll(NULL, 0, &timeout, NULL) == 0); | ||
| clock_gettime(CLOCK_MONOTONIC, &end); | ||
|
|
||
| int64_t duration = timespec_delta_ms(&begin, &end); | ||
| printf(" -> duration: %lld ms\n", duration); | ||
| assert(duration >= TIMEOUT_MS - TIMEOUT_MARGIN_MS); | ||
| } | ||
|
|
||
| // Check if timeout works with fds without events | ||
| void test_timeout_with_fds_without_events() { | ||
| printf("test_timeout_with_fds_without_events\n"); | ||
| struct timespec begin, end; | ||
| int pipe_a[2]; | ||
|
|
||
| assert(pipe(pipe_a) == 0); | ||
|
|
||
| clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| struct pollfd fds = {pipe_a[0], 0, 0}; | ||
| struct timespec timeout; | ||
| timeout.tv_sec = 0; | ||
| timeout.tv_nsec = TIMEOUT_NS; | ||
| assert(ppoll(&fds, 1, &timeout, NULL) == 0); | ||
| clock_gettime(CLOCK_MONOTONIC, &end); | ||
|
|
||
| int64_t duration = timespec_delta_ms(&begin, &end); | ||
| printf(" -> duration: %lld ms\n", duration); | ||
| assert(duration >= TIMEOUT_MS - TIMEOUT_MARGIN_MS); | ||
|
|
||
| close(pipe_a[0]); close(pipe_a[1]); | ||
| } | ||
|
|
||
| int pipe_shared[2]; | ||
|
|
||
| void *write_after_sleep(void * arg) { | ||
| const char *t = "test\n"; | ||
|
|
||
| sleep_ms(TIMEOUT_MS); | ||
| write(pipe_shared[1], t, strlen(t)); | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| // Check if ppoll can unblock on an event | ||
| void test_unblock_ppoll() { | ||
| printf("test_unblock_ppoll\n"); | ||
| struct timespec begin, end; | ||
| pthread_t tid; | ||
| int pipe_a[2]; | ||
|
|
||
| assert(pipe(pipe_a) == 0); | ||
| assert(pipe(pipe_shared) == 0); | ||
|
|
||
| struct pollfd fds[2] = { | ||
| {pipe_a[0], POLLIN, 0}, | ||
| {pipe_shared[0], POLLIN, 0}, | ||
| }; | ||
| clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| assert(pthread_create(&tid, NULL, write_after_sleep, NULL) == 0); | ||
| assert(ppoll(fds, 2, NULL, NULL) == 1); | ||
| clock_gettime(CLOCK_MONOTONIC, &end); | ||
| assert(fds[1].revents & POLLIN); | ||
|
|
||
| int64_t duration = timespec_delta_ms(&begin, &end); | ||
| printf(" -> duration: %lld ms\n", duration); | ||
| assert(duration >= TIMEOUT_MS - TIMEOUT_MARGIN_MS); | ||
|
|
||
| pthread_join(tid, NULL); | ||
|
|
||
| close(pipe_a[0]); close(pipe_a[1]); | ||
| close(pipe_shared[0]); close(pipe_shared[1]); | ||
| } | ||
|
|
||
| int threads_running = 0; | ||
| pthread_mutex_t running_lock = PTHREAD_MUTEX_INITIALIZER; | ||
| pthread_cond_t running_cv = PTHREAD_COND_INITIALIZER; | ||
|
|
||
| void *do_ppoll_in_thread(void * arg) { | ||
| struct timespec begin, end; | ||
|
|
||
| clock_gettime(CLOCK_MONOTONIC, &begin); | ||
| struct pollfd fds = {pipe_shared[0], POLLIN, 0}; | ||
| pthread_mutex_lock(&running_lock); | ||
| threads_running++; | ||
| pthread_cond_signal(&running_cv); | ||
| pthread_mutex_unlock(&running_lock); | ||
| struct timespec timeout; | ||
| timeout.tv_sec = 4; | ||
| timeout.tv_nsec = 0; | ||
| assert(ppoll(&fds, 1, &timeout, NULL) == 1); | ||
| clock_gettime(CLOCK_MONOTONIC, &end); | ||
| assert(fds.revents & POLLIN); | ||
|
|
||
| int64_t duration = timespec_delta_ms(&begin, &end); | ||
| printf(" -> duration: %lld ms\n", duration); | ||
| assert((duration >= TIMEOUT_MS - TIMEOUT_MARGIN_MS) && (duration < 4000)); | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| // Check if ppoll works in threads | ||
| void test_ppoll_in_threads() { | ||
| printf("test_ppoll_in_threads\n"); | ||
| pthread_t tid1, tid2; | ||
| const char *t = "test\n"; | ||
|
|
||
| assert(pipe(pipe_shared) == 0); | ||
|
|
||
| assert(pthread_create(&tid1, NULL, do_ppoll_in_thread, NULL) == 0); | ||
| assert(pthread_create(&tid2, NULL, do_ppoll_in_thread, NULL) == 0); | ||
| pthread_mutex_lock(&running_lock); | ||
| while (threads_running != 2) { | ||
| pthread_cond_wait(&running_cv, &running_lock); | ||
| } | ||
| pthread_mutex_unlock(&running_lock); | ||
|
|
||
| sleep_ms(2 * TIMEOUT_MS); | ||
| write(pipe_shared[1], t, strlen(t)); | ||
|
|
||
| pthread_join(tid1, NULL); | ||
| pthread_join(tid2, NULL); | ||
|
|
||
| close(pipe_shared[0]); close(pipe_shared[1]); | ||
| } | ||
|
|
||
| // Check if ppoll works with ready fds | ||
| void test_ready_fds() { | ||
| printf("test_ready_fds\n"); | ||
| struct timespec zero_timeout; | ||
| zero_timeout.tv_sec = 0; | ||
| zero_timeout.tv_nsec = 0; | ||
| fd_set readfds; | ||
| const char *t = "test\n"; | ||
| int pipe_c[2]; | ||
| int pipe_d[2]; | ||
|
|
||
| assert(pipe(pipe_c) == 0); | ||
| assert(pipe(pipe_d) == 0); | ||
|
|
||
| write(pipe_c[1], t, strlen(t)); | ||
| write(pipe_d[1], t, strlen(t)); | ||
|
|
||
| struct pollfd fds[2] = { | ||
| {pipe_c[0], POLLIN, 0}, | ||
| {pipe_d[0], POLLIN, 0}, | ||
| }; | ||
|
|
||
| assert(ppoll(fds, 2, &zero_timeout, NULL) == 2); | ||
| assert(fds[0].revents & POLLIN); | ||
| assert(fds[1].revents & POLLIN); | ||
|
|
||
| fds[0].revents = 0; | ||
| fds[1].revents = 0; | ||
|
|
||
| assert(ppoll(fds, 2, &zero_timeout, NULL) == 2); | ||
| assert(fds[0].revents & POLLIN); | ||
| assert(fds[1].revents & POLLIN); | ||
|
|
||
| close(pipe_c[0]); close(pipe_c[1]); | ||
| close(pipe_d[0]); close(pipe_d[1]); | ||
| } | ||
|
|
||
| int main() { | ||
| test_ppoll_in_threads(); | ||
| test_timeout_without_fds(); | ||
| test_timeout_with_fds_without_events(); | ||
| test_unblock_ppoll(); | ||
| test_ready_fds(); | ||
| printf("done\n"); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you perhaps know why this symbol (and
$__syscall_shutdownbelow) is included now? I noticed a similar thing in PR #19559, but haven't investigated this further.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. I actually have no idea. Asking gemini if it can figure that out now..
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, AI figured it out pretty quick:
"""
The investigation reveals that the appearance of __syscall_recvmmsg and __syscall_shutdown in the funcs list of test_codesize_hello_dylink_all.json is a side effect of Identical Code Folding (ICF) in the linker, combined with the removal of ppoll and pselect6 from the UNIMPLEMENTED syscall stubs.
"""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Key Findings:
defined (as it is in this test), these functions all have an identical body (return -ENOSYS;). The linker (Wasm-LD) merges functions with
identical bodies and the same signatures into a single WASM function to save space.
This is usually the alphabetically first name among the merged symbols.
alphabetically first, it was chosen as the canonical name ($__syscall_ppoll).
alphabetically first, it was chosen as the canonical name ($__syscall_pselect6).
real implementations).
defined (as it is in this test), these functions all have an identical body (return -ENOSYS;). The linker (Wasm-LD) merges functions with
identical bodies and the same signatures into a single WASM function to save space.
This is usually the alphabetically first name among the merged symbols.
alphabetically first, it was chosen as the canonical name ($__syscall_ppoll).
alphabetically first, it was chosen as the canonical name ($__syscall_pselect6).
real implementations).
previous "representatives" of these merged groups were removed from the codebase.
The symbols were already present as exports before the change; they only "appeared" as new internal function names because they were promoted
to being the representatives for their respective merged groups.
previous "representatives" of these merged groups were removed from the codebase.
The symbols were already present as exports before the change; they only "appeared" as new internal function names because they were promoted
to being the representatives for their respective merged groups.