Skip to content
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

wall clock timing functionality #173

Open
njsmith opened this issue May 26, 2017 · 3 comments

Comments

Projects
None yet
1 participant
@njsmith
Copy link
Member

commented May 26, 2017

This is probably fairly low priority, but: an interesting and sometimes-useful feature is the ability to sleep until a particular wall clock time (e.g., "this cert is expiring at 2017-12-31T12:00:00Z, so I want to wake up 3 days before that so I can renew it"). This is quite different from trio's current timekeeping abilities, which are all oriented around monotonic time (which ignores clock changes, and stops while the computer is suspended, etc.). Really these are just different incommensurable time scales.

Fortunately, I don't think we need the ability to directly set a cancel scope deadline to a particular wall clock time, so this doesn't need to be integrated deep into the guts of trio's run loop. I think it'd be sufficient to provide a sleep_until_wall_clock_time (and maybe current_wall_clock_time for completeness).

This comment and the replies have some more details on this idea, including notes on how it could be implemented on different systems.

@njsmith

This comment has been minimized.

Copy link
Member Author

commented Jan 9, 2018

The discussion in #394 got me thinking about this a bit again.

Contrary to the discussion in #168, it looks like on MacOS the best way to do this is not dispatch_after (which provides no way to cancel the submitted job), but rather to use dispatch_source_create ourselves (which is sort of like a timerfd, except that instead of a notification fd, it submits a job to a GCD queue when the timer fires – but we can reconfigure the timer at any moment). See documentation here. Also there are some global queues that are automatically run in the background that we can use, presumably dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0). (The "main" queue is not as relevant as it might sound -- main doesn't mean "this is the main thing to use", it means "this is for running things in the main thread", so if you want to use it then your main thread has to block forever processing items from the queue.)

On Linux, it isn't 100% clear to me how a CLOCK_REALTIME timerfd reacts to things like the clock being stepped forward over the deadline. We should probably check? But it's not too hard to fix if needed, because you can set a flag on a timerfd asking to be woken any time the clock is stepped, and then do whatever processing logic you want.

I guess the more interesting use case for a clock being stepped is if you have a timer set to "every Saturday at noon" -- in this case if the timer is currently set to the next Saturday – like say it's 2018-01-01, so the timer is set to 2018-01-06 – and then the clock is stepped backwards to 2017-01-01, you don't want to keep sleeping until 2018-01-6, you want to redo the "next Saturday" calculation and reset the timer to a different date (!). Clock change notification is also possible on MacOS by fiddling with Mach ports, as noted here. libdispatch is open source, so you can look at how they do this: src/event/event_kevent.c, search for "calendar_change" (or "CALENDAR_CHANGE"). On windows, you can get this notification from the WM_TIMECHANGE message, but that requires that you be running a window message pump. I'm not sure if there's any other way; the C# SystemEvents.TimeChanged docs make clear that it's based on WM_TIMECHANGE and have special notes on how to get the message pump working if you're in a background service, so maybe there is no better solution.

Brainstorming possible tr- + time-themed names for a trio wall-clock library: treadmill, transience, transient, treacle, trend, trickle

@njsmith

This comment has been minimized.

Copy link
Member Author

commented Apr 19, 2018

Here's the twisted bug in case anyone wants to read 11 years of discussion of this topic: https://twistedmatrix.com/trac/ticket/2424

@njsmith

This comment has been minimized.

Copy link
Member Author

commented Apr 19, 2018

Though most of the discussion in that twisted ticket seems to be about how to untangle themselves from having decided early on to use a mix of monotonic and wall-clock time. (Not really their fault, but they're using wall clock time for calculations, and then calling select or whatever, and select and friends use monotonic time.) Trio's internal clock is always a monotonic clock, so we don't have that particular issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.