Source file src/internal/synctest/synctest.go
1 // Copyright 2024 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package synctest provides support for testing concurrent code. 6 // 7 // See the testing/synctest package for function documentation. 8 package synctest 9 10 import ( 11 _ "unsafe" // for go:linkname 12 ) 13 14 //go:linkname Run 15 func Run(f func()) 16 17 //go:linkname Wait 18 func Wait() 19 20 //go:linkname acquire 21 func acquire() any 22 23 //go:linkname release 24 func release(any) 25 26 //go:linkname inBubble 27 func inBubble(any, func()) 28 29 // A Bubble is a synctest bubble. 30 // 31 // Not a public API. Used by syscall/js to propagate bubble membership through syscalls. 32 type Bubble struct { 33 b any 34 } 35 36 // Acquire returns a reference to the current goroutine's bubble. 37 // The bubble will not become idle until Release is called. 38 func Acquire() *Bubble { 39 if b := acquire(); b != nil { 40 return &Bubble{b} 41 } 42 return nil 43 } 44 45 // Release releases the reference to the bubble, 46 // allowing it to become idle again. 47 func (b *Bubble) Release() { 48 if b == nil { 49 return 50 } 51 release(b.b) 52 b.b = nil 53 } 54 55 // Run executes f in the bubble. 56 // The current goroutine must not be part of a bubble. 57 func (b *Bubble) Run(f func()) { 58 if b == nil { 59 f() 60 } else { 61 inBubble(b.b, f) 62 } 63 } 64