Skip to content

Switch to waitable timers for sleep #20

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

elupus
Copy link
Contributor

@elupus elupus commented Feb 27, 2023

This improves accuracy compared to sleep.

This make use of high resolution timers on modern
windows machines to ensure timers as well as sleeps
are close to 1 ms accurate, without raising the
global tick interval.

In theory it should allow more than 1ms accuracy
however, it seem to generally be limited to around
0.6 ms in testing.

Note. Requires windows 10.

@elupus elupus force-pushed the sleepfix branch 3 times, most recently from 38a6c68 to c343b84 Compare April 8, 2025 15:58
@elupus elupus requested a review from hefloryd April 8, 2025 15:59
{
os_thread_state_t * state;

if (os_thread_tls_index == TLS_OUT_OF_INDEXES)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os_thread_tls_index is shared by all threads? At a quick glance this looks like it might not be thread-safe. Add a lock if needed or otherwise a comment to explain why locking is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Har skrivit om detta till att använda annan allokering kopplat till kompilatorn istället så slipper vi den racen.

Dock är det lite dumt att vi inte har en osal_init() funktion. Jag blev tvungen att behålla os_thread_get_state() funktionen eftersom vi har testcase (och sannolikt verkliga användare) som anropar os_*sleep funktioner utan att vara i en os_thread(). Dvs, från main funktion.

Vi kommer läcka en os_thread_state i dessa fall då vi inte kan göra en cleanup som görs i task entry funktionen just nu. I teorin kan man använda FlsAlloc() och dess cleanup funktionallitet. Men race:et finns kvar då vi inte har någon init() funktion.

This improves accuracy compared to sleep.

This make use of high resolution timers on modern
windows machines to ensure timers as well as sleeps
are close to 1 ms accurate, without raising the
global tick interval.

In theory it should allow more than 1ms accuracy
however, it seem to generally be limited to around
0.6 ms in testing.

Note. Requires windows 10.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants