]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/test/cgo_thread_lock.go
3b9ac845493cd5cfd4d9a665e56e94cab2139956
[gostls13.git] / misc / cgo / test / cgo_thread_lock.go
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 && freebsd && openbsd
6 // +build linux,freebsd,openbsd
7
8 package cgotest
9
10 /*
11 #include <unistd.h>
12 #include <sys/syscall.h>
13 void Gosched(void);
14 static int Ctid(void) { Gosched(); return syscall(SYS_gettid); }
15 */
16 import "C"
17
18 import (
19         "runtime"
20         "syscall"
21         "testing"
22         "time"
23 )
24
25 //export Gosched
26 func Gosched() {
27         runtime.Gosched()
28 }
29
30 func init() {
31         testThreadLockFunc = testThreadLock
32 }
33
34 func testThreadLock(t *testing.T) {
35         stop := make(chan int)
36         go func() {
37                 // We need the G continue running,
38                 // so the M has a chance to run this G.
39                 for {
40                         select {
41                         case <-stop:
42                                 return
43                         case <-time.After(time.Millisecond * 100):
44                         }
45                 }
46         }()
47         defer close(stop)
48
49         for i := 0; i < 1000; i++ {
50                 if C.int(syscall.Gettid()) != C.Ctid() {
51                         t.Fatalf("cgo has not locked OS thread")
52                 }
53         }
54 }