2013-03-18 13:11:11 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-03-07 15:34:40 -07:00
|
|
|
// Futex is only available on DragonFly BSD, FreeBSD and Linux.
|
|
|
|
// The race detector emits calls to split stack functions so it breaks
|
|
|
|
// the test.
|
|
|
|
|
2013-08-23 09:50:24 -06:00
|
|
|
// +build dragonfly freebsd linux
|
2013-07-29 12:22:34 -06:00
|
|
|
// +build !race
|
2013-03-18 13:11:11 -06:00
|
|
|
|
|
|
|
package runtime_test
|
|
|
|
|
|
|
|
import (
|
2014-03-07 15:34:40 -07:00
|
|
|
"runtime"
|
2016-01-13 13:14:26 -07:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2013-03-18 13:11:11 -06:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-03-07 15:34:40 -07:00
|
|
|
type futexsleepTest struct {
|
|
|
|
mtx uint32
|
|
|
|
ns int64
|
|
|
|
msg string
|
2016-01-13 13:14:26 -07:00
|
|
|
ch chan *futexsleepTest
|
2014-03-07 15:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var futexsleepTests = []futexsleepTest{
|
2016-01-13 13:14:26 -07:00
|
|
|
beforeY2038: {mtx: 0, ns: 86400 * 1e9, msg: "before the year 2038"},
|
|
|
|
afterY2038: {mtx: 0, ns: (1<<31 + 100) * 1e9, msg: "after the year 2038"},
|
2014-03-07 15:34:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
beforeY2038 = iota
|
|
|
|
afterY2038
|
|
|
|
)
|
|
|
|
|
2013-03-18 13:11:11 -06:00
|
|
|
func TestFutexsleep(t *testing.T) {
|
2014-03-09 21:20:16 -06:00
|
|
|
if runtime.GOMAXPROCS(0) > 1 {
|
|
|
|
// futexsleep doesn't handle EINTR or other signals,
|
|
|
|
// so spurious wakeups may happen.
|
|
|
|
t.Skip("skipping; GOMAXPROCS>1")
|
|
|
|
}
|
|
|
|
|
2013-03-18 13:11:11 -06:00
|
|
|
start := time.Now()
|
2016-01-13 13:14:26 -07:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := range futexsleepTests {
|
|
|
|
tt := &futexsleepTests[i]
|
|
|
|
tt.mtx = 0
|
|
|
|
tt.ch = make(chan *futexsleepTest, 1)
|
|
|
|
wg.Add(1)
|
|
|
|
go func(tt *futexsleepTest) {
|
2014-11-11 15:08:33 -07:00
|
|
|
runtime.Entersyscall(0)
|
2016-01-13 13:14:26 -07:00
|
|
|
runtime.Futexsleep(&tt.mtx, 0, tt.ns)
|
2014-11-11 15:08:33 -07:00
|
|
|
runtime.Exitsyscall(0)
|
2014-03-07 15:34:40 -07:00
|
|
|
tt.ch <- tt
|
2016-01-13 13:14:26 -07:00
|
|
|
wg.Done()
|
2014-03-07 15:34:40 -07:00
|
|
|
}(tt)
|
|
|
|
}
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case tt := <-futexsleepTests[beforeY2038].ch:
|
|
|
|
t.Errorf("futexsleep test %q finished early after %s", tt.msg, time.Since(start))
|
|
|
|
break loop
|
|
|
|
case tt := <-futexsleepTests[afterY2038].ch:
|
|
|
|
// Looks like FreeBSD 10 kernel has changed
|
|
|
|
// the semantics of timedwait on userspace
|
|
|
|
// mutex to make broken stuff look broken.
|
|
|
|
switch {
|
|
|
|
case runtime.GOOS == "freebsd" && runtime.GOARCH == "386":
|
|
|
|
t.Log("freebsd/386 may not work correctly after the year 2038, see golang.org/issue/7194")
|
|
|
|
default:
|
|
|
|
t.Errorf("futexsleep test %q finished early after %s", tt.msg, time.Since(start))
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
2016-01-13 13:14:26 -07:00
|
|
|
for i := range futexsleepTests {
|
|
|
|
tt := &futexsleepTests[i]
|
|
|
|
atomic.StoreUint32(&tt.mtx, 1)
|
2014-03-07 15:34:40 -07:00
|
|
|
runtime.Futexwakeup(&tt.mtx, 1)
|
2013-03-18 13:11:11 -06:00
|
|
|
}
|
2016-01-13 13:14:26 -07:00
|
|
|
wg.Wait()
|
2013-03-18 13:11:11 -06:00
|
|
|
}
|