2013-03-19 11:40:29 -06:00
|
|
|
// Copyright 2011 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.
|
2011-12-12 16:10:11 -07:00
|
|
|
|
|
|
|
#include "runtime.h"
|
2011-12-16 13:33:58 -07:00
|
|
|
#include "defs_GOOS_GOARCH.h"
|
|
|
|
#include "os_GOOS.h"
|
2013-03-14 12:35:13 -06:00
|
|
|
#include "signal_unix.h"
|
2011-12-12 16:10:11 -07:00
|
|
|
#include "stack.h"
|
2014-09-04 21:05:18 -06:00
|
|
|
#include "textflag.h"
|
2011-12-12 16:10:11 -07:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ESRCH = 3,
|
|
|
|
ENOTSUP = 91,
|
|
|
|
|
2012-05-23 19:33:11 -06:00
|
|
|
// From NetBSD's <sys/time.h>
|
2011-12-12 16:10:11 -07:00
|
|
|
CLOCK_REALTIME = 0,
|
|
|
|
CLOCK_VIRTUAL = 1,
|
|
|
|
CLOCK_PROF = 2,
|
|
|
|
CLOCK_MONOTONIC = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
extern SigTab runtime·sigtab[];
|
|
|
|
|
2013-02-15 10:18:33 -07:00
|
|
|
static Sigset sigset_none;
|
2012-05-23 19:33:11 -06:00
|
|
|
static Sigset sigset_all = { ~(uint32)0, ~(uint32)0, ~(uint32)0, ~(uint32)0, };
|
|
|
|
|
|
|
|
extern void runtime·getcontext(UcontextT *context);
|
|
|
|
extern int32 runtime·lwp_create(UcontextT *context, uintptr flags, void *lwpid);
|
2012-12-18 09:30:29 -07:00
|
|
|
extern void runtime·lwp_mcontext_init(void *mc, void *stack, M *mp, G *gp, void (*fn)(void));
|
2012-05-23 19:33:11 -06:00
|
|
|
extern int32 runtime·lwp_park(Timespec *abstime, int32 unpark, void *hint, void *unparkhint);
|
|
|
|
extern int32 runtime·lwp_unpark(int32 lwp, void *hint);
|
2012-05-30 11:27:04 -06:00
|
|
|
extern int32 runtime·lwp_self(void);
|
2011-12-12 16:10:11 -07:00
|
|
|
|
|
|
|
// From NetBSD's <sys/sysctl.h>
|
|
|
|
#define CTL_HW 6
|
|
|
|
#define HW_NCPU 3
|
|
|
|
|
|
|
|
static int32
|
|
|
|
getncpu(void)
|
|
|
|
{
|
|
|
|
uint32 mib[2];
|
|
|
|
uint32 out;
|
|
|
|
int32 ret;
|
|
|
|
uintptr nout;
|
|
|
|
|
|
|
|
// Fetch hw.ncpu via sysctl.
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_NCPU;
|
|
|
|
nout = sizeof out;
|
|
|
|
out = 0;
|
|
|
|
ret = runtime·sysctl(mib, 2, (byte*)&out, &nout, nil, 0);
|
|
|
|
if(ret >= 0)
|
|
|
|
return out;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:12:31 -06:00
|
|
|
#pragma textflag NOSPLIT
|
2011-12-12 16:10:11 -07:00
|
|
|
uintptr
|
|
|
|
runtime·semacreate(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:12:31 -06:00
|
|
|
static void
|
|
|
|
semasleep(void)
|
2011-12-12 16:10:11 -07:00
|
|
|
{
|
2014-09-04 19:12:31 -06:00
|
|
|
int64 ns;
|
2011-12-12 16:10:11 -07:00
|
|
|
Timespec ts;
|
|
|
|
|
2014-09-04 19:12:31 -06:00
|
|
|
ns = (int64)(uint32)g->m->scalararg[0] | (int64)(uint32)g->m->scalararg[1]<<32;
|
|
|
|
g->m->scalararg[0] = 0;
|
|
|
|
g->m->scalararg[1] = 0;
|
|
|
|
|
2011-12-12 16:10:11 -07:00
|
|
|
// spin-mutex lock
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
while(runtime·xchg(&g->m->waitsemalock, 1))
|
2011-12-12 16:10:11 -07:00
|
|
|
runtime·osyield();
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
// lock held
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
if(g->m->waitsemacount == 0) {
|
2011-12-12 16:10:11 -07:00
|
|
|
// sleep until semaphore != 0 or timeout.
|
|
|
|
// thrsleep unlocks m->waitsemalock.
|
2012-05-23 19:33:11 -06:00
|
|
|
if(ns < 0) {
|
|
|
|
// TODO(jsing) - potential deadlock!
|
|
|
|
//
|
|
|
|
// There is a potential deadlock here since we
|
|
|
|
// have to release the waitsemalock mutex
|
|
|
|
// before we call lwp_park() to suspend the
|
|
|
|
// thread. This allows another thread to
|
|
|
|
// release the lock and call lwp_unpark()
|
|
|
|
// before the thread is actually suspended.
|
|
|
|
// If this occurs the current thread will end
|
|
|
|
// up sleeping indefinitely. Unfortunately
|
|
|
|
// the NetBSD kernel does not appear to provide
|
|
|
|
// a mechanism for unlocking the userspace
|
|
|
|
// mutex once the thread is actually parked.
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
runtime·atomicstore(&g->m->waitsemalock, 0);
|
|
|
|
runtime·lwp_park(nil, 0, &g->m->waitsemacount, nil);
|
2012-05-23 19:33:11 -06:00
|
|
|
} else {
|
2013-08-01 05:19:45 -06:00
|
|
|
ns = ns + runtime·nanotime();
|
2013-07-29 14:31:42 -06:00
|
|
|
// NOTE: tv_nsec is int64 on amd64, so this assumes a little-endian system.
|
2013-07-29 12:22:34 -06:00
|
|
|
ts.tv_nsec = 0;
|
2013-07-29 14:31:42 -06:00
|
|
|
ts.tv_sec = runtime·timediv(ns, 1000000000, (int32*)&ts.tv_nsec);
|
2012-05-23 19:33:11 -06:00
|
|
|
// TODO(jsing) - potential deadlock!
|
|
|
|
// See above for details.
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
runtime·atomicstore(&g->m->waitsemalock, 0);
|
|
|
|
runtime·lwp_park(&ts, 0, &g->m->waitsemacount, nil);
|
2011-12-12 16:10:11 -07:00
|
|
|
}
|
|
|
|
// reacquire lock
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
while(runtime·xchg(&g->m->waitsemalock, 1))
|
2011-12-12 16:10:11 -07:00
|
|
|
runtime·osyield();
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock held (again)
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
if(g->m->waitsemacount != 0) {
|
2011-12-12 16:10:11 -07:00
|
|
|
// semaphore is available.
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
g->m->waitsemacount--;
|
2011-12-12 16:10:11 -07:00
|
|
|
// spin-mutex unlock
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
runtime·atomicstore(&g->m->waitsemalock, 0);
|
2014-09-04 19:12:31 -06:00
|
|
|
g->m->scalararg[0] = 0; // semaphore acquired
|
|
|
|
return;
|
2011-12-12 16:10:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// semaphore not available.
|
|
|
|
// if there is a timeout, stop now.
|
|
|
|
// otherwise keep trying.
|
|
|
|
if(ns >= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock held but giving up
|
|
|
|
// spin-mutex unlock
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
runtime·atomicstore(&g->m->waitsemalock, 0);
|
2014-09-04 19:12:31 -06:00
|
|
|
g->m->scalararg[0] = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma textflag NOSPLIT
|
|
|
|
int32
|
|
|
|
runtime·semasleep(int64 ns)
|
|
|
|
{
|
|
|
|
int32 r;
|
|
|
|
void (*fn)(void);
|
|
|
|
|
|
|
|
g->m->scalararg[0] = (uint32)ns;
|
|
|
|
g->m->scalararg[1] = (uint32)(ns>>32);
|
|
|
|
fn = semasleep;
|
|
|
|
runtime·onM(&fn);
|
|
|
|
r = g->m->scalararg[0];
|
|
|
|
g->m->scalararg[0] = 0;
|
|
|
|
return r;
|
2011-12-12 16:10:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-04 19:12:31 -06:00
|
|
|
static void badsemawakeup(void);
|
|
|
|
|
|
|
|
#pragma textflag NOSPLIT
|
2011-12-12 16:10:11 -07:00
|
|
|
void
|
|
|
|
runtime·semawakeup(M *mp)
|
|
|
|
{
|
|
|
|
uint32 ret;
|
2014-09-04 19:12:31 -06:00
|
|
|
void (*fn)(void);
|
|
|
|
void *oldptr;
|
|
|
|
uintptr oldscalar;
|
2011-12-12 16:10:11 -07:00
|
|
|
|
|
|
|
// spin-mutex lock
|
|
|
|
while(runtime·xchg(&mp->waitsemalock, 1))
|
|
|
|
runtime·osyield();
|
|
|
|
mp->waitsemacount++;
|
2012-05-23 19:33:11 -06:00
|
|
|
// TODO(jsing) - potential deadlock, see semasleep() for details.
|
|
|
|
// Confirm that LWP is parked before unparking...
|
|
|
|
ret = runtime·lwp_unpark(mp->procid, &mp->waitsemacount);
|
2014-09-04 19:12:31 -06:00
|
|
|
if(ret != 0 && ret != ESRCH) {
|
|
|
|
// semawakeup can be called on signal stack.
|
|
|
|
// Save old ptrarg/scalararg so we can restore them.
|
|
|
|
oldptr = g->m->ptrarg[0];
|
|
|
|
oldscalar = g->m->scalararg[0];
|
|
|
|
g->m->ptrarg[0] = mp;
|
|
|
|
g->m->scalararg[0] = ret;
|
|
|
|
fn = badsemawakeup;
|
|
|
|
if(g == g->m->gsignal)
|
|
|
|
fn();
|
|
|
|
else
|
|
|
|
runtime·onM(&fn);
|
|
|
|
g->m->ptrarg[0] = oldptr;
|
|
|
|
g->m->scalararg[0] = oldscalar;
|
|
|
|
}
|
2011-12-12 16:10:11 -07:00
|
|
|
// spin-mutex unlock
|
|
|
|
runtime·atomicstore(&mp->waitsemalock, 0);
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:12:31 -06:00
|
|
|
static void
|
|
|
|
badsemawakeup(void)
|
|
|
|
{
|
|
|
|
M *mp;
|
|
|
|
int32 ret;
|
|
|
|
|
|
|
|
mp = g->m->ptrarg[0];
|
|
|
|
g->m->ptrarg[0] = nil;
|
|
|
|
ret = g->m->scalararg[0];
|
|
|
|
g->m->scalararg[0] = 0;
|
|
|
|
|
|
|
|
runtime·printf("thrwakeup addr=%p sem=%d ret=%d\n", &mp->waitsemacount, mp->waitsemacount, ret);
|
|
|
|
}
|
|
|
|
|
2011-12-12 16:10:11 -07:00
|
|
|
void
|
2013-03-01 09:44:43 -07:00
|
|
|
runtime·newosproc(M *mp, void *stk)
|
2011-12-12 16:10:11 -07:00
|
|
|
{
|
2012-05-23 19:33:11 -06:00
|
|
|
UcontextT uc;
|
2011-12-12 16:10:11 -07:00
|
|
|
int32 ret;
|
|
|
|
|
2012-05-23 19:33:11 -06:00
|
|
|
if(0) {
|
2011-12-12 16:10:11 -07:00
|
|
|
runtime·printf(
|
2013-03-01 09:44:43 -07:00
|
|
|
"newosproc stk=%p m=%p g=%p id=%d/%d ostk=%p\n",
|
|
|
|
stk, mp, mp->g0, mp->id, (int32)mp->tls[0], &mp);
|
2011-12-12 16:10:11 -07:00
|
|
|
}
|
|
|
|
|
2012-12-18 09:30:29 -07:00
|
|
|
mp->tls[0] = mp->id; // so 386 asm can find it
|
2011-12-12 16:10:11 -07:00
|
|
|
|
2012-05-23 19:33:11 -06:00
|
|
|
runtime·getcontext(&uc);
|
|
|
|
|
|
|
|
uc.uc_flags = _UC_SIGMASK | _UC_CPU;
|
|
|
|
uc.uc_link = nil;
|
|
|
|
uc.uc_sigmask = sigset_all;
|
|
|
|
|
2013-03-01 09:44:43 -07:00
|
|
|
runtime·lwp_mcontext_init(&uc.uc_mcontext, stk, mp, mp->g0, runtime·mstart);
|
2012-05-23 19:33:11 -06:00
|
|
|
|
2012-12-18 09:30:29 -07:00
|
|
|
ret = runtime·lwp_create(&uc, 0, &mp->procid);
|
2012-05-23 19:33:11 -06:00
|
|
|
|
|
|
|
if(ret < 0) {
|
2011-12-12 16:10:11 -07:00
|
|
|
runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount() - 1, -ret);
|
|
|
|
runtime·throw("runtime.newosproc");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime·osinit(void)
|
|
|
|
{
|
|
|
|
runtime·ncpu = getncpu();
|
|
|
|
}
|
|
|
|
|
2014-09-04 13:53:45 -06:00
|
|
|
#pragma textflag NOSPLIT
|
2013-03-12 11:47:44 -06:00
|
|
|
void
|
|
|
|
runtime·get_random_data(byte **rnd, int32 *rnd_len)
|
|
|
|
{
|
2014-05-31 17:21:17 -06:00
|
|
|
#pragma dataflag NOPTR
|
2013-03-12 11:47:44 -06:00
|
|
|
static byte urandom_data[HashRandomBytes];
|
|
|
|
int32 fd;
|
|
|
|
fd = runtime·open("/dev/urandom", 0 /* O_RDONLY */, 0);
|
|
|
|
if(runtime·read(fd, urandom_data, HashRandomBytes) == HashRandomBytes) {
|
|
|
|
*rnd = urandom_data;
|
|
|
|
*rnd_len = HashRandomBytes;
|
|
|
|
} else {
|
|
|
|
*rnd = nil;
|
|
|
|
*rnd_len = 0;
|
|
|
|
}
|
|
|
|
runtime·close(fd);
|
|
|
|
}
|
|
|
|
|
2011-12-12 16:10:11 -07:00
|
|
|
void
|
|
|
|
runtime·goenvs(void)
|
|
|
|
{
|
|
|
|
runtime·goenvs_unix();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called to initialize a new m (including the bootstrap m).
|
2013-02-21 05:24:38 -07:00
|
|
|
// Called on the parent thread (main thread in case of bootstrap), can allocate memory.
|
|
|
|
void
|
|
|
|
runtime·mpreinit(M *mp)
|
|
|
|
{
|
|
|
|
mp->gsignal = runtime·malg(32*1024);
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
mp->gsignal->m = mp;
|
2013-02-21 05:24:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called to initialize a new m (including the bootstrap m).
|
|
|
|
// Called on the new thread, can not allocate memory.
|
2011-12-12 16:10:11 -07:00
|
|
|
void
|
|
|
|
runtime·minit(void)
|
|
|
|
{
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
g->m->procid = runtime·lwp_self();
|
2012-05-30 11:27:04 -06:00
|
|
|
|
2011-12-12 16:10:11 -07:00
|
|
|
// Initialize signal handling
|
all: remove 'extern register M *m' from runtime
The runtime has historically held two dedicated values g (current goroutine)
and m (current thread) in 'extern register' slots (TLS on x86, real registers
backed by TLS on ARM).
This CL removes the extern register m; code now uses g->m.
On ARM, this frees up the register that formerly held m (R9).
This is important for NaCl, because NaCl ARM code cannot use R9 at all.
The Go 1 macrobenchmarks (those with per-op times >= 10 µs) are unaffected:
BenchmarkBinaryTree17 5491374955 5471024381 -0.37%
BenchmarkFannkuch11 4357101311 4275174828 -1.88%
BenchmarkGobDecode 11029957 11364184 +3.03%
BenchmarkGobEncode 6852205 6784822 -0.98%
BenchmarkGzip 650795967 650152275 -0.10%
BenchmarkGunzip 140962363 141041670 +0.06%
BenchmarkHTTPClientServer 71581 73081 +2.10%
BenchmarkJSONEncode 31928079 31913356 -0.05%
BenchmarkJSONDecode 117470065 113689916 -3.22%
BenchmarkMandelbrot200 6008923 5998712 -0.17%
BenchmarkGoParse 6310917 6327487 +0.26%
BenchmarkRegexpMatchMedium_1K 114568 114763 +0.17%
BenchmarkRegexpMatchHard_1K 168977 169244 +0.16%
BenchmarkRevcomp 935294971 914060918 -2.27%
BenchmarkTemplate 145917123 148186096 +1.55%
Minux previous reported larger variations, but these were caused by
run-to-run noise, not repeatable slowdowns.
Actual code changes by Minux.
I only did the docs and the benchmarking.
LGTM=dvyukov, iant, minux
R=minux, josharian, iant, dave, bradfitz, dvyukov
CC=golang-codereviews
https://golang.org/cl/109050043
2014-06-26 09:54:39 -06:00
|
|
|
runtime·signalstack((byte*)g->m->gsignal->stackguard - StackGuard, 32*1024);
|
2013-02-15 10:18:33 -07:00
|
|
|
runtime·sigprocmask(SIG_SETMASK, &sigset_none, nil);
|
2011-12-12 16:10:11 -07:00
|
|
|
}
|
|
|
|
|
2013-02-20 15:48:23 -07:00
|
|
|
// Called from dropm to undo the effect of an minit.
|
|
|
|
void
|
|
|
|
runtime·unminit(void)
|
|
|
|
{
|
|
|
|
runtime·signalstack(nil, 0);
|
|
|
|
}
|
|
|
|
|
2012-02-24 13:28:51 -07:00
|
|
|
uintptr
|
|
|
|
runtime·memlimit(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-28 14:18:24 -07:00
|
|
|
|
2013-03-14 12:35:13 -06:00
|
|
|
extern void runtime·sigtramp(void);
|
|
|
|
|
|
|
|
typedef struct sigaction {
|
|
|
|
union {
|
|
|
|
void (*_sa_handler)(int32);
|
|
|
|
void (*_sa_sigaction)(int32, Siginfo*, void *);
|
|
|
|
} _sa_u; /* signal handler */
|
|
|
|
uint32 sa_mask[4]; /* signal mask to apply */
|
|
|
|
int32 sa_flags; /* see signal options below */
|
2014-08-29 14:00:31 -06:00
|
|
|
} SigactionT;
|
2013-03-14 12:35:13 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
runtime·setsig(int32 i, GoSighandler *fn, bool restart)
|
|
|
|
{
|
2014-08-29 14:00:31 -06:00
|
|
|
SigactionT sa;
|
2013-03-14 12:35:13 -06:00
|
|
|
|
|
|
|
runtime·memclr((byte*)&sa, sizeof sa);
|
|
|
|
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
|
|
|
|
if(restart)
|
|
|
|
sa.sa_flags |= SA_RESTART;
|
|
|
|
sa.sa_mask[0] = ~0U;
|
|
|
|
sa.sa_mask[1] = ~0U;
|
|
|
|
sa.sa_mask[2] = ~0U;
|
|
|
|
sa.sa_mask[3] = ~0U;
|
|
|
|
if (fn == runtime·sighandler)
|
|
|
|
fn = (void*)runtime·sigtramp;
|
|
|
|
sa._sa_u._sa_sigaction = (void*)fn;
|
|
|
|
runtime·sigaction(i, &sa, nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
GoSighandler*
|
|
|
|
runtime·getsig(int32 i)
|
|
|
|
{
|
2014-08-29 14:00:31 -06:00
|
|
|
SigactionT sa;
|
2013-03-14 12:35:13 -06:00
|
|
|
|
|
|
|
runtime·memclr((byte*)&sa, sizeof sa);
|
|
|
|
runtime·sigaction(i, nil, &sa);
|
|
|
|
if((void*)sa._sa_u._sa_sigaction == runtime·sigtramp)
|
|
|
|
return runtime·sighandler;
|
|
|
|
return (void*)sa._sa_u._sa_sigaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime·signalstack(byte *p, int32 n)
|
|
|
|
{
|
|
|
|
StackT st;
|
|
|
|
|
|
|
|
st.ss_sp = (void*)p;
|
|
|
|
st.ss_size = n;
|
|
|
|
st.ss_flags = 0;
|
|
|
|
if(p == nil)
|
|
|
|
st.ss_flags = SS_DISABLE;
|
|
|
|
runtime·sigaltstack(&st, nil);
|
|
|
|
}
|
2013-12-19 18:45:05 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
runtime·unblocksignals(void)
|
|
|
|
{
|
|
|
|
runtime·sigprocmask(SIG_SETMASK, &sigset_none, nil);
|
|
|
|
}
|
liblink, runtime: diagnose and fix C code running on Go stack
This CL contains compiler+runtime changes that detect C code
running on Go (not g0, not gsignal) stacks, and it contains
corrections for what it detected.
The detection works by changing the C prologue to use a different
stack guard word in the G than Go prologue does. On the g0 and
gsignal stacks, that stack guard word is set to the usual
stack guard value. But on ordinary Go stacks, that stack
guard word is set to ^0, which will make any stack split
check fail. The C prologue then calls morestackc instead
of morestack, and morestackc aborts the program with
a message about running C code on a Go stack.
This check catches all C code running on the Go stack
except NOSPLIT code. The NOSPLIT code is allowed,
so the check is complete. Since it is a dynamic check,
the code must execute to be caught. But unlike the static
checks we've been using in cmd/ld, the dynamic check
works with function pointers and other indirect calls.
For example it caught sigpanic being pushed onto Go
stacks in the signal handlers.
Fixes #8667.
LGTM=khr, iant
R=golang-codereviews, khr, iant
CC=golang-codereviews, r
https://golang.org/cl/133700043
2014-09-08 12:05:23 -06:00
|
|
|
|
|
|
|
#pragma textflag NOSPLIT
|
|
|
|
int8*
|
|
|
|
runtime·signame(int32 sig)
|
|
|
|
{
|
|
|
|
return runtime·sigtab[sig].name;
|
|
|
|
}
|