re PR go/52358 (math FAILs on Solaris 8 and 9)
[gcc.git] / libgo / runtime / thread-linux.c
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.
4
5 #include "runtime.h"
6 #include "defs.h"
7
8 // Linux futex.
9 //
10 // futexsleep(uint32 *addr, uint32 val)
11 // futexwakeup(uint32 *addr)
12 //
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.
16
17 #include <errno.h>
18 #include <string.h>
19 #include <time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <syscall.h>
25 #include <linux/futex.h>
26
27 typedef struct timespec Timespec;
28
29 // Atomically,
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.
33 void
34 runtime_futexsleep(uint32 *addr, uint32 val, int64 ns)
35 {
36 Timespec ts, *tsp;
37
38 if(ns < 0)
39 tsp = nil;
40 else {
41 ts.tv_sec = ns/1000000000LL;
42 ts.tv_nsec = ns%1000000000LL;
43 // Avoid overflow
44 if(ts.tv_sec > 1<<30)
45 ts.tv_sec = 1<<30;
46 tsp = &ts;
47 }
48
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);
55 }
56
57 // If any procs are sleeping on addr, wake up at most cnt.
58 void
59 runtime_futexwakeup(uint32 *addr, uint32 cnt)
60 {
61 int64 ret;
62
63 ret = syscall(__NR_futex, addr, FUTEX_WAKE, cnt, nil, nil, 0);
64
65 if(ret >= 0)
66 return;
67
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 %lld\n", addr, (long long)ret);
72 *(int32*)0x1006 = 0x1006;
73 }
74
75 #ifndef O_CLOEXEC
76 #define O_CLOEXEC 0
77 #endif
78
79 static int32
80 getproccount(void)
81 {
82 int32 fd, rd, cnt, cpustrlen;
83 const char *cpustr;
84 const byte *pos;
85 byte *bufpos;
86 byte buf[256];
87
88 fd = open("/proc/stat", O_RDONLY|O_CLOEXEC, 0);
89 if(fd == -1)
90 return 1;
91 cnt = 0;
92 bufpos = buf;
93 cpustr = "\ncpu";
94 cpustrlen = strlen(cpustr);
95 for(;;) {
96 rd = read(fd, bufpos, sizeof(buf)-cpustrlen);
97 if(rd == -1)
98 break;
99 bufpos[rd] = 0;
100 for(pos=buf; (pos=(const byte*)strstr((const char*)pos, cpustr)) != nil; cnt++, pos++) {
101 }
102 if(rd < cpustrlen)
103 break;
104 memmove(buf, bufpos+rd-cpustrlen+1, cpustrlen-1);
105 bufpos = buf+cpustrlen-1;
106 }
107 close(fd);
108 return cnt ? cnt : 1;
109 }
110
111 void
112 runtime_osinit(void)
113 {
114 runtime_ncpu = getproccount();
115 }
116
117 void
118 runtime_goenvs(void)
119 {
120 runtime_goenvs_unix();
121 }