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

Coroutines #412

Closed
wants to merge 25 commits into from
Closed

Coroutines #412

wants to merge 25 commits into from

Conversation

sodiumjoe
Copy link
Contributor

No description provided.

willglynn and others added 25 commits August 9, 2017 07:12
poll_events() and run_forever() now delegate event production to get_event(),
a new function whose job it is to either a) produce Some(Event) by a Timeout
or b) return None. This dramatically simplifies the entire event loop.

MacOS in particular has multiple ways that Events can be produced: wakeup(),
AppDelegate callbacks, and a Cocoa event receive -> send -> translate cycle.
The first two are handled by an event queue; wakeup() now posts an
Event::Awakened to the queue, just like the callbacks.

The CocoaEvent cycle is handled either via the usual blocking
[NSApp sendEvent:] call or via a coroutine-based event dispatcher. The
coroutine yields back to get_event() on a regular basis, even while it's in
the middle of sending events. This allows get_event() to return an event to
the calling application even if it is still in the middle of a sendEvent:
call.
events_loop::runloop is now responsible for the entire Cocoa runloop cycle.
It's a simple blocking implementation (which fails during a resize), but
it's an API that maps cleanly to a coroutine-based implementation.

NSEvent concerns are now separated into events_loop::nsevent.
This puts an upper bound on how long the coroutine can execute without
yielding back to the main thread. The runloop observer fires before going to
sleep, which is useful if `forward_event_to_cocoa()` has triggered an inner
event loop.

The timer could be used to wake the event loop from a different thread, but
in practice that has a few milliseconds of latency, and it also requires
finding a reference to the timer from `Runloop::wake()`. Instead, the timer
is configured to fire every few milliseconds all the time, again in the
pursuit of lower latency.
Posting an event causes receive_event_from_cocoa() to return, which gives
the runloop an opportunity to check Shared::has_queued_events(). This ensures
that wakeup events can be delivered promptly in the absence of other event
traffic.
@sodiumjoe sodiumjoe closed this Feb 24, 2018
@sodiumjoe sodiumjoe deleted the coroutines branch February 24, 2018 22:44
madsmtm pushed a commit to madsmtm/winit that referenced this pull request Jun 11, 2022
* feat: connect mouse wheel event with GTK window

* Update mouse-wheel.md

Co-authored-by: Ngo Iok Ui (Wu Yu Wei) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants