summaryrefslogtreecommitdiff
path: root/vendor/github.com/ebitengine/purego/internal/fakecgo/go_libinit.go
blob: e5a66f39d4f3f545ad98f643e77659db5cbfee4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors

//go:build !cgo && (darwin || freebsd || linux)

package fakecgo

import (
	"syscall"
	"unsafe"
)

var (
	pthread_g pthread_key_t

	runtime_init_cond = PTHREAD_COND_INITIALIZER
	runtime_init_mu   = PTHREAD_MUTEX_INITIALIZER
	runtime_init_done int
)

//go:nosplit
//go:norace
func x_cgo_notify_runtime_init_done() {
	pthread_mutex_lock(&runtime_init_mu)
	runtime_init_done = 1
	pthread_cond_broadcast(&runtime_init_cond)
	pthread_mutex_unlock(&runtime_init_mu)
}

// Store the g into a thread-specific value associated with the pthread key pthread_g.
// And pthread_key_destructor will dropm when the thread is exiting.
//
//go:norace
func x_cgo_bindm(g unsafe.Pointer) {
	// We assume this will always succeed, otherwise, there might be extra M leaking,
	// when a C thread exits after a cgo call.
	// We only invoke this function once per thread in runtime.needAndBindM,
	// and the next calls just reuse the bound m.
	pthread_setspecific(pthread_g, g)
}

// _cgo_try_pthread_create retries pthread_create if it fails with
// EAGAIN.
//
//go:nosplit
//go:norace
func _cgo_try_pthread_create(thread *pthread_t, attr *pthread_attr_t, pfn unsafe.Pointer, arg *ThreadStart) int {
	var ts syscall.Timespec
	// tries needs to be the same type as syscall.Timespec.Nsec
	// but the fields are int32 on 32bit and int64 on 64bit.
	// tries is assigned to syscall.Timespec.Nsec in order to match its type.
	tries := ts.Nsec
	var err int

	for tries = 0; tries < 20; tries++ {
		// inlined this call because it ran out of stack when inlining was disabled
		err = int(call5(pthread_createABI0, uintptr(unsafe.Pointer(thread)), uintptr(unsafe.Pointer(attr)), uintptr(pfn), uintptr(unsafe.Pointer(arg)), 0))
		if err == 0 {
			// inlined this call because it ran out of stack when inlining was disabled
			call5(pthread_detachABI0, uintptr(*thread), 0, 0, 0, 0)
			return 0
		}
		if err != int(syscall.EAGAIN) {
			return err
		}
		ts.Sec = 0
		ts.Nsec = (tries + 1) * 1000 * 1000 // Milliseconds.
		// inlined this call because it ran out of stack when inlining was disabled
		call5(nanosleepABI0, uintptr(unsafe.Pointer(&ts)), 0, 0, 0, 0)
	}
	return int(syscall.EAGAIN)
}