c08cc0dba2950b9ea16d1df4757d46d3bcff19fd
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.
10 #include <sys/types.h>
15 #include <linux/futex.h>
17 typedef struct timespec Timespec
;
20 // if(*addr == val) sleep
21 // Might be woken up spuriously; that's allowed.
22 // Don't sleep longer than ns; ns < 0 means forever.
24 runtime_futexsleep(uint32
*addr
, uint32 val
, int64 ns
)
31 ts
.tv_sec
= ns
/1000000000LL;
32 ts
.tv_nsec
= ns
%1000000000LL;
39 // Some Linux kernels have a bug where futex of
40 // FUTEX_WAIT returns an internal error code
41 // as an errno. Libpthread ignores the return value
42 // here, and so can we: as it says a few lines up,
43 // spurious wakeups are allowed.
44 syscall(__NR_futex
, addr
, FUTEX_WAIT
, val
, tsp
, nil
, 0);
47 // If any procs are sleeping on addr, wake up at most cnt.
49 runtime_futexwakeup(uint32
*addr
, uint32 cnt
)
53 ret
= syscall(__NR_futex
, addr
, FUTEX_WAKE
, cnt
, nil
, nil
, 0);
58 // I don't know that futex wakeup can return
59 // EAGAIN or EINTR, but if it does, it would be
60 // safe to loop and call futex again.
61 runtime_printf("futexwakeup addr=%p returned %lld\n", addr
, (long long)ret
);
62 *(int32
*)0x1006 = 0x1006;
72 int32 fd
, rd
, cnt
, cpustrlen
;
78 fd
= open("/proc/stat", O_RDONLY
|O_CLOEXEC
, 0);
84 cpustrlen
= strlen(cpustr
);
86 rd
= read(fd
, bufpos
, sizeof(buf
)-cpustrlen
);
90 for(pos
=buf
; (pos
=(const byte
*)strstr((const char*)pos
, cpustr
)) != nil
; cnt
++, pos
++) {
94 memmove(buf
, bufpos
+rd
-cpustrlen
+1, cpustrlen
-1);
95 bufpos
= buf
+cpustrlen
-1;
104 runtime_ncpu
= getproccount();