Source file src/runtime/cgo.go

     1  // Copyright 2014 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  package runtime
     6  
     7  import "unsafe"
     8  
     9  //go:cgo_export_static main
    10  
    11  // Filled in by runtime/cgo when linked into binary.
    12  
    13  //go:linkname _cgo_init _cgo_init
    14  //go:linkname _cgo_thread_start _cgo_thread_start
    15  //go:linkname _cgo_sys_thread_create _cgo_sys_thread_create
    16  //go:linkname _cgo_notify_runtime_init_done _cgo_notify_runtime_init_done
    17  //go:linkname _cgo_callers _cgo_callers
    18  //go:linkname _cgo_set_traceback_functions _cgo_set_traceback_functions
    19  //go:linkname _cgo_call_traceback_function _cgo_call_traceback_function
    20  //go:linkname _cgo_call_symbolizer_function _cgo_call_symbolizer_function
    21  //go:linkname _cgo_yield _cgo_yield
    22  //go:linkname _cgo_pthread_key_created _cgo_pthread_key_created
    23  //go:linkname _cgo_bindm _cgo_bindm
    24  //go:linkname _cgo_getstackbound _cgo_getstackbound
    25  
    26  var (
    27  	_cgo_init                     unsafe.Pointer
    28  	_cgo_thread_start             unsafe.Pointer
    29  	_cgo_sys_thread_create        unsafe.Pointer
    30  	_cgo_notify_runtime_init_done unsafe.Pointer
    31  	_cgo_callers                  unsafe.Pointer
    32  	_cgo_set_traceback_functions  unsafe.Pointer
    33  	_cgo_call_traceback_function  unsafe.Pointer
    34  	_cgo_call_symbolizer_function unsafe.Pointer
    35  	_cgo_yield                    unsafe.Pointer
    36  	_cgo_pthread_key_created      unsafe.Pointer
    37  	_cgo_bindm                    unsafe.Pointer
    38  	_cgo_getstackbound            unsafe.Pointer
    39  )
    40  
    41  // iscgo is set to true by the runtime/cgo package
    42  //
    43  // iscgo should be an internal detail,
    44  // but widely used packages access it using linkname.
    45  // Notable members of the hall of shame include:
    46  //   - github.com/ebitengine/purego
    47  //
    48  // Do not remove or change the type signature.
    49  // See go.dev/issue/67401.
    50  //
    51  //go:linkname iscgo
    52  var iscgo bool
    53  
    54  // set_crosscall2 is set by the runtime/cgo package
    55  // set_crosscall2 should be an internal detail,
    56  // but widely used packages access it using linkname.
    57  // Notable members of the hall of shame include:
    58  //   - github.com/ebitengine/purego
    59  //
    60  // Do not remove or change the type signature.
    61  // See go.dev/issue/67401.
    62  //
    63  //go:linkname set_crosscall2
    64  var set_crosscall2 func()
    65  
    66  // cgoHasExtraM is set on startup when an extra M is created for cgo.
    67  // The extra M must be created before any C/C++ code calls cgocallback.
    68  var cgoHasExtraM bool
    69  
    70  // cgoUse is called by cgo-generated code (using go:linkname to get at
    71  // an unexported name). The calls serve two purposes:
    72  // 1) they are opaque to escape analysis, so the argument is considered to
    73  // escape to the heap.
    74  // 2) they keep the argument alive until the call site; the call is emitted after
    75  // the end of the (presumed) use of the argument by C.
    76  // cgoUse should not actually be called (see cgoAlwaysFalse).
    77  func cgoUse(any) { throw("cgoUse should not be called") }
    78  
    79  // cgoKeepAlive is called by cgo-generated code (using go:linkname to get at
    80  // an unexported name). This call keeps its argument alive until the call site;
    81  // cgo emits the call after the last possible use of the argument by C code.
    82  // cgoKeepAlive is marked in the cgo-generated code as //go:noescape, so
    83  // unlike cgoUse it does not force the argument to escape to the heap.
    84  // This is used to implement the #cgo noescape directive.
    85  func cgoKeepAlive(any) { throw("cgoKeepAlive should not be called") }
    86  
    87  // cgoAlwaysFalse is a boolean value that is always false.
    88  // The cgo-generated code says if cgoAlwaysFalse { cgoUse(p) },
    89  // or if cgoAlwaysFalse { cgoKeepAlive(p) }.
    90  // The compiler cannot see that cgoAlwaysFalse is always false,
    91  // so it emits the test and keeps the call, giving the desired
    92  // escape/alive analysis result. The test is cheaper than the call.
    93  var cgoAlwaysFalse bool
    94  
    95  var cgo_yield = &_cgo_yield
    96  
    97  func cgoNoCallback(v bool) {
    98  	g := getg()
    99  	if g.nocgocallback && v {
   100  		panic("runtime: unexpected setting cgoNoCallback")
   101  	}
   102  	g.nocgocallback = v
   103  }
   104  

View as plain text