Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/export_unix_test.go

Documentation: runtime

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

View as plain text