@@ -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 :
@@ -139,7 +111,7 @@ func main() {
139111 case n := <- make (chan int ):
140112 println ("unreachable:" , n )
141113 }
142- wg .wait ()
114+ wg .Wait ()
143115
144116 // Test select recv with closed channel.
145117 close (ch )
@@ -154,7 +126,7 @@ func main() {
154126
155127 // Test select send.
156128 ch = make (chan int )
157- wg .add (1 )
129+ wg .Add (1 )
158130 go fastreceiver (ch )
159131 select {
160132 case ch <- 235 :
@@ -163,7 +135,7 @@ func main() {
163135 println ("unreachable:" , n )
164136 }
165137 close (ch )
166- wg .wait ()
138+ wg .Wait ()
167139
168140 // test non-concurrent buffered channels
169141 ch = make (chan int , 2 )
@@ -181,15 +153,15 @@ func main() {
181153 println ("closed buffered channel recieve:" , <- ch )
182154
183155 // test using buffered channels as regular channels with special properties
184- wg .add (6 )
156+ wg .Add (6 )
185157 ch = make (chan int , 2 )
186158 go send (ch )
187159 go send (ch )
188160 go send (ch )
189161 go send (ch )
190162 go receive (ch )
191163 go receive (ch )
192- wg .wait ()
164+ wg .Wait ()
193165 close (ch )
194166 var count int
195167 for range ch {
@@ -202,19 +174,19 @@ func main() {
202174 sch1 := make (chan int )
203175 sch2 := make (chan int )
204176 sch3 := make (chan int )
205- wg .add (3 )
177+ wg .Add (3 )
206178 go func () {
207- defer wg .done ()
179+ defer wg .Done ()
208180 time .Sleep (time .Millisecond )
209181 sch1 <- 1
210182 }()
211183 go func () {
212- defer wg .done ()
184+ defer wg .Done ()
213185 time .Sleep (time .Millisecond )
214186 sch2 <- 2
215187 }()
216188 go func () {
217- defer wg .done ()
189+ defer wg .Done ()
218190 // merge sch2 and sch3 into ch
219191 for i := 0 ; i < 2 ; i ++ {
220192 var v int
@@ -238,18 +210,18 @@ func main() {
238210 sum += v
239211 }
240212 }
241- wg .wait ()
213+ wg .Wait ()
242214 println ("blocking select sum:" , sum )
243215}
244216
245217func send (ch chan <- int ) {
246218 ch <- 1
247- wg .done ()
219+ wg .Done ()
248220}
249221
250222func receive (ch <- chan int ) {
251223 <- ch
252- wg .done ()
224+ wg .Done ()
253225}
254226
255227func sender (ch chan int ) {
@@ -261,18 +233,18 @@ func sender(ch chan int) {
261233 ch <- i
262234 }
263235 close (ch )
264- wg .done ()
236+ wg .Done ()
265237}
266238
267239func sendComplex (ch chan complex128 ) {
268240 ch <- 7 + 10.5i
269- wg .done ()
241+ wg .Done ()
270242}
271243
272244func fastsender (ch chan int , n int ) {
273245 ch <- n
274246 ch <- n + 1
275- wg .done ()
247+ wg .Done ()
276248}
277249
278250func slowreceiver (ch chan int ) {
@@ -298,15 +270,15 @@ func fastreceiver(ch chan int) {
298270 sum += n
299271 }
300272 println ("sum:" , sum )
301- wg .done ()
273+ wg .Done ()
302274}
303275
304276func iterator (ch chan int , top int ) {
305277 for i := 0 ; i < top ; i ++ {
306278 ch <- i
307279 }
308280 close (ch )
309- wg .done ()
281+ wg .Done ()
310282}
311283
312284func selectDeadlock () {
@@ -321,5 +293,5 @@ func selectNoOp() {
321293 default :
322294 }
323295 println ("after no-op" )
324- wg .done ()
296+ wg .Done ()
325297}
0 commit comments