Skip to content

Commit 304c6dc

Browse files
committed
kmc-solid: Synchronize the first update of ThreadInner::lifecycle with the second one on detach
The first update (swap RMW operation) must happen-before the second update so that the latter can release `ThreadInner` safely.
1 parent ae7633f commit 304c6dc

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

library/std/src/sys/itron/thread.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl Thread {
119119

120120
let old_lifecycle = inner
121121
.lifecycle
122-
.swap(LIFECYCLE_EXITED_OR_FINISHED_OR_JOIN_FINALIZE, Ordering::Release);
122+
.swap(LIFECYCLE_EXITED_OR_FINISHED_OR_JOIN_FINALIZE, Ordering::AcqRel);
123123

124124
match old_lifecycle {
125125
LIFECYCLE_DETACHED => {
@@ -129,9 +129,9 @@ impl Thread {
129129

130130
// In this case, `*p_inner`'s ownership has been moved to
131131
// us, and we are responsible for dropping it. The acquire
132-
// ordering is not necessary because the parent thread made
133-
// no memory access needing synchronization since the call
134-
// to `acre_tsk`.
132+
// ordering ensures that the swap operation that wrote
133+
// `LIFECYCLE_DETACHED` happens-before `Box::from_raw(
134+
// p_inner)`.
135135
// Safety: See above.
136136
let _ = unsafe { Box::from_raw(p_inner) };
137137

@@ -267,15 +267,15 @@ impl Drop for Thread {
267267
let inner = unsafe { self.p_inner.as_ref() };
268268

269269
// Detach the thread.
270-
match inner.lifecycle.swap(LIFECYCLE_DETACHED_OR_JOINED, Ordering::Acquire) {
270+
match inner.lifecycle.swap(LIFECYCLE_DETACHED_OR_JOINED, Ordering::AcqRel) {
271271
LIFECYCLE_INIT => {
272272
// [INIT → DETACHED]
273273
// When the time comes, the child will figure out that no
274274
// one will ever join it.
275275
// The ownership of `*p_inner` is moved to the child thread.
276-
// However, the release ordering is not necessary because we
277-
// made no memory access needing synchronization since the call
278-
// to `acre_tsk`.
276+
// The release ordering ensures that the above swap operation on
277+
// `lifecycle` happens-before the child thread's
278+
// `Box::from_raw(p_inner)`.
279279
}
280280
LIFECYCLE_FINISHED => {
281281
// [FINISHED → JOINED]

0 commit comments

Comments
 (0)