@@ -2,45 +2,17 @@ package main
22
33import (
44 "runtime"
5+ "sync"
56 "time"
67)
78
8- // waitGroup is a small type reimplementing some of the behavior of sync.WaitGroup
9- type waitGroup uint
10-
11- func (wg * waitGroup ) wait () {
12- n := 0
13- for * wg != 0 {
14- // pause and wait to be rescheduled
15- runtime .Gosched ()
16-
17- if n > 100 {
18- // if something is using the sleep queue, this may be necessary
19- time .Sleep (time .Millisecond )
20- }
21-
22- n ++
23- }
24- }
25-
26- func (wg * waitGroup ) add (n uint ) {
27- * wg += waitGroup (n )
28- }
29-
30- func (wg * waitGroup ) done () {
31- if * wg == 0 {
32- panic ("wait group underflow" )
33- }
34- * wg --
35- }
36-
37- var wg waitGroup
9+ var wg sync.WaitGroup
3810
3911func main () {
4012 ch := make (chan int )
4113 println ("len, cap of channel:" , len (ch ), cap (ch ), ch == nil )
4214
43- wg .add (1 )
15+ wg .Add (1 )
4416 go sender (ch )
4517
4618 n , ok := <- ch
@@ -50,7 +22,7 @@ func main() {
5022 println ("received num:" , n )
5123 }
5224
53- wg .wait ()
25+ wg .Wait ()
5426 n , ok = <- ch
5527 println ("recv from closed channel:" , n , ok )
5628
@@ -66,55 +38,55 @@ func main() {
6638
6739 // Test bigger values
6840 ch2 := make (chan complex128 )
69- wg .add (1 )
41+ wg .Add (1 )
7042 go sendComplex (ch2 )
7143 println ("complex128:" , <- ch2 )
72- wg .wait ()
44+ wg .Wait ()
7345
7446 // Test multi-sender.
7547 ch = make (chan int )
76- wg .add (3 )
48+ wg .Add (3 )
7749 go fastsender (ch , 10 )
7850 go fastsender (ch , 23 )
7951 go fastsender (ch , 40 )
8052 slowreceiver (ch )
81- wg .wait ()
53+ wg .Wait ()
8254
8355 // Test multi-receiver.
8456 ch = make (chan int )
85- wg .add (3 )
57+ wg .Add (3 )
8658 go fastreceiver (ch )
8759 go fastreceiver (ch )
8860 go fastreceiver (ch )
8961 slowsender (ch )
90- wg .wait ()
62+ wg .Wait ()
9163
9264 // Test iterator style channel.
9365 ch = make (chan int )
94- wg .add (1 )
66+ wg .Add (1 )
9567 go iterator (ch , 100 )
9668 sum := 0
9769 for i := range ch {
9870 sum += i
9971 }
100- wg .wait ()
72+ wg .Wait ()
10173 println ("sum(100):" , sum )
10274
10375 // Test simple selects.
10476 go selectDeadlock () // cannot use waitGroup here - never terminates
105- wg .add (1 )
77+ wg .Add (1 )
10678 go selectNoOp ()
107- wg .wait ()
79+ wg .Wait ()
10880
10981 // Test select with a single send operation (transformed into chan send).
11082 ch = make (chan int )
111- wg .add (1 )
83+ wg .Add (1 )
11284 go fastreceiver (ch )
11385 select {
11486 case ch <- 5 :
11587 }
11688 close (ch )
117- wg .wait ()
89+ wg .Wait ()
11890 println ("did send one" )
11991
12092 // Test select with a single recv operation (transformed into chan recv).
@@ -125,11 +97,11 @@ func main() {
12597
12698 // Test select recv with channel that has one entry.
12799 ch = make (chan int )
128- wg .add (1 )
100+ wg .Add (1 )
129101 go func (ch chan int ) {
130102 runtime .Gosched ()
131103 ch <- 55
132- wg .done ()
104+ wg .Done ()
133105 }(ch )
134106 select {
135107 case make (chan int ) <- 3 :
@@ -141,7 +113,7 @@ func main() {
141113 case n := <- make (chan int ):
142114 println ("unreachable:" , n )
143115 }
144- wg .wait ()
116+ wg .Wait ()
145117
146118 // Test select recv with closed channel.
147119 close (ch )
@@ -156,7 +128,7 @@ func main() {
156128
157129 // Test select send.
158130 ch = make (chan int )
159- wg .add (1 )
131+ wg .Add (1 )
160132 go fastreceiver (ch )
161133 select {
162134 case ch <- 235 :
@@ -165,7 +137,7 @@ func main() {
165137 println ("unreachable:" , n )
166138 }
167139 close (ch )
168- wg .wait ()
140+ wg .Wait ()
169141
170142 // test non-concurrent buffered channels
171143 ch = make (chan int , 2 )
@@ -183,15 +155,15 @@ func main() {
183155 println ("closed buffered channel recieve:" , <- ch )
184156
185157 // test using buffered channels as regular channels with special properties
186- wg .add (6 )
158+ wg .Add (6 )
187159 ch = make (chan int , 2 )
188160 go send (ch )
189161 go send (ch )
190162 go send (ch )
191163 go send (ch )
192164 go receive (ch )
193165 go receive (ch )
194- wg .wait ()
166+ wg .Wait ()
195167 close (ch )
196168 var count int
197169 for range ch {
@@ -204,19 +176,19 @@ func main() {
204176 sch1 := make (chan int )
205177 sch2 := make (chan int )
206178 sch3 := make (chan int )
207- wg .add (3 )
179+ wg .Add (3 )
208180 go func () {
209- defer wg .done ()
181+ defer wg .Done ()
210182 time .Sleep (time .Millisecond )
211183 sch1 <- 1
212184 }()
213185 go func () {
214- defer wg .done ()
186+ defer wg .Done ()
215187 time .Sleep (time .Millisecond )
216188 sch2 <- 2
217189 }()
218190 go func () {
219- defer wg .done ()
191+ defer wg .Done ()
220192 // merge sch2 and sch3 into ch
221193 for i := 0 ; i < 2 ; i ++ {
222194 var v int
@@ -240,18 +212,18 @@ func main() {
240212 sum += v
241213 }
242214 }
243- wg .wait ()
215+ wg .Wait ()
244216 println ("blocking select sum:" , sum )
245217}
246218
247219func send (ch chan <- int ) {
248220 ch <- 1
249- wg .done ()
221+ wg .Done ()
250222}
251223
252224func receive (ch <- chan int ) {
253225 <- ch
254- wg .done ()
226+ wg .Done ()
255227}
256228
257229func sender (ch chan int ) {
@@ -263,18 +235,18 @@ func sender(ch chan int) {
263235 ch <- i
264236 }
265237 close (ch )
266- wg .done ()
238+ wg .Done ()
267239}
268240
269241func sendComplex (ch chan complex128 ) {
270242 ch <- 7 + 10.5i
271- wg .done ()
243+ wg .Done ()
272244}
273245
274246func fastsender (ch chan int , n int ) {
275247 ch <- n
276248 ch <- n + 1
277- wg .done ()
249+ wg .Done ()
278250}
279251
280252func slowreceiver (ch chan int ) {
@@ -300,15 +272,15 @@ func fastreceiver(ch chan int) {
300272 sum += n
301273 }
302274 println ("sum:" , sum )
303- wg .done ()
275+ wg .Done ()
304276}
305277
306278func iterator (ch chan int , top int ) {
307279 for i := 0 ; i < top ; i ++ {
308280 ch <- i
309281 }
310282 close (ch )
311- wg .done ()
283+ wg .Done ()
312284}
313285
314286func selectDeadlock () {
@@ -323,5 +295,5 @@ func selectNoOp() {
323295 default :
324296 }
325297 println ("after no-op" )
326- wg .done ()
298+ wg .Done ()
327299}
0 commit comments