Debian Codegraph @

stdlib/generic/go1_21_sync.go.txt

raw
1 // Copyright 2022 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 sync
6
7 import (
8 "sync/atomic"
9 )
10
11 // OnceFunc returns a function that invokes f only once. The returned function
12 // may be called concurrently.
13 //
14 // If f panics, the returned function will panic with the same value on every call.
15 func OnceFunc(f func()) func() {
16 var (
17 once Once
18 valid bool
19 p any
20 )
21 // Construct the inner closure just once to reduce costs on the fast path.
22 g := func() {
23 defer func() {
24 p = recover()
25 if !valid {
26 // Re-panic immediately so on the first call the user gets a
27 // complete stack trace into f.
28 panic(p)
29 }
30 }()
31 f()
32 valid = true // Set only if f does not panic
33 }
34 return func() {
35 once.Do(g)
36 if !valid {
37 panic(p)
38 }
39 }
40 }
41
42 // OnceValue returns a function that invokes f only once and returns the value
43 // returned by f. The returned function may be called concurrently.
44 //
45 // If f panics, the returned function will panic with the same value on every call.
46 func OnceValue[T any](f func() T) func() T {
47 var (
48 once Once
49 valid bool
50 p any
51 result T
52 )
53 g := func() {
54 defer func() {
55 p = recover()
56 if !valid {
57 panic(p)
58 }
59 }()
60 result = f()
61 valid = true
62 }
63 return func() T {
64 once.Do(g)
65 if !valid {
66 panic(p)
67 }
68 return result
69 }
70 }
71
72 // OnceValues returns a function that invokes f only once and returns the values
73 // returned by f. The returned function may be called concurrently.
74 //
75 // If f panics, the returned function will panic with the same value on every call.
76 func OnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2) {
77 var (
78 once Once
79 valid bool
80 p any
81 r1 T1
82 r2 T2
83 )
84 g := func() {
85 defer func() {
86 p = recover()
87 if !valid {
88 panic(p)
89 }
90 }()
91 r1, r2 = f()
92 valid = true
93 }
94 return func() (T1, T2) {
95 once.Do(g)
96 if !valid {
97 panic(p)
98 }
99 return r1, r2
100 }
101 }
102 // Copyright 2009 The Go Authors. All rights reserved.
103 // Use of this source code is governed by a BSD-style
104 // license that can be found in the LICENSE file.
105
106 //package sync
107
108 // import (
109 // "sync/atomic"
110 // )
111
112 // Once is an object that will perform exactly one action.
113 //
114 // A Once must not be copied after first use.
115 //
116 // In the terminology of the Go memory model,
117 // the return from f “synchronizes before”
118 // the return from any call of once.Do(f).
119 type Once struct {
120 // done indicates whether the action has been performed.
121 // It is first in the struct because it is used in the hot path.
122 // The hot path is inlined at every call site.
123 // Placing done first allows more compact instructions on some architectures (amd64/386),
124 // and fewer instructions (to calculate offset) on other architectures.
125 done uint32
126 m Mutex
127 }
128
129 // Do calls the function f if and only if Do is being called for the
130 // first time for this instance of Once. In other words, given
131 //
132 // var once Once
133 //
134 // if once.Do(f) is called multiple times, only the first call will invoke f,
135 // even if f has a different value in each invocation. A new instance of
136 // Once is required for each function to execute.
137 //
138 // Do is intended for initialization that must be run exactly once. Since f
139 // is niladic, it may be necessary to use a function literal to capture the
140 // arguments to a function to be invoked by Do:
141 //
142 // config.once.Do(func() { config.init(filename) })
143 //
144 // Because no call to Do returns until the one call to f returns, if f causes
145 // Do to be called, it will deadlock.
146 //
147 // If f panics, Do considers it to have returned; future calls of Do return
148 // without calling f.
149 func (o *Once) Do(f func()) {
150 // Note: Here is an incorrect implementation of Do:
151 //
152 // if atomic.CompareAndSwapUint32(&o.done, 0, 1) {
153 // f()
154 // }
155 //
156 // Do guarantees that when it returns, f has finished.
157 // This implementation would not implement that guarantee:
158 // given two simultaneous calls, the winner of the cas would
159 // call f, and the second would return immediately, without
160 // waiting for the first's call to f to complete.
161 // This is why the slow path falls back to a mutex, and why
162 // the atomic.StoreUint32 must be delayed until after f returns.
163
164 if atomic.LoadUint32(&o.done) == 0 {
165 // Outlined slow-path to allow inlining of the fast-path.
166 o.doSlow(f)
167 }
168 }
169
170 func (o *Once) doSlow(f func()) {
171 o.m.Lock()
172 defer o.m.Unlock()
173 if o.done == 0 {
174 defer atomic.StoreUint32(&o.done, 1)
175 f()
176 }
177 }