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

improved coroutine cancel semantics #1307

Merged
merged 21 commits into from
Jul 30, 2018
Merged

improved coroutine cancel semantics #1307

merged 21 commits into from
Jul 30, 2018

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Jul 30, 2018

This is a prerequisite of #1294.

This makes the alignment of promise a minimum of 8 (which was already the alignment on x86_64), which gives us 3 bits in the atomic state of a coroutine.

These 3 bits are used as follows (starting with LSB):

  • Cancel bit. Set when a coroutine is canceled.
  • Suspend bit. Set when a coroutine is suspended, and cleared when it is resumed.
  • Awaited bit. Set when a coroutine is awaited.

I had to add more atomicrmw operations to make this all work. The atomic ops are now:

  • suspend
    • atomicrmw OR itself (set the suspend bit, check if canceled)
  • resume
    • atomicrmw AND target (clear the suspend bit, check if canceled)
  • cancel
    • atomicrmw OR target (set the cancel bit)
  • await
    • atomicrmw OR target (set the await bit and store its handle, check if canceled).
    • atomicrmw OR itself (set the suspend bit)
  • return
    • atomicrmw OR itself (set the handle bits, check for await handle)
    • atomicrmw AND await handle (clear the suspend bit, check if canceled)

This fixes a few issues:

#1163 is still a problem - an awaiting coroutine is indistinguishable from a suspended coroutine to zig.

With this, cancel becomes thread-safe, just like await already is.

@andrewrk andrewrk merged commit 5d4a02c into master Jul 30, 2018
@andrewrk andrewrk deleted the cancel-semantics branch July 30, 2018 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant