github.com/bir3/gocompiler@v0.9.2202/src/cmd/cgo/internal/test/cgo_thread_lock.go (about) 1 // Copyright 2016 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 //go:build linux 6 7 package cgotest 8 9 /* 10 #include <unistd.h> 11 #include <stdbool.h> 12 #include <sys/syscall.h> 13 void Gosched(void); 14 static bool Ctid(void) { 15 long tid1 = syscall(SYS_gettid); 16 Gosched(); 17 return tid1 == syscall(SYS_gettid); 18 } 19 */ 20 import "C" 21 22 import ( 23 "runtime" 24 "testing" 25 "time" 26 ) 27 28 //export Gosched 29 func Gosched() { 30 runtime.Gosched() 31 } 32 33 func init() { 34 testThreadLockFunc = testThreadLock 35 } 36 37 func testThreadLock(t *testing.T) { 38 stop := make(chan int) 39 go func() { 40 // We need the G continue running, 41 // so the M has a chance to run this G. 42 for { 43 select { 44 case <-stop: 45 return 46 case <-time.After(time.Millisecond * 100): 47 } 48 } 49 }() 50 defer close(stop) 51 52 for i := 0; i < 1000; i++ { 53 if !C.Ctid() { 54 t.Fatalf("cgo has not locked OS thread") 55 } 56 } 57 }