| 1 |
// Copyright 2012 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 runtime_test |
| 6 |
|
| 7 |
import ( |
| 8 |
. "runtime" |
| 9 |
"syscall" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
var pid, tid int |
| 15 |
|
| 16 |
func init() { |
| 17 |
// Record pid and tid of init thread for use during test. |
| 18 |
// The call to LockOSThread is just to exercise it; |
| 19 |
// we can't test that it does anything. |
| 20 |
// Instead we're testing that the conditions are good |
| 21 |
// for how it is used in init (must be on main thread). |
| 22 |
pid, tid = syscall.Getpid(), syscall.Gettid() |
| 23 |
LockOSThread() |
| 24 |
|
| 25 |
sysNanosleep = func(d time.Duration) { |
| 26 |
// Invoke a blocking syscall directly; calling time.Sleep() |
| 27 |
// would deschedule the goroutine instead. |
| 28 |
ts := syscall.NsecToTimespec(d.Nanoseconds()) |
| 29 |
for { |
| 30 |
if err := syscall.Nanosleep(&ts, &ts); err != syscall.EINTR { |
| 31 |
return |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestLockOSThread(t *testing.T) { |
| 38 |
if pid != tid { |
| 39 |
t.Fatalf("pid=%d but tid=%d", pid, tid) |
| 40 |
} |
| 41 |
} |