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 // futexsleep(uint32 *addr, uint32 val)
11 // futexwakeup(uint32 *addr)
13 // Futexsleep atomically checks if *addr == val and if so, sleeps on addr.
14 // Futexwakeup wakes up threads sleeping on addr.
15 // Futexsleep is allowed to wake up spuriously.
20 #include <sys/types.h>
25 #include <linux/futex.h>
27 typedef struct timespec Timespec;
30 // if(*addr == val) sleep
31 // Might be woken up spuriously; that's allowed.
32 // Don't sleep longer than ns; ns < 0 means forever.
34 runtime_futexsleep(uint32 *addr, uint32 val, int64 ns)
41 ts.tv_sec = ns/1000000000LL;
42 ts.tv_nsec = ns%1000000000LL;
49 // Some Linux kernels have a bug where futex of
50 // FUTEX_WAIT returns an internal error code
51 // as an errno. Libpthread ignores the return value
52 // here, and so can we: as it says a few lines up,
53 // spurious wakeups are allowed.
54 syscall(__NR_futex, addr, FUTEX_WAIT, val, tsp, nil, 0);
57 // If any procs are sleeping on addr, wake up at most cnt.
59 runtime_futexwakeup(uint32 *addr, uint32 cnt)
63 ret = syscall(__NR_futex, addr, FUTEX_WAKE, cnt, nil, nil, 0);
68 // I don't know that futex wakeup can return
69 // EAGAIN or EINTR, but if it does, it would be
70 // safe to loop and call futex again.
71 runtime_printf("futexwakeup addr=%p returned %D\n", addr, ret);
72 *(int32*)0x1006 = 0x1006;
82 int32 fd, rd, cnt, cpustrlen;
88 fd = open("/proc/stat", O_RDONLY|O_CLOEXEC, 0);
94 cpustrlen = strlen(cpustr);
96 rd = read(fd, bufpos, sizeof(buf)-cpustrlen);
100 for(pos=buf; (pos=(const byte*)strstr((const char*)pos, cpustr)) != nil; cnt++, pos++) {
104 memmove(buf, bufpos+rd-cpustrlen+1, cpustrlen-1);
105 bufpos = buf+cpustrlen-1;
108 return cnt ? cnt : 1;
114 runtime_ncpu = getproccount();
120 runtime_goenvs_unix();