Skip to content

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

2 participants