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