summaryrefslogtreecommitdiffstats
path: root/misc/cgo/testtls
diff options
context:
space:
mode:
Diffstat (limited to 'misc/cgo/testtls')
-rw-r--r--misc/cgo/testtls/tls.go30
-rw-r--r--misc/cgo/testtls/tls_test.go13
-rw-r--r--misc/cgo/testtls/tls_unix.c19
3 files changed, 62 insertions, 0 deletions
diff --git a/misc/cgo/testtls/tls.go b/misc/cgo/testtls/tls.go
new file mode 100644
index 0000000..e634220
--- /dev/null
+++ b/misc/cgo/testtls/tls.go
@@ -0,0 +1,30 @@
+// 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
+
+// #include <pthread.h>
+// extern void setTLS(int);
+// extern int getTLS();
+import "C"
+
+import (
+ "runtime"
+ "testing"
+)
+
+func testTLS(t *testing.T) {
+ 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/misc/cgo/testtls/tls_test.go b/misc/cgo/testtls/tls_test.go
new file mode 100644
index 0000000..3076c2d
--- /dev/null
+++ b/misc/cgo/testtls/tls_test.go
@@ -0,0 +1,13 @@
+// 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.
+
+// +build !windows
+
+package cgotlstest
+
+import "testing"
+
+func TestTLS(t *testing.T) {
+ testTLS(t)
+}
diff --git a/misc/cgo/testtls/tls_unix.c b/misc/cgo/testtls/tls_unix.c
new file mode 100644
index 0000000..957afce
--- /dev/null
+++ b/misc/cgo/testtls/tls_unix.c
@@ -0,0 +1,19 @@
+// 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 <pthread.h>
+
+static __thread int tls;
+
+void
+setTLS(int v)
+{
+ tls = v;
+}
+
+int
+getTLS()
+{
+ return tls;
+}