...

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

View as plain text