Skip to content

Commit 8044b77

Browse files
committed
runtime: eliminate write barriers from dropg
Currently this contains no write barriers because it's writing nil pointers, but with the hybrid barrier, even these will produce write barriers. However, since these are *gs and *ms, they don't need write barriers, so we can simply eliminate them. Updates #17503. Change-Id: Ib188a60492c5cfb352814bf9b2bcb2941fb7d6c0 Reviewed-on: https://go-review.googlesource.com/31570 Reviewed-by: Rick Hudson <[email protected]>
1 parent 85c22bc commit 8044b77

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/runtime/proc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,8 +2175,8 @@ top:
21752175
func dropg() {
21762176
_g_ := getg()
21772177

2178-
_g_.m.curg.m = nil
2179-
_g_.m.curg = nil
2178+
setMNoWB(&_g_.m.curg.m, nil)
2179+
setGNoWB(&_g_.m.curg, nil)
21802180
}
21812181

21822182
func parkunlock_c(gp *g, lock unsafe.Pointer) bool {

src/runtime/runtime2.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ func (gp *guintptr) cas(old, new guintptr) bool {
205205
return atomic.Casuintptr((*uintptr)(unsafe.Pointer(gp)), uintptr(old), uintptr(new))
206206
}
207207

208+
// setGNoWB performs *gp = new without a write barrier.
209+
// For times when it's impractical to use a guintptr.
210+
//go:nosplit
211+
//go:nowritebarrier
212+
func setGNoWB(gp **g, new *g) {
213+
(*guintptr)(unsafe.Pointer(gp)).set(new)
214+
}
215+
208216
type puintptr uintptr
209217

210218
//go:nosplit
@@ -221,6 +229,14 @@ func (mp muintptr) ptr() *m { return (*m)(unsafe.Pointer(mp)) }
221229
//go:nosplit
222230
func (mp *muintptr) set(m *m) { *mp = muintptr(unsafe.Pointer(m)) }
223231

232+
// setMNoWB performs *mp = new without a write barrier.
233+
// For times when it's impractical to use an muintptr.
234+
//go:nosplit
235+
//go:nowritebarrier
236+
func setMNoWB(mp **m, new *m) {
237+
(*muintptr)(unsafe.Pointer(mp)).set(new)
238+
}
239+
224240
type gobuf struct {
225241
// The offsets of sp, pc, and g are known to (hard-coded in) libmach.
226242
sp uintptr

0 commit comments

Comments
 (0)