emscripten
emscripten copied to clipboard
Fix `select` didn't work with pipes and the timeout argument
This PR fixes an issue that the timeout argument for the select syscall wasn't applied to pipes and it returned immedeately instead of waiting for events.
Although the select syscall passes the timeout value to the poll method of the stream implementations, this can't handle multiple fds because a fd can't notify readiness while another is blocking in poll. As a result, using the select syscall with a combination of pipes and other streams (e.g. PTY) can be problematic.
To address this, this PR implements a callback-based event notification. Each stream implementation's poll invocation receives a callback, allowing it asynchronously notify the select syscall when it becomes ready. The select syscall blocks until one of these callbacks is triggered or the timeout expires. This behviour is enabled only when PROXY_TO_PTHREAD is enabled to avoid blocking the main worker.
To maintain compatibility with non-pipefs streams, the select syscall allows stream implementations to ignore the callback and synchronously return the event status instead. In such cases, the select syscall still updates the flags accordingly.