github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/misc/cgo/testtls/tls.go (about)

     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  
     5  package cgotlstest
     6  
     7  // #include <pthread.h>
     8  // extern void setTLS(int);
     9  // extern int getTLS();
    10  import "C"
    11  
    12  import (
    13  	"runtime"
    14  	"testing"
    15  )
    16  
    17  func testTLS(t *testing.T) {
    18  	runtime.LockOSThread()
    19  	defer runtime.UnlockOSThread()
    20  
    21  	if val := C.getTLS(); val != 0 {
    22  		t.Fatalf("at start, C.getTLS() = %#x, want 0", val)
    23  	}
    24  
    25  	const keyVal = 0x1234
    26  	C.setTLS(keyVal)
    27  	if val := C.getTLS(); val != keyVal {
    28  		t.Fatalf("at end, C.getTLS() = %#x, want %#x", val, keyVal)
    29  	}
    30  }