tls.go 627 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package cgotlstest
  5. // #include <pthread.h>
  6. // extern void setTLS(int);
  7. // extern int getTLS();
  8. import "C"
  9. import (
  10. "runtime"
  11. "testing"
  12. )
  13. func testTLS(t *testing.T) {
  14. runtime.LockOSThread()
  15. defer runtime.UnlockOSThread()
  16. if val := C.getTLS(); val != 0 {
  17. t.Fatalf("at start, C.getTLS() = %#x, want 0", val)
  18. }
  19. const keyVal = 0x1234
  20. C.setTLS(keyVal)
  21. if val := C.getTLS(); val != keyVal {
  22. t.Fatalf("at end, C.getTLS() = %#x, want %#x", val, keyVal)
  23. }
  24. }