github.com/go-darwin/sys@v0.0.0-20220510002607-68fd01f054ca/runtime.go (about)

     1  // Copyright 2021 The Go Darwin Authors
     2  // SPDX-License-Identifier: BSD-3-Clause
     3  
     4  // Copyright 2009 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  //go:build darwin
     9  // +build darwin
    10  
    11  package sys
    12  
    13  import (
    14  	_ "unsafe" // for go:linkname
    15  )
    16  
    17  // Keep a cached value to make gotraceback fast,
    18  // since we call it on every call to gentraceback.
    19  // The cached value is a uint32 in which the low bits
    20  // are the "crash" and "all" settings and the remaining
    21  // bits are the traceback value (0 off, 1 on, 2 include system).
    22  const (
    23  	TracebackCrash = 1 << iota
    24  	TracebackAll
    25  	TracebackShift = iota
    26  )
    27  
    28  //go:noescape
    29  //go:linkname systemstack runtime.systemstack
    30  func systemstack(fn func())
    31  
    32  // SystemStack runs fn on a system stack.
    33  // If systemstack is called from the per-OS-thread (g0) stack, or
    34  // if systemstack is called from the signal handling (gsignal) stack,
    35  // systemstack calls fn directly and returns.
    36  // Otherwise, systemstack is being called from the limited stack
    37  // of an ordinary goroutine. In this case, systemstack switches
    38  // to the per-OS-thread stack, calls fn, and switches back.
    39  // It is common to use a func literal as the argument, in order
    40  // to share inputs and outputs with the code around the call
    41  // to system stack:
    42  //
    43  //	... set up y ...
    44  //	systemstack(func() {
    45  //		x = bigcall(y)
    46  //	})
    47  //	... use x ...
    48  func SystemStack(fn func()) {
    49  	systemstack(fn)
    50  }