1 // Copyright 2009 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.
7 #include "signal_unix.h"
11 // futexsleep(uint32 *addr, uint32 val)
12 // futexwakeup(uint32 *addr)
14 // Futexsleep atomically checks if *addr == val and if so, sleeps on addr.
15 // Futexwakeup wakes up threads sleeping on addr.
16 // Futexsleep is allowed to wake up spuriously.
21 #include <sys/types.h>
26 #include <linux/futex.h>
28 typedef struct timespec Timespec
;
31 // if(*addr == val) sleep
32 // Might be woken up spuriously; that's allowed.
33 // Don't sleep longer than ns; ns < 0 means forever.
35 runtime_futexsleep(uint32
*addr
, uint32 val
, int64 ns
)
40 // Some Linux kernels have a bug where futex of
41 // FUTEX_WAIT returns an internal error code
42 // as an errno. Libpthread ignores the return value
43 // here, and so can we: as it says a few lines up,
44 // spurious wakeups are allowed.
47 syscall(__NR_futex
, addr
, FUTEX_WAIT
, val
, nil
, nil
, 0);
50 ts
.tv_sec
= runtime_timediv(ns
, 1000000000LL, &nsec
);
52 syscall(__NR_futex
, addr
, FUTEX_WAIT
, val
, &ts
, nil
, 0);
55 // If any procs are sleeping on addr, wake up at most cnt.
57 runtime_futexwakeup(uint32
*addr
, uint32 cnt
)
61 ret
= syscall(__NR_futex
, addr
, FUTEX_WAKE
, cnt
, nil
, nil
, 0);
66 // I don't know that futex wakeup can return
67 // EAGAIN or EINTR, but if it does, it would be
68 // safe to loop and call futex again.
69 runtime_printf("futexwakeup addr=%p returned %D\n", addr
, ret
);
70 *(int32
*)0x1006 = 0x1006;
76 runtime_ncpu
= getproccount();
82 runtime_goenvs_unix();