summaryrefslogtreecommitdiff
path: root/libgo/runtime
diff options
context:
space:
mode:
authorian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>2012-08-07 04:42:49 +0000
committerian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>2012-08-07 04:42:49 +0000
commite04e2524c2b8b288529c2cbcf217c25035be0236 (patch)
tree1bca221ed18cd8f917eb75d5ad3cf307a2cb51e1 /libgo/runtime
parentf8c23635dafc9c0c41c9f6abea40809c6542ddb3 (diff)
downloadgcc-e04e2524c2b8b288529c2cbcf217c25035be0236.tar.gz
runtime: support NumCPU() on more platforms
Added support for Solaris, Irix, *BSD (including Darwin). Still missing support for RTEMS. Fixes issue 3698 in Go issue tracker. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@190197 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/runtime')
-rw-r--r--libgo/runtime/getncpu-bsd.c24
-rw-r--r--libgo/runtime/getncpu-irix.c16
-rw-r--r--libgo/runtime/getncpu-linux.c47
-rw-r--r--libgo/runtime/getncpu-none.c12
-rw-r--r--libgo/runtime/getncpu-solaris.c16
-rw-r--r--libgo/runtime/runtime.h2
-rw-r--r--libgo/runtime/thread-linux.c36
-rw-r--r--libgo/runtime/thread-sema.c1
8 files changed, 118 insertions, 36 deletions
diff --git a/libgo/runtime/getncpu-bsd.c b/libgo/runtime/getncpu-bsd.c
new file mode 100644
index 00000000000..00a81d1ddae
--- /dev/null
+++ b/libgo/runtime/getncpu-bsd.c
@@ -0,0 +1,24 @@
+// Copyright 2012 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.
+
+#include <sys/types.h>
+#include <sys/sysctl.h>
+
+#include "runtime.h"
+#include "defs.h"
+
+int32
+getproccount(void)
+{
+ int mib[2], out;
+ size_t len;
+
+ mib[0] = CTL_HW;
+ mib[1] = HW_NCPU;
+ len = sizeof(out);
+ if(sysctl(mib, 2, &out, &len, NULL, 0) >= 0)
+ return (int32)out;
+ else
+ return 0;
+}
diff --git a/libgo/runtime/getncpu-irix.c b/libgo/runtime/getncpu-irix.c
new file mode 100644
index 00000000000..a65ca63d2ae
--- /dev/null
+++ b/libgo/runtime/getncpu-irix.c
@@ -0,0 +1,16 @@
+// Copyright 2012 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.
+
+#include <unistd.h>
+
+#include "runtime.h"
+#include "defs.h"
+
+int32
+getproccount(void)
+{
+ int32 n;
+ n = (int32)sysconf(_SC_NPROC_ONLN);
+ return n > 1 ? n : 1;
+}
diff --git a/libgo/runtime/getncpu-linux.c b/libgo/runtime/getncpu-linux.c
new file mode 100644
index 00000000000..05bd4a37e52
--- /dev/null
+++ b/libgo/runtime/getncpu-linux.c
@@ -0,0 +1,47 @@
+// Copyright 2012 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.
+
+#include <string.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "runtime.h"
+#include "defs.h"
+
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
+int32
+getproccount(void)
+{
+ int32 fd, rd, cnt, cpustrlen;
+ const char *cpustr;
+ const byte *pos;
+ byte *bufpos;
+ byte buf[256];
+
+ fd = open("/proc/stat", O_RDONLY|O_CLOEXEC, 0);
+ if(fd == -1)
+ return 1;
+ cnt = 0;
+ bufpos = buf;
+ cpustr = "\ncpu";
+ cpustrlen = strlen(cpustr);
+ for(;;) {
+ rd = read(fd, bufpos, sizeof(buf)-cpustrlen);
+ if(rd == -1)
+ break;
+ bufpos[rd] = 0;
+ for(pos=buf; (pos=(const byte*)strstr((const char*)pos, cpustr)) != nil; cnt++, pos++) {
+ }
+ if(rd < cpustrlen)
+ break;
+ memmove(buf, bufpos+rd-cpustrlen+1, cpustrlen-1);
+ bufpos = buf+cpustrlen-1;
+ }
+ close(fd);
+ return cnt ? cnt : 1;
+}
diff --git a/libgo/runtime/getncpu-none.c b/libgo/runtime/getncpu-none.c
new file mode 100644
index 00000000000..ba6fd4e689d
--- /dev/null
+++ b/libgo/runtime/getncpu-none.c
@@ -0,0 +1,12 @@
+// Copyright 2012 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.
+
+#include "runtime.h"
+#include "defs.h"
+
+int32
+getproccount(void)
+{
+ return 0;
+}
diff --git a/libgo/runtime/getncpu-solaris.c b/libgo/runtime/getncpu-solaris.c
new file mode 100644
index 00000000000..5d5d7025dfe
--- /dev/null
+++ b/libgo/runtime/getncpu-solaris.c
@@ -0,0 +1,16 @@
+// Copyright 2012 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.
+
+#include <unistd.h>
+
+#include "runtime.h"
+#include "defs.h"
+
+int32
+getproccount(void)
+{
+ int32 n;
+ n = (int32)sysconf(_SC_NPROCESSORS_ONLN);
+ return n > 1 ? n : 1;
+}
diff --git a/libgo/runtime/runtime.h b/libgo/runtime/runtime.h
index dc4fc0817f5..cebc1fd9801 100644
--- a/libgo/runtime/runtime.h
+++ b/libgo/runtime/runtime.h
@@ -518,3 +518,5 @@ void __go_register_gc_roots(struct root_list*);
extern uintptr runtime_stacks_sys;
extern _Bool __go_file_line (uintptr, String*, String*, int *);
+
+int32 getproccount(void);
diff --git a/libgo/runtime/thread-linux.c b/libgo/runtime/thread-linux.c
index 0014068b2cd..13d23c47b07 100644
--- a/libgo/runtime/thread-linux.c
+++ b/libgo/runtime/thread-linux.c
@@ -72,42 +72,6 @@ runtime_futexwakeup(uint32 *addr, uint32 cnt)
*(int32*)0x1006 = 0x1006;
}
-#ifndef O_CLOEXEC
-#define O_CLOEXEC 0
-#endif
-
-static int32
-getproccount(void)
-{
- int32 fd, rd, cnt, cpustrlen;
- const char *cpustr;
- const byte *pos;
- byte *bufpos;
- byte buf[256];
-
- fd = open("/proc/stat", O_RDONLY|O_CLOEXEC, 0);
- if(fd == -1)
- return 1;
- cnt = 0;
- bufpos = buf;
- cpustr = "\ncpu";
- cpustrlen = strlen(cpustr);
- for(;;) {
- rd = read(fd, bufpos, sizeof(buf)-cpustrlen);
- if(rd == -1)
- break;
- bufpos[rd] = 0;
- for(pos=buf; (pos=(const byte*)strstr((const char*)pos, cpustr)) != nil; cnt++, pos++) {
- }
- if(rd < cpustrlen)
- break;
- memmove(buf, bufpos+rd-cpustrlen+1, cpustrlen-1);
- bufpos = buf+cpustrlen-1;
- }
- close(fd);
- return cnt ? cnt : 1;
-}
-
void
runtime_osinit(void)
{
diff --git a/libgo/runtime/thread-sema.c b/libgo/runtime/thread-sema.c
index 7d0acfb1ce1..18827b025d7 100644
--- a/libgo/runtime/thread-sema.c
+++ b/libgo/runtime/thread-sema.c
@@ -138,6 +138,7 @@ runtime_semawakeup (M *mp)
void
runtime_osinit (void)
{
+ runtime_ncpu = getproccount();
}
void