summaryrefslogtreecommitdiffstats
path: root/src/cmd/cgo/internal/testtls
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/cgo/internal/testtls')
-rw-r--r--src/cmd/cgo/internal/testtls/tls.c47
-rw-r--r--src/cmd/cgo/internal/testtls/tls.go34
-rw-r--r--src/cmd/cgo/internal/testtls/tls_none.go13
-rw-r--r--src/cmd/cgo/internal/testtls/tls_test.go11
4 files changed, 105 insertions, 0 deletions
diff --git a/src/cmd/cgo/internal/testtls/tls.c b/src/cmd/cgo/internal/testtls/tls.c
new file mode 100644
index 0000000..8839cc8
--- /dev/null
+++ b/src/cmd/cgo/internal/testtls/tls.c
@@ -0,0 +1,47 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include <stddef.h>
+
+#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
+
+// 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;
+
+const char *
+checkTLS() {
+ return NULL;
+}
+
+void
+setTLS(int v)
+{
+ tls = v;
+}
+
+int
+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
diff --git a/src/cmd/cgo/internal/testtls/tls.go b/src/cmd/cgo/internal/testtls/tls.go
new file mode 100644
index 0000000..78628f5
--- /dev/null
+++ b/src/cmd/cgo/internal/testtls/tls.go
@@ -0,0 +1,34 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgotlstest
+
+// extern const char *checkTLS();
+// extern void setTLS(int);
+// extern int getTLS();
+import "C"
+
+import (
+ "runtime"
+ "testing"
+)
+
+func testTLS(t *testing.T) {
+ if skip := C.checkTLS(); skip != nil {
+ t.Skipf("%s", C.GoString(skip))
+ }
+
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ if val := C.getTLS(); val != 0 {
+ t.Fatalf("at start, C.getTLS() = %#x, want 0", val)
+ }
+
+ const keyVal = 0x1234
+ C.setTLS(keyVal)
+ if val := C.getTLS(); val != keyVal {
+ t.Fatalf("at end, C.getTLS() = %#x, want %#x", val, keyVal)
+ }
+}
diff --git a/src/cmd/cgo/internal/testtls/tls_none.go b/src/cmd/cgo/internal/testtls/tls_none.go
new file mode 100644
index 0000000..b6033fb
--- /dev/null
+++ b/src/cmd/cgo/internal/testtls/tls_none.go
@@ -0,0 +1,13 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build !cgo
+
+package cgotlstest
+
+import "testing"
+
+func testTLS(t *testing.T) {
+ t.Skip("cgo not supported")
+}
diff --git a/src/cmd/cgo/internal/testtls/tls_test.go b/src/cmd/cgo/internal/testtls/tls_test.go
new file mode 100644
index 0000000..8e14add
--- /dev/null
+++ b/src/cmd/cgo/internal/testtls/tls_test.go
@@ -0,0 +1,11 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cgotlstest
+
+import "testing"
+
+func TestTLS(t *testing.T) {
+ testTLS(t)
+}