github.com/ebitengine/purego@v0.8.0-alpha.2.0.20240512170805-6cd12240d332/internal/fakecgo/go_darwin_amd64.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 !cgo
     6  
     7  package fakecgo
     8  
     9  import "unsafe"
    10  
    11  //go:nosplit
    12  //go:norace
    13  func _cgo_sys_thread_start(ts *ThreadStart) {
    14  	var attr pthread_attr_t
    15  	var ign, oset sigset_t
    16  	var p pthread_t
    17  	var size size_t
    18  	var err int
    19  
    20  	sigfillset(&ign)
    21  	pthread_sigmask(SIG_SETMASK, &ign, &oset)
    22  
    23  	size = pthread_get_stacksize_np(pthread_self())
    24  	pthread_attr_init(&attr)
    25  	pthread_attr_setstacksize(&attr, size)
    26  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    27  	ts.g.stackhi = uintptr(size)
    28  
    29  	err = _cgo_try_pthread_create(&p, &attr, unsafe.Pointer(threadentry_trampolineABI0), ts)
    30  
    31  	pthread_sigmask(SIG_SETMASK, &oset, nil)
    32  
    33  	if err != 0 {
    34  		print("fakecgo: pthread_create failed: ")
    35  		println(err)
    36  		abort()
    37  	}
    38  }
    39  
    40  // threadentry_trampolineABI0 maps the C ABI to Go ABI then calls the Go function
    41  //
    42  //go:linkname x_threadentry_trampoline threadentry_trampoline
    43  var x_threadentry_trampoline byte
    44  var threadentry_trampolineABI0 = &x_threadentry_trampoline
    45  
    46  //go:nosplit
    47  //go:norace
    48  func threadentry(v unsafe.Pointer) unsafe.Pointer {
    49  	ts := *(*ThreadStart)(v)
    50  	free(v)
    51  
    52  	setg_trampoline(setg_func, uintptr(unsafe.Pointer(ts.g)))
    53  
    54  	// faking funcs in go is a bit a... involved - but the following works :)
    55  	fn := uintptr(unsafe.Pointer(&ts.fn))
    56  	(*(*func())(unsafe.Pointer(&fn)))()
    57  
    58  	return nil
    59  }
    60  
    61  // here we will store a pointer to the provided setg func
    62  var setg_func uintptr
    63  
    64  //go:nosplit
    65  //go:norace
    66  func x_cgo_init(g *G, setg uintptr) {
    67  	var size size_t
    68  
    69  	setg_func = setg
    70  
    71  	size = pthread_get_stacksize_np(pthread_self())
    72  	g.stacklo = uintptr(unsafe.Add(unsafe.Pointer(&size), -size+4096))
    73  }