Skip to content

Commit eabdd05

Browse files
committed
runtime: document memory ordering for h_spans
h_spans can be accessed concurrently without synchronization from other threads, which means it needs the appropriate memory barriers on weakly ordered machines. It happens to already have the necessary memory barriers because all accesses to h_spans are currently protected by the heap lock and the unlocks happen in exactly the places where release barriers are needed, but it's easy to imagine that this could change in the future. Document the fact that we're depending on the barrier implied by the unlock. Related to issue #9984. Change-Id: I1bc3c95cd73361b041c8c95cd4bb92daf8c1f94a Reviewed-on: https://go-review.googlesource.com/11361 Reviewed-by: Rick Hudson <[email protected]> Reviewed-by: Dmitry Vyukov <[email protected]>
1 parent ef4a17b commit eabdd05

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/runtime/mheap.go

+12
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,16 @@ func mHeap_Alloc_m(h *mheap, npage uintptr, sizeclass int32, large bool) *mspan
444444
if trace.enabled {
445445
traceHeapAlloc()
446446
}
447+
448+
// h_spans is accessed concurrently without synchronization
449+
// from other threads. Hence, there must be a store/store
450+
// barrier here to ensure the writes to h_spans above happen
451+
// before the caller can publish a pointer p to an object
452+
// allocated from s. As soon as this happens, the garbage
453+
// collector running on another processor could read p and
454+
// look up s in h_spans. The unlock acts as the barrier to
455+
// order these writes. On the read side, the data dependency
456+
// between p and the index in h_spans orders the reads.
447457
unlock(&h.lock)
448458
return s
449459
}
@@ -479,6 +489,8 @@ func mHeap_AllocStack(h *mheap, npage uintptr) *mspan {
479489
s.ref = 0
480490
memstats.stacks_inuse += uint64(s.npages << _PageShift)
481491
}
492+
493+
// This unlock acts as a release barrier. See mHeap_Alloc_m.
482494
unlock(&h.lock)
483495
return s
484496
}

0 commit comments

Comments
 (0)