Brad Fitzpatrick | 5194744 | 2016-03-01 22:57:46 +0000 | [diff] [blame] | 1 | // Copyright 2012 The Go Authors. All rights reserved. |
Russ Cox | dc159fa | 2012-03-01 11:48:17 -0500 | [diff] [blame] | 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" |
Brad Fitzpatrick | 1e3f563 | 2017-11-21 20:46:40 +0000 | [diff] [blame] | 11 | "time" |
Brad Fitzpatrick | 70418eb | 2016-02-11 16:36:38 +0000 | [diff] [blame] | 12 | "unsafe" |
Russ Cox | dc159fa | 2012-03-01 11:48:17 -0500 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var pid, tid int |
| 16 | |
| 17 | func init() { |
| 18 | // Record pid and tid of init thread for use during test. |
| 19 | // The call to LockOSThread is just to exercise it; |
| 20 | // we can't test that it does anything. |
| 21 | // Instead we're testing that the conditions are good |
| 22 | // for how it is used in init (must be on main thread). |
| 23 | pid, tid = syscall.Getpid(), syscall.Gettid() |
| 24 | LockOSThread() |
Brad Fitzpatrick | 1e3f563 | 2017-11-21 20:46:40 +0000 | [diff] [blame] | 25 | |
| 26 | sysNanosleep = func(d time.Duration) { |
| 27 | // Invoke a blocking syscall directly; calling time.Sleep() |
| 28 | // would deschedule the goroutine instead. |
| 29 | ts := syscall.NsecToTimespec(d.Nanoseconds()) |
| 30 | for { |
| 31 | if err := syscall.Nanosleep(&ts, &ts); err != syscall.EINTR { |
| 32 | return |
| 33 | } |
| 34 | } |
| 35 | } |
Russ Cox | dc159fa | 2012-03-01 11:48:17 -0500 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func TestLockOSThread(t *testing.T) { |
| 39 | if pid != tid { |
| 40 | t.Fatalf("pid=%d but tid=%d", pid, tid) |
| 41 | } |
| 42 | } |
Brad Fitzpatrick | 70418eb | 2016-02-11 16:36:38 +0000 | [diff] [blame] | 43 | |
| 44 | // Test that error values are negative. Use address 1 (a misaligned |
| 45 | // pointer) to get -EINVAL. |
| 46 | func TestMincoreErrorSign(t *testing.T) { |
| 47 | var dst byte |
| 48 | v := Mincore(unsafe.Pointer(uintptr(1)), 1, &dst) |
| 49 | |
| 50 | const EINVAL = 0x16 |
| 51 | if v != -EINVAL { |
| 52 | t.Errorf("mincore = %v, want %v", v, -EINVAL) |
| 53 | } |
| 54 | } |