github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/misc/cgo/test/issue3775.go (about)

     1  package cgotest
     2  
     3  /*
     4  void lockOSThreadCallback(void);
     5  inline static void lockOSThreadC(void)
     6  {
     7          lockOSThreadCallback();
     8  }
     9  int usleep(unsigned usec);
    10  */
    11  import "C"
    12  
    13  import (
    14  	"runtime"
    15  	"testing"
    16  )
    17  
    18  func init() {
    19  	// Same as test3775 but run during init so that
    20  	// there are two levels of internal runtime lock
    21  	// (1 for init, 1 for cgo).
    22  	// This would have been broken by CL 11663043.
    23  	C.lockOSThreadC()
    24  }
    25  
    26  func test3775(t *testing.T) {
    27  	// Used to panic because of the UnlockOSThread below.
    28  	C.lockOSThreadC()
    29  }
    30  
    31  //export lockOSThreadCallback
    32  func lockOSThreadCallback() {
    33  	runtime.LockOSThread()
    34  	runtime.UnlockOSThread()
    35  	go C.usleep(10000)
    36  	runtime.Gosched()
    37  }