Source file src/runtime/export_unix_test.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build unix
     6  
     7  package runtime
     8  
     9  import "unsafe"
    10  
    11  var NonblockingPipe = nonblockingPipe
    12  var Closeonexec = closeonexec
    13  
    14  func sigismember(mask *sigset, i int) bool {
    15  	clear := *mask
    16  	sigdelset(&clear, i)
    17  	return clear != *mask
    18  }
    19  
    20  func Sigisblocked(i int) bool {
    21  	var sigmask sigset
    22  	sigprocmask(_SIG_SETMASK, nil, &sigmask)
    23  	return sigismember(&sigmask, i)
    24  }
    25  
    26  type M = m
    27  
    28  var waitForSigusr1 struct {
    29  	rdpipe int32
    30  	wrpipe int32
    31  	mID    int64
    32  }
    33  
    34  // WaitForSigusr1 blocks until a SIGUSR1 is received. It calls ready
    35  // when it is set up to receive SIGUSR1. The ready function should
    36  // cause a SIGUSR1 to be sent. The r and w arguments are a pipe that
    37  // the signal handler can use to report when the signal is received.
    38  //
    39  // Once SIGUSR1 is received, it returns the ID of the current M and
    40  // the ID of the M the SIGUSR1 was received on. If the caller writes
    41  // a non-zero byte to w, WaitForSigusr1 returns immediately with -1, -1.
    42  func WaitForSigusr1(r, w int32, ready func(mp *M)) (int64, int64) {
    43  	lockOSThread()
    44  	// Make sure we can receive SIGUSR1.
    45  	unblocksig(_SIGUSR1)
    46  
    47  	waitForSigusr1.rdpipe = r
    48  	waitForSigusr1.wrpipe = w
    49  
    50  	mp := getg().m
    51  	testSigusr1 = waitForSigusr1Callback
    52  	ready(mp)
    53  
    54  	// Wait for the signal. We use a pipe rather than a note
    55  	// because write is always async-signal-safe.
    56  	entersyscallblock()
    57  	var b byte
    58  	read(waitForSigusr1.rdpipe, noescape(unsafe.Pointer(&b)), 1)
    59  	exitsyscall()
    60  
    61  	gotM := waitForSigusr1.mID
    62  	testSigusr1 = nil
    63  
    64  	unlockOSThread()
    65  
    66  	if b != 0 {
    67  		// timeout signal from caller
    68  		return -1, -1
    69  	}
    70  	return mp.id, gotM
    71  }
    72  
    73  // waitForSigusr1Callback is called from the signal handler during
    74  // WaitForSigusr1. It must not have write barriers because there may
    75  // not be a P.
    76  //
    77  //go:nowritebarrierrec
    78  func waitForSigusr1Callback(gp *g) bool {
    79  	if gp == nil || gp.m == nil {
    80  		waitForSigusr1.mID = -1
    81  	} else {
    82  		waitForSigusr1.mID = gp.m.id
    83  	}
    84  	b := byte(0)
    85  	write(uintptr(waitForSigusr1.wrpipe), noescape(unsafe.Pointer(&b)), 1)
    86  	return true
    87  }
    88  
    89  // SendSigusr1 sends SIGUSR1 to mp.
    90  func SendSigusr1(mp *M) {
    91  	signalM(mp, _SIGUSR1)
    92  }
    93  

View as plain text