]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo/internal/testtls: cleanup and support more arches
authorAustin Clements <austin@google.com>
Thu, 18 May 2023 20:26:13 +0000 (16:26 -0400)
committerAustin Clements <austin@google.com>
Fri, 19 May 2023 01:37:39 +0000 (01:37 +0000)
Currently, this test only enabled on non-Darwin UNIX platforms because
it uses the non-standard _thread attribute for thread-local storage.
C11 introduced a standard way to declare something thread-local, so
this CL takes advantage of that to generalize the test to Darwin and
Windows.

Change-Id: Iba31b6216721df6eb8e978d7487cd3a787cae588
Reviewed-on: https://go-review.googlesource.com/c/go/+/496295
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Austin Clements <austin@google.com>

src/cmd/cgo/internal/testtls/tls.c
src/cmd/cgo/internal/testtls/tls.go
src/cmd/cgo/internal/testtls/tls_none.go

index 0e2bbee5422656658cc8669ccb332af46d3e9f41..8839cc86762c995cf3666f5663e46cc9f64e0d09 100644 (file)
@@ -2,13 +2,18 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Darwin does not have __thread.
+#include <stddef.h>
 
-//go:build cgo && unix && !darwin
+#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
 
-#include <pthread.h>
+// Mingw seems not to have threads.h, so we use the _Thread_local keyword rather
+// than the thread_local macro.
+static _Thread_local int tls;
 
-static __thread int tls;
+const char *
+checkTLS() {
+       return NULL;
+}
 
 void
 setTLS(int v)
@@ -21,3 +26,22 @@ getTLS()
 {
        return tls;
 }
+
+#else
+
+const char *
+checkTLS() {
+       return "_Thread_local requires C11 and not __STDC_NO_THREADS__";
+}
+
+void
+setTLS(int v) {
+}
+
+int
+getTLS()
+{
+       return 0;
+}
+
+#endif
index eb59ad41eb8548aea270883edab6f1bba86dc343..78628f5caa2fce5de51a0a03b7714fd89ac2d889 100644 (file)
@@ -2,11 +2,9 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build cgo && unix && !darwin
-
 package cgotlstest
 
-// #include <pthread.h>
+// extern const char *checkTLS();
 // extern void setTLS(int);
 // extern int getTLS();
 import "C"
@@ -17,6 +15,10 @@ import (
 )
 
 func testTLS(t *testing.T) {
+       if skip := C.checkTLS(); skip != nil {
+               t.Skipf("%s", C.GoString(skip))
+       }
+
        runtime.LockOSThread()
        defer runtime.UnlockOSThread()
 
index 81c9c5e23d5acdde3a0c07fd6057ba1dcafe5a86..b6033fb76d4335950e457a70c370f48ace76c3c1 100644 (file)
@@ -2,12 +2,12 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build !(cgo && unix && !darwin)
+//go:build !cgo
 
 package cgotlstest
 
 import "testing"
 
 func testTLS(t *testing.T) {
-       t.Skip("__thread is not supported")
+       t.Skip("cgo not supported")
 }