[coop] Use bitfields for coop state machine state representation (#17298)
commitebb5846e0d743a12a359f4b1dbcea9d18ccfe867
authorAleksey Kliger (λgeek) <alklig@microsoft.com>
Tue, 22 Oct 2019 16:14:05 +0000 (22 12:14 -0400)
committerGitHub <noreply@github.com>
Tue, 22 Oct 2019 16:14:05 +0000 (22 12:14 -0400)
treec69bf468b4b2fef6d8301521c198f317fe8d3b26
parentf34fd20fbf009c9ef174c1fabdbccda47c93b9da
[coop] Use bitfields for coop state machine state representation (#17298)

* [checked] Mask the thread state in GC thread state assertions

Because we have extra bits (like suspend count and the no_checkpoints bit) on
the thread state, we have to mask it to get the actual thread state enum value

* [coop] Use bitfields to represent coop thread state

Instead of using a 32-bit int, use bitfields and type-punning through a union
type.  We need to type pun because we need to access the entire state as a 32-bit int so that we can do atomic CAS.

* use atomic load of thread state in unwrap_thread_state.

Saw one stack trace on CI on the full coop lane where check_thread_state saw STATE_RUNNING
but with a positive suspend count.  Another thread was starting a GC a
the time.  Seems like we saw the state as runnning, but the suspend
count as if we were already in async_suspend_requested.

Thread 22 (Thread 0x7f43c03ff700 (LWP 39860)):
mono/utils/checked-build.c
mono/utils/mono-threads-coop.c
mono/utils/mono-threads-state-machine.c
mono/utils/mono-threads.c
mono/utils/mono-threads.h