2009-12-15 19:21:29 -07:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
// This file implements runtime support for signal handling.
|
|
|
|
//
|
|
|
|
// Most synchronization primitives are not available from
|
2012-12-28 04:36:06 -07:00
|
|
|
// the signal handler (it cannot block, allocate memory, or use locks)
|
2009-12-15 19:21:29 -07:00
|
|
|
// so the handler communicates with a processing goroutine
|
|
|
|
// via struct sig, below.
|
|
|
|
//
|
2012-12-28 04:36:06 -07:00
|
|
|
// sigsend() is called by the signal handler to queue a new signal.
|
|
|
|
// signal_recv() is called by the Go program to receive a newly queued signal.
|
|
|
|
// Synchronization between sigsend() and signal_recv() is based on the sig.state
|
|
|
|
// variable. It can be in 3 states: 0, HASWAITER and HASSIGNAL.
|
|
|
|
// HASWAITER means that signal_recv() is blocked on sig.Note and there are no
|
|
|
|
// new pending signals.
|
|
|
|
// HASSIGNAL means that sig.mask *may* contain new pending signals,
|
|
|
|
// signal_recv() can't be blocked in this state.
|
|
|
|
// 0 means that there are no new pending signals and signal_recv() is not blocked.
|
|
|
|
// Transitions between states are done atomically with CAS.
|
|
|
|
// When signal_recv() is unblocked, it resets sig.Note and rechecks sig.mask.
|
|
|
|
// If several sigsend()'s and signal_recv() execute concurrently, it can lead to
|
|
|
|
// unnecessary rechecks of sig.mask, but must not lead to missed signals
|
|
|
|
// nor deadlocks.
|
2009-12-15 19:21:29 -07:00
|
|
|
|
|
|
|
package runtime
|
|
|
|
#include "runtime.h"
|
2011-12-16 13:33:58 -07:00
|
|
|
#include "defs_GOOS_GOARCH.h"
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
#include "os_GOOS.h"
|
2013-07-11 14:39:39 -06:00
|
|
|
#include "cgocall.h"
|
2009-12-15 19:21:29 -07:00
|
|
|
|
|
|
|
static struct {
|
|
|
|
Note;
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
uint32 mask[(NSIG+31)/32];
|
|
|
|
uint32 wanted[(NSIG+31)/32];
|
2012-12-28 04:36:06 -07:00
|
|
|
uint32 state;
|
2009-12-16 21:20:50 -07:00
|
|
|
bool inuse;
|
2009-12-15 19:21:29 -07:00
|
|
|
} sig;
|
|
|
|
|
2012-12-28 04:36:06 -07:00
|
|
|
enum {
|
|
|
|
HASWAITER = 1,
|
|
|
|
HASSIGNAL = 2,
|
|
|
|
};
|
|
|
|
|
2009-12-15 19:21:29 -07:00
|
|
|
// Called from sighandler to send a signal back out of the signal handling thread.
|
2009-12-16 21:20:50 -07:00
|
|
|
bool
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·sigsend(int32 s)
|
2009-12-15 19:21:29 -07:00
|
|
|
{
|
2012-12-28 04:36:06 -07:00
|
|
|
uint32 bit, mask, old, new;
|
2009-12-15 19:21:29 -07:00
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
if(!sig.inuse || s < 0 || s >= 32*nelem(sig.wanted) || !(sig.wanted[s/32]&(1U<<(s&31))))
|
2009-12-16 21:20:50 -07:00
|
|
|
return false;
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
bit = 1 << (s&31);
|
2009-12-15 19:21:29 -07:00
|
|
|
for(;;) {
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
mask = sig.mask[s/32];
|
2009-12-15 19:21:29 -07:00
|
|
|
if(mask & bit)
|
2009-12-16 21:20:50 -07:00
|
|
|
break; // signal already in queue
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
if(runtime·cas(&sig.mask[s/32], mask, mask|bit)) {
|
2009-12-15 19:21:29 -07:00
|
|
|
// Added to queue.
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
// Only send a wakeup if the receiver needs a kick.
|
2012-12-28 04:36:06 -07:00
|
|
|
for(;;) {
|
|
|
|
old = runtime·atomicload(&sig.state);
|
|
|
|
if(old == HASSIGNAL)
|
|
|
|
break;
|
|
|
|
if(old == HASWAITER)
|
|
|
|
new = 0;
|
|
|
|
else // if(old == 0)
|
|
|
|
new = HASSIGNAL;
|
|
|
|
if(runtime·cas(&sig.state, old, new)) {
|
|
|
|
if (old == HASWAITER)
|
|
|
|
runtime·notewakeup(&sig);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-12-16 21:20:50 -07:00
|
|
|
break;
|
2009-12-15 19:21:29 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-16 21:20:50 -07:00
|
|
|
return true;
|
2009-12-15 19:21:29 -07:00
|
|
|
}
|
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
// Called to receive the next queued signal.
|
|
|
|
// Must only be called from a single goroutine at a time.
|
|
|
|
func signal_recv() (m uint32) {
|
|
|
|
static uint32 recv[nelem(sig.mask)];
|
2012-12-28 04:36:06 -07:00
|
|
|
uint32 i, old, new;
|
2013-03-05 13:36:40 -07:00
|
|
|
|
2009-12-15 19:21:29 -07:00
|
|
|
for(;;) {
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
// Serve from local copy if there are bits left.
|
|
|
|
for(i=0; i<NSIG; i++) {
|
|
|
|
if(recv[i/32]&(1U<<(i&31))) {
|
|
|
|
recv[i/32] ^= 1U<<(i&31);
|
|
|
|
m = i;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-28 04:36:06 -07:00
|
|
|
// Check and update sig.state.
|
|
|
|
for(;;) {
|
|
|
|
old = runtime·atomicload(&sig.state);
|
|
|
|
if(old == HASWAITER)
|
|
|
|
runtime·throw("inconsistent state in signal_recv");
|
|
|
|
if(old == HASSIGNAL)
|
|
|
|
new = 0;
|
|
|
|
else // if(old == 0)
|
|
|
|
new = HASWAITER;
|
|
|
|
if(runtime·cas(&sig.state, old, new)) {
|
|
|
|
if (new == HASWAITER) {
|
2013-02-20 09:21:45 -07:00
|
|
|
runtime·entersyscallblock();
|
2012-12-28 04:36:06 -07:00
|
|
|
runtime·notesleep(&sig);
|
|
|
|
runtime·exitsyscall();
|
|
|
|
runtime·noteclear(&sig);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
}
|
|
|
|
|
2012-12-28 04:36:06 -07:00
|
|
|
// Get a new local copy.
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
for(i=0; i<nelem(sig.mask); i++) {
|
|
|
|
for(;;) {
|
|
|
|
m = sig.mask[i];
|
|
|
|
if(runtime·cas(&sig.mask[i], m, 0))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
recv[i] = m;
|
|
|
|
}
|
2009-12-15 19:21:29 -07:00
|
|
|
}
|
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
done:;
|
|
|
|
// goc requires that we fall off the end of functions
|
|
|
|
// that return values instead of using our own return
|
|
|
|
// statements.
|
2009-12-15 19:21:29 -07:00
|
|
|
}
|
2009-12-16 21:20:50 -07:00
|
|
|
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
// Must only be called from a single goroutine at a time.
|
|
|
|
func signal_enable(s uint32) {
|
|
|
|
if(!sig.inuse) {
|
|
|
|
// The first call to signal_enable is for us
|
|
|
|
// to use for initialization. It does not pass
|
|
|
|
// signal information in m.
|
|
|
|
sig.inuse = true; // enable reception of signals; cannot disable
|
|
|
|
runtime·noteclear(&sig);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s >= nelem(sig.wanted)*32)
|
|
|
|
return;
|
|
|
|
sig.wanted[s/32] |= 1U<<(s&31);
|
2012-02-16 20:36:40 -07:00
|
|
|
runtime·sigenable(s);
|
2009-12-16 21:20:50 -07:00
|
|
|
}
|
2013-03-14 22:00:02 -06:00
|
|
|
|
|
|
|
// Must only be called from a single goroutine at a time.
|
|
|
|
func signal_disable(s uint32) {
|
|
|
|
if(s >= nelem(sig.wanted)*32)
|
|
|
|
return;
|
|
|
|
sig.wanted[s/32] &= ~(1U<<(s&31));
|
|
|
|
runtime·sigdisable(s);
|
|
|
|
}
|
2013-07-11 14:39:39 -06:00
|
|
|
|
|
|
|
// This runs on a foreign stack, without an m or a g. No stack split.
|
|
|
|
#pragma textflag 7
|
|
|
|
void
|
|
|
|
runtime·badsignal(uintptr sig)
|
|
|
|
{
|
|
|
|
runtime·cgocallback((void (*)(void))runtime·sigsend, &sig, sizeof(sig));
|
|
|
|
}
|