github.com/jwijenbergh/purego@v0.0.0-20240126093400-70ff3a61df13/internal/fakecgo/callbacks.go (about)

     1  // Copyright 2011 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 darwin || freebsd || linux
     6  
     7  package fakecgo
     8  
     9  import (
    10  	_ "unsafe"
    11  )
    12  
    13  // TODO: decide if we need _runtime_cgo_panic_internal
    14  
    15  //go:linkname x_cgo_init_trampoline x_cgo_init_trampoline
    16  //go:linkname _cgo_init _cgo_init
    17  var x_cgo_init_trampoline byte
    18  var _cgo_init = &x_cgo_init_trampoline
    19  
    20  // Creates a new system thread without updating any Go state.
    21  //
    22  // This method is invoked during shared library loading to create a new OS
    23  // thread to perform the runtime initialization. This method is similar to
    24  // _cgo_sys_thread_start except that it doesn't update any Go state.
    25  
    26  //go:linkname x_cgo_thread_start_trampoline x_cgo_thread_start_trampoline
    27  //go:linkname _cgo_thread_start _cgo_thread_start
    28  var x_cgo_thread_start_trampoline byte
    29  var _cgo_thread_start = &x_cgo_thread_start_trampoline
    30  
    31  // Notifies that the runtime has been initialized.
    32  //
    33  // We currently block at every CGO entry point (via _cgo_wait_runtime_init_done)
    34  // to ensure that the runtime has been initialized before the CGO call is
    35  // executed. This is necessary for shared libraries where we kickoff runtime
    36  // initialization in a separate thread and return without waiting for this
    37  // thread to complete the init.
    38  
    39  //go:linkname x_cgo_notify_runtime_init_done_trampoline x_cgo_notify_runtime_init_done_trampoline
    40  //go:linkname _cgo_notify_runtime_init_done _cgo_notify_runtime_init_done
    41  var x_cgo_notify_runtime_init_done_trampoline byte
    42  var _cgo_notify_runtime_init_done = &x_cgo_notify_runtime_init_done_trampoline
    43  
    44  // Indicates whether a dummy thread key has been created or not.
    45  //
    46  // When calling go exported function from C, we register a destructor
    47  // callback, for a dummy thread key, by using pthread_key_create.
    48  
    49  //go:linkname _cgo_pthread_key_created _cgo_pthread_key_created
    50  var x_cgo_pthread_key_created uintptr
    51  var _cgo_pthread_key_created = &x_cgo_pthread_key_created
    52  
    53  // Set the x_crosscall2_ptr C function pointer variable point to crosscall2.
    54  // It's for the runtime package to call at init time.
    55  func set_crosscall2() {
    56  	// nothing needs to be done here for fakecgo
    57  	// because it's possible to just call cgocallback directly
    58  }
    59  
    60  //go:linkname _set_crosscall2 runtime.set_crosscall2
    61  var _set_crosscall2 = set_crosscall2
    62  
    63  // Store the g into the thread-specific value.
    64  // So that pthread_key_destructor will dropm when the thread is exiting.
    65  
    66  //go:linkname x_cgo_bindm_trampoline x_cgo_bindm_trampoline
    67  //go:linkname _cgo_bindm _cgo_bindm
    68  var x_cgo_bindm_trampoline byte
    69  var _cgo_bindm = &x_cgo_bindm_trampoline
    70  
    71  // TODO: decide if we need x_cgo_set_context_function
    72  // TODO: decide if we need _cgo_yield
    73  
    74  var (
    75  	// In Go 1.20 the race detector was rewritten to pure Go
    76  	// on darwin. This means that when CGO_ENABLED=0 is set
    77  	// fakecgo is built with race detector code. This is not
    78  	// good since this code is pretending to be C. The go:norace
    79  	// pragma is not enough, since it only applies to the native
    80  	// ABIInternal function. The ABIO wrapper (which is necessary,
    81  	// since all references to text symbols from assembly will use it)
    82  	// does not inherit the go:norace pragma, so it will still be
    83  	// instrumented by the race detector.
    84  	//
    85  	// To circumvent this issue, using closure calls in the
    86  	// assembly, which forces the compiler to use the ABIInternal
    87  	// native implementation (which has go:norace) instead.
    88  	threadentry_call        = threadentry
    89  	x_cgo_init_call         = x_cgo_init
    90  	x_cgo_setenv_call       = x_cgo_setenv
    91  	x_cgo_unsetenv_call     = x_cgo_unsetenv
    92  	x_cgo_thread_start_call = x_cgo_thread_start
    93  )