Skip to content

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