aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cgo
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2023-05-18 11:19:54 -0400
committerGopher Robot <gobot@golang.org>2023-05-18 16:55:07 +0000
commit88f89d87c4e818679b65cfc50a7b0cb554e154d3 (patch)
treefb6815282726ddcc3d9c34e0ee7f8a7a532ff7ef /src/cmd/cgo
parent33a601bf7d8e330d7c6ce1044f2c6a1df4f4fb5d (diff)
downloadgo-88f89d87c4e818679b65cfc50a7b0cb554e154d3.tar.xz
cmd/cgo/internal/test: fix TestThreadLock
This test was introduced in CL 18882, but only recently enabled as of CL 493603. It's intended to check that we don't move executing C code between threads when it re-enters Go, but it has always contained a flake. Go *can* preempt between the Go call to gettid and the C call to gettid and move the goroutine to another thread because there's no C code on the stack during the Go call to gettid. This will cause the test to fail. Fix this by making both gettid calls in C, with a re-entry to Go between them. Fixes #60265 Change-Id: I546621a541ce52b996d68b17d3bed709d2b5b1f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/496182 Auto-Submit: Austin Clements <austin@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Austin Clements <austin@google.com>
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r--src/cmd/cgo/internal/test/cgo_thread_lock.go10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/cmd/cgo/internal/test/cgo_thread_lock.go b/src/cmd/cgo/internal/test/cgo_thread_lock.go
index 981830b6b9..e8749384af 100644
--- a/src/cmd/cgo/internal/test/cgo_thread_lock.go
+++ b/src/cmd/cgo/internal/test/cgo_thread_lock.go
@@ -8,15 +8,19 @@ package cgotest
/*
#include <unistd.h>
+#include <stdbool.h>
#include <sys/syscall.h>
void Gosched(void);
-static int Ctid(void) { Gosched(); return syscall(SYS_gettid); }
+static bool Ctid(void) {
+ long tid1 = syscall(SYS_gettid);
+ Gosched();
+ return tid1 == syscall(SYS_gettid);
+}
*/
import "C"
import (
"runtime"
- "syscall"
"testing"
"time"
)
@@ -46,7 +50,7 @@ func testThreadLock(t *testing.T) {
defer close(stop)
for i := 0; i < 1000; i++ {
- if C.int(syscall.Gettid()) != C.Ctid() {
+ if !C.Ctid() {
t.Fatalf("cgo has not locked OS thread")
}
}