Skip to content

Expand scope of code that works with ev/deadline again #1575

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
merged 6 commits into from
Mar 26, 2025

Conversation

pyrmont
Copy link
Contributor

@pyrmont pyrmont commented Mar 25, 2025

This PR carries on from #1566. The background, implementation and limitations are largely the same as set out in that PR.

The key difference is that the background thread is now sent to sleep for the amount of the timeout (previously, the thread used sleep(0)). To avoid a scenario where the fiber being monitored has finished running but the background thread remains asleep (which would prevent the process from terminating), the thread is now cancelled. This is achieved on POSIX by using nanosleep (which creates a 'cancellation point' for pthread_cancel) and on Windows by using SleepEx (which permits an asynchronous procedure call to be made using QueueUserAPC).

A test is also added to demonstrate usage.

Note

The JanetTimeout struct is only used when the JANET_EV macro is defined but was always being declared in state.h. This PR puts the declaration inside a preprocessor directive. This is similar to how <pthread.h> is only included if the JANET_EV macro is defined.

@bakpakin
Copy link
Member

This looks great! I don't see any issues on a first look, and the cancellation code looks solid with no busy loops.

@bakpakin bakpakin merged commit 707463a into janet-lang:master Mar 26, 2025
13 checks passed
@pyrmont pyrmont deleted the feature.ev-interrupt2 branch March 26, 2025 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants