2011-08-08 07:56:38 -06:00
|
|
|
// Use of this source file is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.`
|
|
|
|
|
|
|
|
#include "runtime.h"
|
2011-12-16 13:33:58 -07:00
|
|
|
#include "defs_GOOS_GOARCH.h"
|
|
|
|
#include "os_GOOS.h"
|
2011-08-08 07:56:38 -06:00
|
|
|
#include "stack.h"
|
|
|
|
|
2011-08-29 07:35:13 -06:00
|
|
|
enum
|
|
|
|
{
|
2011-10-08 07:56:13 -06:00
|
|
|
ESRCH = 3,
|
2011-08-29 07:35:13 -06:00
|
|
|
ENOTSUP = 91,
|
2011-11-07 09:57:34 -07:00
|
|
|
|
|
|
|
// From OpenBSD's sys/time.h
|
|
|
|
CLOCK_REALTIME = 0,
|
|
|
|
CLOCK_VIRTUAL = 1,
|
|
|
|
CLOCK_PROF = 2,
|
|
|
|
CLOCK_MONOTONIC = 3
|
2011-08-29 07:35:13 -06:00
|
|
|
};
|
2011-08-08 07:56:38 -06:00
|
|
|
|
2011-10-08 07:56:13 -06:00
|
|
|
extern SigTab runtime·sigtab[];
|
|
|
|
|
2012-04-10 05:57:05 -06:00
|
|
|
static Sigset sigset_all = ~(Sigset)0;
|
|
|
|
static Sigset sigset_none;
|
|
|
|
|
2012-12-18 09:30:29 -07:00
|
|
|
extern int64 runtime·tfork(void *param, uintptr psize, M *mp, G *gp, void (*fn)(void));
|
2012-04-11 06:02:08 -06:00
|
|
|
extern int32 runtime·thrsleep(void *ident, int32 clock_id, void *tsp, void *lock, const int32 *abort);
|
2011-11-07 09:57:34 -07:00
|
|
|
extern int32 runtime·thrwakeup(void *ident, int32 n);
|
2011-10-08 07:56:13 -06:00
|
|
|
|
2011-10-05 11:16:43 -06:00
|
|
|
// From OpenBSD'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;
|
|
|
|
}
|
|
|
|
|
2011-11-02 07:42:01 -06:00
|
|
|
uintptr
|
|
|
|
runtime·semacreate(void)
|
2011-08-08 07:56:38 -06:00
|
|
|
{
|
2011-11-02 07:42:01 -06:00
|
|
|
return 1;
|
2011-08-08 07:56:38 -06:00
|
|
|
}
|
|
|
|
|
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
2011-11-09 13:17:05 -07:00
|
|
|
int32
|
|
|
|
runtime·semasleep(int64 ns)
|
2011-08-08 07:56:38 -06:00
|
|
|
{
|
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
2011-11-09 13:17:05 -07:00
|
|
|
Timespec ts;
|
|
|
|
|
2011-11-02 07:42:01 -06:00
|
|
|
// spin-mutex lock
|
|
|
|
while(runtime·xchg(&m->waitsemalock, 1))
|
2011-10-08 07:56:13 -06:00
|
|
|
runtime·osyield();
|
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
2011-11-09 13:17:05 -07:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
// lock held
|
|
|
|
if(m->waitsemacount == 0) {
|
|
|
|
// sleep until semaphore != 0 or timeout.
|
|
|
|
// thrsleep unlocks m->waitsemalock.
|
|
|
|
if(ns < 0)
|
2012-04-11 06:02:08 -06:00
|
|
|
runtime·thrsleep(&m->waitsemacount, 0, nil, &m->waitsemalock, nil);
|
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
2011-11-09 13:17:05 -07:00
|
|
|
else {
|
2011-11-10 12:42:01 -07:00
|
|
|
ns += runtime·nanotime();
|
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
2011-11-09 13:17:05 -07:00
|
|
|
ts.tv_sec = ns/1000000000LL;
|
|
|
|
ts.tv_nsec = ns%1000000000LL;
|
2012-04-11 06:02:08 -06:00
|
|
|
runtime·thrsleep(&m->waitsemacount, CLOCK_REALTIME, &ts, &m->waitsemalock, nil);
|
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
2011-11-09 13:17:05 -07:00
|
|
|
}
|
|
|
|
// reacquire lock
|
|
|
|
while(runtime·xchg(&m->waitsemalock, 1))
|
|
|
|
runtime·osyield();
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock held (again)
|
|
|
|
if(m->waitsemacount != 0) {
|
|
|
|
// semaphore is available.
|
|
|
|
m->waitsemacount--;
|
|
|
|
// spin-mutex unlock
|
|
|
|
runtime·atomicstore(&m->waitsemalock, 0);
|
|
|
|
return 0; // semaphore acquired
|
|
|
|
}
|
|
|
|
|
|
|
|
// semaphore not available.
|
|
|
|
// if there is a timeout, stop now.
|
|
|
|
// otherwise keep trying.
|
|
|
|
if(ns >= 0)
|
|
|
|
break;
|
2011-08-08 07:56:38 -06:00
|
|
|
}
|
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
2011-11-09 13:17:05 -07:00
|
|
|
|
|
|
|
// lock held but giving up
|
2011-11-02 07:42:01 -06:00
|
|
|
// spin-mutex unlock
|
|
|
|
runtime·atomicstore(&m->waitsemalock, 0);
|
runtime: add timer support, use for package time
This looks like it is just moving some code from
time to runtime (and translating it to C), but the
runtime can do a better job managing the goroutines,
and it needs this functionality for its own maintenance
(for example, for the garbage collector to hand back
unused memory to the OS on a time delay).
Might as well have just one copy of the timer logic,
and runtime can't depend on time, so vice versa.
It also unifies Sleep, NewTicker, and NewTimer behind
one mechanism, so that there are no claims that one
is more efficient than another. (For example, today
people recommend using time.After instead of time.Sleep
to avoid blocking an OS thread.)
Fixes #1644.
Fixes #1731.
Fixes #2190.
R=golang-dev, r, hectorchu, iant, iant, jsing, alex.brainman, dvyukov
CC=golang-dev
https://golang.org/cl/5334051
2011-11-09 13:17:05 -07:00
|
|
|
return -1;
|
2011-08-08 07:56:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-11-02 07:42:01 -06:00
|
|
|
runtime·semawakeup(M *mp)
|
2011-08-08 07:56:38 -06:00
|
|
|
{
|
2011-11-02 07:42:01 -06:00
|
|
|
uint32 ret;
|
2011-08-08 07:56:38 -06:00
|
|
|
|
2011-11-02 07:42:01 -06:00
|
|
|
// spin-mutex lock
|
|
|
|
while(runtime·xchg(&mp->waitsemalock, 1))
|
|
|
|
runtime·osyield();
|
|
|
|
mp->waitsemacount++;
|
|
|
|
ret = runtime·thrwakeup(&mp->waitsemacount, 1);
|
|
|
|
if(ret != 0 && ret != ESRCH)
|
|
|
|
runtime·printf("thrwakeup addr=%p sem=%d ret=%d\n", &mp->waitsemacount, mp->waitsemacount, ret);
|
|
|
|
// spin-mutex unlock
|
|
|
|
runtime·atomicstore(&mp->waitsemalock, 0);
|
2011-08-08 07:56:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-18 09:30:29 -07:00
|
|
|
runtime·newosproc(M *mp, G *gp, void *stk, void (*fn)(void))
|
2011-08-08 07:56:38 -06:00
|
|
|
{
|
2012-04-25 08:08:02 -06:00
|
|
|
Tfork param;
|
2012-04-10 05:57:05 -06:00
|
|
|
Sigset oset;
|
2011-08-08 07:56:38 -06:00
|
|
|
int32 ret;
|
|
|
|
|
2012-04-25 08:08:02 -06:00
|
|
|
if(0) {
|
2011-08-08 07:56:38 -06:00
|
|
|
runtime·printf(
|
|
|
|
"newosproc stk=%p m=%p g=%p fn=%p id=%d/%d ostk=%p\n",
|
2012-12-18 09:30:29 -07:00
|
|
|
stk, mp, gp, fn, mp->id, mp->tls[0], &mp);
|
2011-08-08 07:56:38 -06:00
|
|
|
}
|
|
|
|
|
2012-12-18 09:30:29 -07:00
|
|
|
mp->tls[0] = mp->id; // so 386 asm can find it
|
2011-08-08 07:56:38 -06:00
|
|
|
|
2012-12-18 09:30:29 -07:00
|
|
|
param.tf_tcb = (byte*)&mp->tls[0];
|
|
|
|
param.tf_tid = (int32*)&mp->procid;
|
2012-11-21 07:25:53 -07:00
|
|
|
param.tf_stack = stk;
|
2012-04-25 08:08:02 -06:00
|
|
|
|
2012-04-10 05:57:05 -06:00
|
|
|
oset = runtime·sigprocmask(SIG_SETMASK, sigset_all);
|
2012-12-18 09:30:29 -07:00
|
|
|
ret = runtime·tfork((byte*)¶m, sizeof(param), mp, gp, fn);
|
2012-04-10 05:57:05 -06:00
|
|
|
runtime·sigprocmask(SIG_SETMASK, oset);
|
|
|
|
|
|
|
|
if(ret < 0) {
|
2011-08-08 07:56:38 -06:00
|
|
|
runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount() - 1, -ret);
|
2011-08-29 07:35:13 -06:00
|
|
|
if (ret == -ENOTSUP)
|
|
|
|
runtime·printf("runtime: is kern.rthreads disabled?\n");
|
2011-08-08 07:56:38 -06:00
|
|
|
runtime·throw("runtime.newosproc");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime·osinit(void)
|
|
|
|
{
|
2011-10-05 11:16:43 -06:00
|
|
|
runtime·ncpu = getncpu();
|
2011-08-08 07:56:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime·goenvs(void)
|
|
|
|
{
|
|
|
|
runtime·goenvs_unix();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called to initialize a new m (including the bootstrap m).
|
|
|
|
void
|
|
|
|
runtime·minit(void)
|
|
|
|
{
|
|
|
|
// Initialize signal handling
|
|
|
|
m->gsignal = runtime·malg(32*1024);
|
2012-05-30 11:07:52 -06:00
|
|
|
runtime·signalstack((byte*)m->gsignal->stackguard - StackGuard, 32*1024);
|
2012-04-10 05:57:05 -06:00
|
|
|
runtime·sigprocmask(SIG_SETMASK, sigset_none);
|
2011-08-08 07:56:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime·sigpanic(void)
|
|
|
|
{
|
|
|
|
switch(g->sig) {
|
|
|
|
case SIGBUS:
|
2012-01-10 12:46:57 -07:00
|
|
|
if(g->sigcode0 == BUS_ADRERR && g->sigcode1 < 0x1000) {
|
|
|
|
if(g->sigpc == 0)
|
|
|
|
runtime·panicstring("call of nil func value");
|
2011-08-08 07:56:38 -06:00
|
|
|
runtime·panicstring("invalid memory address or nil pointer dereference");
|
2012-01-10 12:46:57 -07:00
|
|
|
}
|
2011-08-08 07:56:38 -06:00
|
|
|
runtime·printf("unexpected fault address %p\n", g->sigcode1);
|
|
|
|
runtime·throw("fault");
|
|
|
|
case SIGSEGV:
|
2012-01-10 12:46:57 -07:00
|
|
|
if((g->sigcode0 == 0 || g->sigcode0 == SEGV_MAPERR || g->sigcode0 == SEGV_ACCERR) && g->sigcode1 < 0x1000) {
|
|
|
|
if(g->sigpc == 0)
|
|
|
|
runtime·panicstring("call of nil func value");
|
2011-08-08 07:56:38 -06:00
|
|
|
runtime·panicstring("invalid memory address or nil pointer dereference");
|
2012-01-10 12:46:57 -07:00
|
|
|
}
|
2011-08-08 07:56:38 -06:00
|
|
|
runtime·printf("unexpected fault address %p\n", g->sigcode1);
|
|
|
|
runtime·throw("fault");
|
|
|
|
case SIGFPE:
|
|
|
|
switch(g->sigcode0) {
|
|
|
|
case FPE_INTDIV:
|
|
|
|
runtime·panicstring("integer divide by zero");
|
|
|
|
case FPE_INTOVF:
|
|
|
|
runtime·panicstring("integer overflow");
|
|
|
|
}
|
|
|
|
runtime·panicstring("floating point error");
|
|
|
|
}
|
|
|
|
runtime·panicstring(runtime·sigtab[g->sig].name);
|
|
|
|
}
|
2012-02-24 13:28:51 -07:00
|
|
|
|
|
|
|
uintptr
|
|
|
|
runtime·memlimit(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-28 14:18:24 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
runtime·setprof(bool on)
|
|
|
|
{
|
|
|
|
USED(on);
|
|
|
|
}
|
2012-03-08 10:12:40 -07:00
|
|
|
|
|
|
|
static int8 badcallback[] = "runtime: cgo callback on thread not created by Go.\n";
|
|
|
|
|
|
|
|
// This runs on a foreign stack, without an m or a g. No stack split.
|
|
|
|
#pragma textflag 7
|
|
|
|
void
|
|
|
|
runtime·badcallback(void)
|
|
|
|
{
|
|
|
|
runtime·write(2, badcallback, sizeof badcallback - 1);
|
|
|
|
}
|
2012-03-12 13:55:18 -06:00
|
|
|
|
|
|
|
static int8 badsignal[] = "runtime: signal received on thread not created by Go.\n";
|
|
|
|
|
|
|
|
// This runs on a foreign stack, without an m or a g. No stack split.
|
|
|
|
#pragma textflag 7
|
|
|
|
void
|
2012-09-04 12:40:49 -06:00
|
|
|
runtime·badsignal(int32 sig)
|
2012-03-12 13:55:18 -06:00
|
|
|
{
|
2012-09-04 12:40:49 -06:00
|
|
|
if (sig == SIGPROF) {
|
|
|
|
return; // Ignore SIGPROFs intended for a non-Go thread.
|
|
|
|
}
|
2012-03-12 13:55:18 -06:00
|
|
|
runtime·write(2, badsignal, sizeof badsignal - 1);
|
2012-09-04 14:35:05 -06:00
|
|
|
runtime·exit(1);
|
2012-03-12 13:55:18 -06:00
|
|
|
}
|