Source file src/runtime/cgo/callbacks.go
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 package cgo 6 7 import "unsafe" 8 9 // These utility functions are available to be called from code 10 // compiled with gcc via crosscall2. 11 12 // The declaration of crosscall2 is: 13 // void crosscall2(void (*fn)(void *), void *, int); 14 // 15 // We need to export the symbol crosscall2 in order to support 16 // callbacks from shared libraries. This applies regardless of 17 // linking mode. 18 // 19 // Compatibility note: SWIG uses crosscall2 in exactly one situation: 20 // to call _cgo_panic using the pattern shown below. We need to keep 21 // that pattern working. In particular, crosscall2 actually takes four 22 // arguments, but it works to call it with three arguments when 23 // calling _cgo_panic. 24 //go:cgo_export_static crosscall2 25 //go:cgo_export_dynamic crosscall2 26 27 // Panic. The argument is converted into a Go string. 28 29 // Call like this in code compiled with gcc: 30 // struct { const char *p; } a; 31 // a.p = /* string to pass to panic */; 32 // crosscall2(_cgo_panic, &a, sizeof a); 33 // /* The function call will not return. */ 34 35 // TODO: We should export a regular C function to panic, change SWIG 36 // to use that instead of the above pattern, and then we can drop 37 // backwards-compatibility from crosscall2 and stop exporting it. 38 39 //go:linkname _runtime_cgo_panic_internal runtime._cgo_panic_internal 40 func _runtime_cgo_panic_internal(p *byte) 41 42 //go:linkname _cgo_panic _cgo_panic 43 //go:cgo_export_static _cgo_panic 44 //go:cgo_export_dynamic _cgo_panic 45 func _cgo_panic(a *struct{ cstr *byte }) { 46 _runtime_cgo_panic_internal(a.cstr) 47 } 48 49 //go:cgo_import_static x_cgo_init 50 //go:linkname x_cgo_init x_cgo_init 51 //go:linkname _cgo_init _cgo_init 52 var x_cgo_init byte 53 var _cgo_init = &x_cgo_init 54 55 //go:cgo_import_static x_cgo_thread_start 56 //go:linkname x_cgo_thread_start x_cgo_thread_start 57 //go:linkname _cgo_thread_start _cgo_thread_start 58 var x_cgo_thread_start byte 59 var _cgo_thread_start = &x_cgo_thread_start 60 61 // Creates a new system thread without updating any Go state. 62 // 63 // This method is invoked during shared library loading to create a new OS 64 // thread to perform the runtime initialization. This method is similar to 65 // _cgo_sys_thread_start except that it doesn't update any Go state. 66 67 //go:cgo_import_static x_cgo_sys_thread_create 68 //go:linkname x_cgo_sys_thread_create x_cgo_sys_thread_create 69 //go:linkname _cgo_sys_thread_create _cgo_sys_thread_create 70 var x_cgo_sys_thread_create byte 71 var _cgo_sys_thread_create = &x_cgo_sys_thread_create 72 73 // Notifies that the runtime has been initialized. 74 // 75 // We currently block at every CGO entry point (via _cgo_wait_runtime_init_done) 76 // to ensure that the runtime has been initialized before the CGO call is 77 // executed. This is necessary for shared libraries where we kickoff runtime 78 // initialization in a separate thread and return without waiting for this 79 // thread to complete the init. 80 81 //go:cgo_import_static x_cgo_notify_runtime_init_done 82 //go:linkname x_cgo_notify_runtime_init_done x_cgo_notify_runtime_init_done 83 //go:linkname _cgo_notify_runtime_init_done _cgo_notify_runtime_init_done 84 var x_cgo_notify_runtime_init_done byte 85 var _cgo_notify_runtime_init_done = &x_cgo_notify_runtime_init_done 86 87 // Sets the traceback context function. See runtime.SetCgoTraceback. 88 89 //go:cgo_import_static x_cgo_set_context_function 90 //go:linkname x_cgo_set_context_function x_cgo_set_context_function 91 //go:linkname _cgo_set_context_function _cgo_set_context_function 92 var x_cgo_set_context_function byte 93 var _cgo_set_context_function = &x_cgo_set_context_function 94 95 // Calls a libc function to execute background work injected via libc 96 // interceptors, such as processing pending signals under the thread 97 // sanitizer. 98 // 99 // Left as a nil pointer if no libc interceptors are expected. 100 101 //go:cgo_import_static _cgo_yield 102 //go:linkname _cgo_yield _cgo_yield 103 var _cgo_yield unsafe.Pointer 104 105 //go:cgo_export_static _cgo_topofstack 106 //go:cgo_export_dynamic _cgo_topofstack 107