]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/test: remove timing dependency from TestParallelSleep
authorIan Lance Taylor <iant@golang.org>
Wed, 13 Apr 2022 21:35:15 +0000 (14:35 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 21 Apr 2022 18:43:55 +0000 (18:43 +0000)
Rename it TestIssue1560 since it no longer sleeps.

For #1560
Fixes #45586

Change-Id: I338eee9de43e871da142143943e9435218438e90
Reviewed-on: https://go-review.googlesource.com/c/go/+/400194
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

misc/cgo/test/callback_c.c
misc/cgo/test/cgo_test.go
misc/cgo/test/testx.go

index 8921b7306c6b148f13611ecaca6da4b47dcbfb98..8ecf70f2729c6ffe3383d16ca2bc95451499cf67 100644 (file)
@@ -3,8 +3,7 @@
 // license that can be found in the LICENSE file.
 
 #include <string.h>
-#include <sys/types.h>
-#include <unistd.h>
+
 #include "_cgo_export.h"
 
 void
@@ -31,32 +30,10 @@ IntoC(void)
        BackIntoGo();
 }
 
-#ifdef WIN32
-#include <windows.h>
-long long
-mysleep(int seconds) {
-       long long st = GetTickCount();
-       Sleep(1000 * seconds);
-       return st;
-}
-#else
-#include <sys/time.h>
-long long
-mysleep(int seconds) {
-       long long st;
-       struct timeval tv;
-       gettimeofday(&tv, NULL);
-       st = tv.tv_sec * 1000 + tv.tv_usec / 1000;
-       sleep(seconds);
-       return st;
-}
-#endif
-
-long long
-twoSleep(int n)
+void
+Issue1560InC(void)
 {
-       BackgroundSleep(n);
-       return mysleep(n);
+       Issue1560FromC();
 }
 
 void
index 774277e10da6a080cfa9040f11ac5692459d5e9d..dee61643544fdc6d0946e7bacd622a12ea9db1b5 100644 (file)
@@ -11,6 +11,7 @@ import "testing"
 // These wrappers are here for gotest to find.
 
 func Test1328(t *testing.T)                  { test1328(t) }
+func Test1560(t *testing.T)                  { test1560(t) }
 func Test1635(t *testing.T)                  { test1635(t) }
 func Test3250(t *testing.T)                  { test3250(t) }
 func Test3729(t *testing.T)                  { test3729(t) }
@@ -89,7 +90,6 @@ func TestLibgcc(t *testing.T)                { testLibgcc(t) }
 func TestMultipleAssign(t *testing.T)        { testMultipleAssign(t) }
 func TestNaming(t *testing.T)                { testNaming(t) }
 func TestPanicFromC(t *testing.T)            { testPanicFromC(t) }
-func TestParallelSleep(t *testing.T)         { testParallelSleep(t) }
 func TestPrintf(t *testing.T)                { testPrintf(t) }
 func TestReturnAfterGrow(t *testing.T)       { testReturnAfterGrow(t) }
 func TestReturnAfterGrowFromGo(t *testing.T) { testReturnAfterGrowFromGo(t) }
index 8ec84a8b22e01bcb6b6d78bd16f92d74e6d7cb7d..6a8e97ddf3f1ac30ce2870d6cdcd5db46e3ed8e2 100644 (file)
@@ -18,7 +18,6 @@ import (
        "sync"
        "sync/atomic"
        "testing"
-       "time"
        "unsafe"
 )
 
@@ -30,8 +29,7 @@ extern void doAdd(int, int);
 void IntoC(void);
 
 // issue 1560
-// mysleep returns the absolute start time in ms.
-long long mysleep(int seconds);
+extern void Issue1560InC(void);
 
 // twoSleep returns the absolute start time of the first sleep
 // in ms.
@@ -183,35 +181,40 @@ func test1328(t *testing.T) {
 }
 
 // issue 1560
+// Test that C functions and Go functions run in parallel.
 
-var sleepDone = make(chan int64)
+var (
+       issue1560 int32
 
-// parallelSleep returns the absolute difference between the start time
-// of the two sleeps.
-func parallelSleep(n int) int64 {
-       t := int64(C.twoSleep(C.int(n))) - <-sleepDone
-       if t < 0 {
-               return -t
+       issue1560Ch = make(chan bool, 2)
+)
+
+//export Issue1560FromC
+func Issue1560FromC() {
+       for atomic.LoadInt32(&issue1560) != 1 {
+               runtime.Gosched()
+       }
+       atomic.AddInt32(&issue1560, 1)
+       for atomic.LoadInt32(&issue1560) != 3 {
+               runtime.Gosched()
        }
-       return t
+       issue1560Ch <- true
 }
 
-//export BackgroundSleep
-func BackgroundSleep(n int32) {
-       go func() {
-               sleepDone <- int64(C.mysleep(C.int(n)))
-       }()
+func Issue1560FromGo() {
+       atomic.AddInt32(&issue1560, 1)
+       for atomic.LoadInt32(&issue1560) != 2 {
+               runtime.Gosched()
+       }
+       atomic.AddInt32(&issue1560, 1)
+       issue1560Ch <- true
 }
 
-func testParallelSleep(t *testing.T) {
-       sleepSec := 1
-       dt := time.Duration(parallelSleep(sleepSec)) * time.Millisecond
-       t.Logf("difference in start time for two sleep(%d) is %v", sleepSec, dt)
-       // bug used to run sleeps in serial, producing a 2*sleepSec-second delay.
-       // we detect if the start times of those sleeps are > 0.5*sleepSec-second.
-       if dt >= time.Duration(sleepSec)*time.Second/2 {
-               t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds())
-       }
+func test1560(t *testing.T) {
+       go Issue1560FromGo()
+       go C.Issue1560InC()
+       <-issue1560Ch
+       <-issue1560Ch
 }
 
 // issue 2462