[coop] Use bitfields for coop state machine state representation (mono/mono#17298)
authorAleksey Kliger (λgeek) <alklig@microsoft.com>
Tue, 22 Oct 2019 16:14:05 +0000 (12:14 -0400)
committerGitHub <noreply@github.com>
Tue, 22 Oct 2019 16:14:05 +0000 (12:14 -0400)
commit5dc174ed5ce7e6d1c7c3c69708ccdeb6415507d2
tree295e349236653c2d9ab0f5a701bf144951dbef22
parent4c044407aefb497db3d4968ff52670c3ff9c26c1
[coop] Use bitfields for coop state machine state representation (mono/mono#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)):

Commit migrated from https://github.com/mono/mono/commit/ebb5846e0d743a12a359f4b1dbcea9d18ccfe867
src/mono/mono/utils/checked-build.c
src/mono/mono/utils/mono-threads-coop.c
src/mono/mono/utils/mono-threads-state-machine.c
src/mono/mono/utils/mono-threads.c
src/mono/mono/utils/mono-threads.h