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