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

View as plain text