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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
package main
import (
"runtime"
"sync"
"time"
)
func init() {
println("init")
go println("goroutine in init")
time.Sleep(1 * time.Millisecond)
}
func main() {
println("main 1")
go sub()
time.Sleep(1 * time.Millisecond)
println("main 2")
time.Sleep(2 * time.Millisecond)
println("main 3")
// Await a blocking call.
println("wait:")
wait()
println("end waiting")
value := delayedValue()
println("value produced after some time:", value)
// Run a non-blocking call in a goroutine. This should be turned into a
// regular call, so should be equivalent to calling nowait() without 'go'
// prefix.
go nowait()
time.Sleep(time.Millisecond)
println("done with non-blocking goroutine")
var printer Printer
printer = &myPrinter{}
printer.Print()
sleepFuncValue(func(x int) {
time.Sleep(1 * time.Millisecond)
println("slept inside func pointer", x)
})
time.Sleep(1 * time.Millisecond)
n := 20
sleepFuncValue(func(x int) {
time.Sleep(1 * time.Millisecond)
println("slept inside closure, with value:", n, x)
})
time.Sleep(2 * time.Millisecond)
var x int
go func() {
time.Sleep(2 * time.Millisecond)
x = 1
}()
time.Sleep(time.Second / 2)
println("closure go call result:", x)
time.Sleep(2 * time.Millisecond)
var m sync.Mutex
m.Lock()
println("pre-acquired mutex")
go acquire(&m)
time.Sleep(2 * time.Millisecond)
println("releasing mutex")
m.Unlock()
time.Sleep(2 * time.Millisecond)
m.Lock()
println("re-acquired mutex")
m.Unlock()
println("done")
startSimpleFunc(emptyFunc)
time.Sleep(2 * time.Millisecond)
testGoOnBuiltins()
testGoOnInterface(Foo(0))
testCond()
testIssue1790()
done := make(chan int)
go testPaddedParameters(paddedStruct{x: 5, y: 7}, done)
<-done
}
func acquire(m *sync.Mutex) {
m.Lock()
println("acquired mutex from goroutine")
time.Sleep(2 * time.Millisecond)
println("releasing mutex from goroutine")
m.Unlock()
}
func sub() {
println("sub 1")
time.Sleep(2 * time.Millisecond)
println("sub 2")
}
func wait() {
println(" wait start")
time.Sleep(time.Millisecond)
println(" wait end")
}
func delayedValue() int {
time.Sleep(time.Millisecond)
return 42
}
func sleepFuncValue(fn func(int)) {
go fn(8)
}
func startSimpleFunc(fn simpleFunc) {
// Test that named function types don't crash the compiler.
go fn()
}
func nowait() {
println("non-blocking goroutine")
}
type Printer interface {
Print()
}
type myPrinter struct {
}
func (i *myPrinter) Print() {
time.Sleep(time.Millisecond)
println("async interface method call")
}
type simpleFunc func()
func emptyFunc() {
}
func testGoOnBuiltins() {
// Test copy builtin (there is no non-racy practical use of this).
go copy(make([]int, 8), []int{2, 5, 8, 4})
// Test recover builtin (no-op).
go recover()
// Test close builtin.
ch := make(chan int)
go close(ch)
n, ok := <-ch
if n != 0 || ok != false {
println("error: expected closed channel to return 0, false")
}
// Test delete builtin.
m := map[string]int{"foo": 3}
go delete(m, "foo")
time.Sleep(time.Millisecond)
v, ok := m["foo"]
if v != 0 || ok != false {
println("error: expected deleted map entry to be 0, false")
}
}
func testCond() {
var cond runtime.Cond
go func() {
// Wait for the caller to wait on the cond.
time.Sleep(time.Millisecond)
// Notify the caller.
ok := cond.Notify()
if !ok {
panic("notification not sent")
}
// This notification will be buffered inside the cond.
ok = cond.Notify()
if !ok {
panic("notification not queued")
}
// This notification should fail, since there is already one buffered.
ok = cond.Notify()
if ok {
panic("notification double-sent")
}
}()
// Verify that the cond has no pending notifications.
ok := cond.Poll()
if ok {
panic("unexpected early notification")
}
// Wait for the goroutine spawned earlier to send a notification.
cond.Wait()
// The goroutine should have also queued a notification in the cond.
ok = cond.Poll()
if !ok {
panic("missing queued notification")
}
}
var once sync.Once
func testGoOnInterface(f Itf) {
go f.Nowait()
time.Sleep(time.Millisecond)
go f.Wait()
time.Sleep(time.Millisecond * 2)
println("done with 'go on interface'")
}
// This tests a fix for issue 1790:
// https://github.com/tinygo-org/tinygo/issues/1790
func testIssue1790() *int {
once.Do(func() {})
i := 0
return &i
}
type Itf interface {
Nowait()
Wait()
}
type Foo string
func (f Foo) Nowait() {
println("called: Foo.Nowait")
}
func (f Foo) Wait() {
println("called: Foo.Wait")
time.Sleep(time.Microsecond)
println(" ...waited")
}
type paddedStruct struct {
x uint8
_ [0]int64
y uint8
}
// Structs with interesting padding used to crash.
func testPaddedParameters(s paddedStruct, done chan int) {
println("paddedStruct:", s.x, s.y)
close(done)
}
|