@@ -18,7 +18,6 @@ import (
18
18
"sync"
19
19
"sync/atomic"
20
20
"testing"
21
- "time"
22
21
"unsafe"
23
22
)
24
23
@@ -30,8 +29,7 @@ extern void doAdd(int, int);
30
29
void IntoC(void);
31
30
32
31
// issue 1560
33
- // mysleep returns the absolute start time in ms.
34
- long long mysleep(int seconds);
32
+ extern void Issue1560InC(void);
35
33
36
34
// twoSleep returns the absolute start time of the first sleep
37
35
// in ms.
@@ -183,35 +181,40 @@ func test1328(t *testing.T) {
183
181
}
184
182
185
183
// issue 1560
184
+ // Test that C functions and Go functions run in parallel.
186
185
187
- var sleepDone = make (chan int64 )
186
+ var (
187
+ issue1560 int32
188
188
189
- // parallelSleep returns the absolute difference between the start time
190
- // of the two sleeps.
191
- func parallelSleep (n int ) int64 {
192
- t := int64 (C .twoSleep (C .int (n ))) - <- sleepDone
193
- if t < 0 {
194
- return - t
189
+ issue1560Ch = make (chan bool , 2 )
190
+ )
191
+
192
+ //export Issue1560FromC
193
+ func Issue1560FromC () {
194
+ for atomic .LoadInt32 (& issue1560 ) != 1 {
195
+ runtime .Gosched ()
196
+ }
197
+ atomic .AddInt32 (& issue1560 , 1 )
198
+ for atomic .LoadInt32 (& issue1560 ) != 3 {
199
+ runtime .Gosched ()
195
200
}
196
- return t
201
+ issue1560Ch <- true
197
202
}
198
203
199
- //export BackgroundSleep
200
- func BackgroundSleep (n int32 ) {
201
- go func () {
202
- sleepDone <- int64 (C .mysleep (C .int (n )))
203
- }()
204
+ func Issue1560FromGo () {
205
+ atomic .AddInt32 (& issue1560 , 1 )
206
+ for atomic .LoadInt32 (& issue1560 ) != 2 {
207
+ runtime .Gosched ()
208
+ }
209
+ atomic .AddInt32 (& issue1560 , 1 )
210
+ issue1560Ch <- true
204
211
}
205
212
206
- func testParallelSleep (t * testing.T ) {
207
- sleepSec := 1
208
- dt := time .Duration (parallelSleep (sleepSec )) * time .Millisecond
209
- t .Logf ("difference in start time for two sleep(%d) is %v" , sleepSec , dt )
210
- // bug used to run sleeps in serial, producing a 2*sleepSec-second delay.
211
- // we detect if the start times of those sleeps are > 0.5*sleepSec-second.
212
- if dt >= time .Duration (sleepSec )* time .Second / 2 {
213
- t .Fatalf ("parallel %d-second sleeps slept for %f seconds" , sleepSec , dt .Seconds ())
214
- }
213
+ func test1560 (t * testing.T ) {
214
+ go Issue1560FromGo ()
215
+ go C .Issue1560InC ()
216
+ <- issue1560Ch
217
+ <- issue1560Ch
215
218
}
216
219
217
220
// issue 2462
0 commit comments