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
|
|
|
|
// the signal handler (it cannot block and cannot use locks)
|
|
|
|
// so the handler communicates with a processing goroutine
|
|
|
|
// via struct sig, below.
|
|
|
|
//
|
|
|
|
// Ownership for sig.Note passes back and forth between
|
|
|
|
// the signal handler and the signal goroutine in rounds.
|
2012-02-13 19:51:38 -07:00
|
|
|
// The initial state is that sig.note is cleared (setup by signal_enable).
|
2009-12-15 19:21:29 -07:00
|
|
|
// At the beginning of each round, mask == 0.
|
|
|
|
// The round goes through three stages:
|
|
|
|
//
|
|
|
|
// (In parallel)
|
|
|
|
// 1a) One or more signals arrive and are handled
|
|
|
|
// by sigsend using cas to set bits in sig.mask.
|
|
|
|
// The handler that changes sig.mask from zero to non-zero
|
|
|
|
// calls notewakeup(&sig).
|
|
|
|
// 1b) Sigrecv calls notesleep(&sig) to wait for the wakeup.
|
|
|
|
//
|
|
|
|
// 2) Having received the wakeup, sigrecv knows that sigsend
|
|
|
|
// will not send another wakeup, so it can noteclear(&sig)
|
|
|
|
// to prepare for the next round. (Sigsend may still be adding
|
|
|
|
// signals to sig.mask at this point, which is fine.)
|
|
|
|
//
|
|
|
|
// 3) Sigrecv uses cas to grab the current sig.mask and zero it,
|
|
|
|
// triggering the next round.
|
|
|
|
//
|
|
|
|
// The signal handler takes ownership of the note by atomically
|
|
|
|
// changing mask from a zero to non-zero value. It gives up
|
|
|
|
// ownership by calling notewakeup. The signal goroutine takes
|
|
|
|
// ownership by returning from notesleep (caused by the notewakeup)
|
|
|
|
// and gives up ownership by clearing mask.
|
|
|
|
|
|
|
|
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"
|
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];
|
|
|
|
uint32 kick;
|
2009-12-16 21:20:50 -07:00
|
|
|
bool inuse;
|
2009-12-15 19:21:29 -07:00
|
|
|
} sig;
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
|
|
|
uint32 bit, mask;
|
|
|
|
|
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.
|
|
|
|
if(runtime·cas(&sig.kick, 1, 0))
|
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·notewakeup(&sig);
|
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)];
|
|
|
|
int32 i, more;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new local copy.
|
|
|
|
// Ask for a kick if more signals come in
|
|
|
|
// during or after our check (before the sleep).
|
|
|
|
if(sig.kick == 0) {
|
|
|
|
runtime·noteclear(&sig);
|
|
|
|
runtime·cas(&sig.kick, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
more = 0;
|
|
|
|
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;
|
|
|
|
if(m != 0)
|
|
|
|
more = 1;
|
|
|
|
}
|
|
|
|
if(more)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Sleep waiting for more.
|
|
|
|
runtime·entersyscall();
|
|
|
|
runtime·notesleep(&sig);
|
|
|
|
runtime·exitsyscall();
|
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) {
|
|
|
|
int32 i;
|
|
|
|
|
|
|
|
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 == 0) {
|
|
|
|
// Special case: want everything.
|
|
|
|
for(i=0; i<nelem(sig.wanted); i++)
|
|
|
|
sig.wanted[i] = ~(uint32)0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s >= nelem(sig.wanted)*32)
|
|
|
|
return;
|
|
|
|
sig.wanted[s/32] |= 1U<<(s&31);
|
2009-12-16 21:20:50 -07:00
|
|
|
}
|