callbacks.go 3.6 KB

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