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

Remove AtomicBool from Waker #18

Closed
tustvold opened this issue Aug 6, 2019 · 3 comments
Closed

Remove AtomicBool from Waker #18

tustvold opened this issue Aug 6, 2019 · 3 comments

Comments

@tustvold
Copy link

tustvold commented Aug 6, 2019

This might be a conceptual misunderstanding on my part, but I believe you can remove the AtomicBool from Waker and this would remove the static lifetime requirement, as the Waker would then have no state - it could be a nullptr and the vtable static.

The key observation is it is not illegal to call poll again on a future that has returned Pending but not invoked the waker, just ill-advised if you can do better. In this case we can't - all the AtomicBool does is move the location of the busy wait.

On armv6m and armv7m the AtomicBool is ultimately duplicating the functionality already provided by the wfe - assuming it isn't actually treated as a noop, in which case busy waiting is the best you can do anyway.

If my reasoning is wrong, I'd be very interested to know as I'm currently working on a PoC embedded_hal, see here, that somewhat relies on this logic being correct...

@tustvold
Copy link
Author

tustvold commented Aug 8, 2019

This logic is not correct as a function can call a waker prior to returning Pending and expect to be re-enqueued immediately. The proposed use of wfe and sev wouldn't handle this correctly.

@tustvold tustvold closed this as completed Aug 8, 2019
@Nemo157
Copy link
Owner

Nemo157 commented Aug 8, 2019

I was planning on taking some time to dig into this. One reason for having an atomic bool there is to provide better performance on spuriously exiting sleep, e.g. for wfe if an event occurs which is unrelated to what the future is currently waiting for. I was also thinking about extending the executor to support multiple futures using static allocations, which would need some way to know which was woken, for a small number of futures iterating over them checking the bools seems performant enough (though I never got around to implementing this)

I think removing it could still be logically correct in the current implementations, as far as I'm aware wfe will immediately return if an event was triggered between the last time it was called and this call, i.e. wfe(); sev(); wfe() is guaranteed to continue execution over the second wfe().

@tustvold
Copy link
Author

tustvold commented Aug 8, 2019

You are correct, I did wonder why I couldn't produce the "expected" behaviour. See here.

I would have thought that in most cases the underlying poll operation will simply be checking a bit in a register and so I'm not sure that the AtomicBool would necessarily perform better. Happy for you to reopen

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

No branches or pull requests

2 participants