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

EventListener::run_handler now takes an Arc to the handler instead of moving it #9

Merged
merged 2 commits into from
Jun 17, 2022

Conversation

foresterre
Copy link
Owner

This is convenient when the handler stores, or refers to outside state. By taking an atomic reference, we can simply clone the reference counting ptr and keep it around to inspect the state of the handler (assuming the handler allows inspection of its contents).

The cost compared to just moving the handler is negligible compared to the expected cost of the running lifetime of the handler.

Unfortunately, since thread::spawn takes an F: 'static, we can't just take the handler by reference. We may be able to use scoped threads and put the scope in the EventListener::FinishProcessingHandle, but doing so would require doing acrobatics with lifetimes beyond the added value, over the little added cost of introducing the Arc.

A bigger disadvantage of taking an Arc is that the Arc will be present in the EventListener::run_handler API.

Why Arc over Rc? It is expected that since the handler must run in some place where it doesn't block the main loop, some form of concurrency will be exhibited. As such, we would require the atomicity of the Arc.

… moving it

This is convenient when the handler stores, or refers to outside state. By taking an atomic reference, we can simply clone the reference counting ptr and keep it around to inspect the state of the handler (assuming the handler allows inspection of its contents).

The cost compared to just moving the handler is negligible compared to the expected cost of the running lifetime of the handler.

Unfortunately, since thread::spawn takes an F: 'static, we can't just take the handler by reference. We may be able to use scoped threads and put the scope in the EventListener::FinishProcessingHandle, but doing so would require doing acrobatics with lifetimes beyond the added value, over the little added cost of introducing the Arc.

A bigger disadvantage of taking an Arc is that the Arc will be present in the EventListener::run_handler API.

Why Arc over Rc? It is expected that since the handler must run in some place where it doesn't block the main loop, some form of concurrency will be exhibited. As such, we would require the atomicity of the Arc.
@foresterre foresterre merged commit 7916b63 into main Jun 17, 2022
@foresterre foresterre deleted the arc_handler branch June 17, 2022 15:04
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.

1 participant