internal/fakecgo: refactor (#62)
* remove comment about <> Carrots make the symbol only available in that source file. We need these functions available outside in order for us to call them. * use syscall.Timespec Use the syscall version of this type. That way we can clean up our code and not have duplicated types. * remove duplicate type arguments * remove // +build tags * update comment
- commit
- 2ebcfb9c0764eb218b2cf2bb4e8ecaa8213f11f4
- author
- TotallyGamerJet · · 2022-11-08 04:52 UTC
- committer
- GitHub · · 2022-11-08 04:52 UTC
- parent
- 6dfb1bb
- signature
- GPG Unverified
4AEE18F83AFDEB23The key is unknown
6 files · +14 -20
M dlfcn_stubs.s +0 -1
| @@ -2,7 +2,6 @@ | |||
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | 4 | //go:build darwin | |
| 5 | - | // +build darwin | |
| 6 | 5 | ||
| 7 | 6 | #include "textflag.h" | |
| 8 | 7 | ||
| @@ -2,7 +2,6 @@ | |||||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | //go:build darwin | 4 | //go:build darwin | ||
| 5 | - | // +build darwin | |||
| 6 | 5 | ||||
| 7 | #include "textflag.h" | 6 | #include "textflag.h" | ||
| 8 | 7 | ||||
M internal/fakecgo/go_libinit.go +7 -4
| @@ -20,9 +20,12 @@ func x_cgo_notify_runtime_init_done() { | |||
| 20 | 20 | // | |
| 21 | 21 | //go:nosplit | |
| 22 | 22 | func _cgo_try_pthread_create(thread *pthread_t, attr *pthread_attr_t, pfn unsafe.Pointer, arg *ThreadStart) int { | |
| 23 | - | var tries int | |
| 23 | + | var ts syscall.Timespec | |
| 24 | + | // tries needs to be the same type as syscall.Timespec.Nsec | |
| 25 | + | // but the fields are int32 on 32bit and int64 on 64bit. | |
| 26 | + | // tries is assigned to syscall.Timespec.Nsec in order to match its type. | |
| 27 | + | var tries = ts.Nsec | |
| 24 | 28 | var err int | |
| 25 | - | var ts timespec | |
| 26 | 29 | ||
| 27 | 30 | for tries = 0; tries < 20; tries++ { | |
| 28 | 31 | err = int(pthread_create(thread, attr, pfn, unsafe.Pointer(arg))) | |
| @@ -33,8 +36,8 @@ func _cgo_try_pthread_create(thread *pthread_t, attr *pthread_attr_t, pfn unsafe | |||
| 33 | 36 | if err != int(syscall.EAGAIN) { | |
| 34 | 37 | return err | |
| 35 | 38 | } | |
| 36 | - | ts.tv_sec = 0 | |
| 37 | - | ts.tv_nsec = (tries + 1) * 1000 * 1000 // Milliseconds. | |
| 39 | + | ts.Sec = 0 | |
| 40 | + | ts.Nsec = (tries + 1) * 1000 * 1000 // Milliseconds. | |
| 38 | 41 | nanosleep(&ts, nil) | |
| 39 | 42 | } | |
| 40 | 43 | return int(syscall.EAGAIN) | |
| @@ -20,9 +20,12 @@ func x_cgo_notify_runtime_init_done() { | |||||
| 20 | // | 20 | // | ||
| 21 | //go:nosplit | 21 | //go:nosplit | ||
| 22 | func _cgo_try_pthread_create(thread *pthread_t, attr *pthread_attr_t, pfn unsafe.Pointer, arg *ThreadStart) int { | 22 | func _cgo_try_pthread_create(thread *pthread_t, attr *pthread_attr_t, pfn unsafe.Pointer, arg *ThreadStart) int { | ||
| 23 | - | var tries int | 23 | + | var ts syscall.Timespec |
| 24 | + | // tries needs to be the same type as syscall.Timespec.Nsec | |||
| 25 | + | // but the fields are int32 on 32bit and int64 on 64bit. | |||
| 26 | + | // tries is assigned to syscall.Timespec.Nsec in order to match its type. | |||
| 27 | + | var tries = ts.Nsec | |||
| 24 | var err int | 28 | var err int | ||
| 25 | - | var ts timespec | |||
| 26 | 29 | ||||
| 27 | for tries = 0; tries < 20; tries++ { | 30 | for tries = 0; tries < 20; tries++ { | ||
| 28 | err = int(pthread_create(thread, attr, pfn, unsafe.Pointer(arg))) | 31 | err = int(pthread_create(thread, attr, pfn, unsafe.Pointer(arg))) | ||
| @@ -33,8 +36,8 @@ func _cgo_try_pthread_create(thread *pthread_t, attr *pthread_attr_t, pfn unsafe | |||||
| 33 | if err != int(syscall.EAGAIN) { | 36 | if err != int(syscall.EAGAIN) { | ||
| 34 | return err | 37 | return err | ||
| 35 | } | 38 | } | ||
| 36 | - | ts.tv_sec = 0 | 39 | + | ts.Sec = 0 |
| 37 | - | ts.tv_nsec = (tries + 1) * 1000 * 1000 // Milliseconds. | 40 | + | ts.Nsec = (tries + 1) * 1000 * 1000 // Milliseconds. |
| 38 | nanosleep(&ts, nil) | 41 | nanosleep(&ts, nil) | ||
| 39 | } | 42 | } | ||
| 40 | return int(syscall.EAGAIN) | 43 | return int(syscall.EAGAIN) | ||
M internal/fakecgo/libcgo.go +0 -7
| @@ -6,17 +6,10 @@ | |||
| 6 | 6 | package fakecgo | |
| 7 | 7 | ||
| 8 | 8 | type size_t uintptr | |
| 9 | - | ||
| 10 | 9 | type sigset_t [128]byte // TODO: figure out how big this should be | |
| 11 | 10 | type pthread_attr_t [56]byte // TODO: figure out how big this should be | |
| 12 | 11 | type pthread_t int | |
| 13 | 12 | ||
| 14 | - | // We could take timespec from syscall - but there it uses int32 and int64 for 32 bit and 64 bit arch, which complicates stuff for us | |
| 15 | - | type timespec struct { | |
| 16 | - | tv_sec int | |
| 17 | - | tv_nsec int | |
| 18 | - | } | |
| 19 | - | ||
| 20 | 13 | // for pthread_sigmask: | |
| 21 | 14 | ||
| 22 | 15 | type sighow int32 | |
| @@ -6,17 +6,10 @@ | |||||
| 6 | package fakecgo | 6 | package fakecgo | ||
| 7 | 7 | ||||
| 8 | type size_t uintptr | 8 | type size_t uintptr | ||
| 9 | - | ||||
| 10 | type sigset_t [128]byte // TODO: figure out how big this should be | 9 | type sigset_t [128]byte // TODO: figure out how big this should be | ||
| 11 | type pthread_attr_t [56]byte // TODO: figure out how big this should be | 10 | type pthread_attr_t [56]byte // TODO: figure out how big this should be | ||
| 12 | type pthread_t int | 11 | type pthread_t int | ||
| 13 | 12 | ||||
| 14 | - | // We could take timespec from syscall - but there it uses int32 and int64 for 32 bit and 64 bit arch, which complicates stuff for us | |||
| 15 | - | type timespec struct { | |||
| 16 | - | tv_sec int | |||
| 17 | - | tv_nsec int | |||
| 18 | - | } | |||
| 19 | - | ||||
| 20 | // for pthread_sigmask: | 13 | // for pthread_sigmask: | ||
| 21 | 14 | ||||
| 22 | type sighow int32 | 15 | type sighow int32 | ||
M internal/fakecgo/symbols.go +7 -4
| @@ -5,7 +5,10 @@ | |||
| 5 | 5 | ||
| 6 | 6 | package fakecgo | |
| 7 | 7 | ||
| 8 | - | import "unsafe" | |
| 8 | + | import ( | |
| 9 | + | "syscall" | |
| 10 | + | "unsafe" | |
| 11 | + | ) | |
| 9 | 12 | ||
| 10 | 13 | // setg_trampoline calls setg with the G provided | |
| 11 | 14 | func setg_trampoline(setg uintptr, G uintptr) | |
| @@ -26,7 +29,7 @@ func free(ptr unsafe.Pointer) { | |||
| 26 | 29 | call5(freeABI0, uintptr(ptr), 0, 0, 0, 0) | |
| 27 | 30 | } | |
| 28 | 31 | ||
| 29 | - | func setenv(name *byte, value *byte, overwrite int32) int32 { | |
| 32 | + | func setenv(name, value *byte, overwrite int32) int32 { | |
| 30 | 33 | return int32(call5(setenvABI0, uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(value)), uintptr(overwrite), 0, 0)) | |
| 31 | 34 | } | |
| 32 | 35 | ||
| @@ -46,7 +49,7 @@ func pthread_detach(thread pthread_t) int32 { | |||
| 46 | 49 | return int32(call5(pthread_detachABI0, uintptr(thread), 0, 0, 0, 0)) | |
| 47 | 50 | } | |
| 48 | 51 | ||
| 49 | - | func pthread_sigmask(how sighow, ign *sigset_t, oset *sigset_t) int32 { | |
| 52 | + | func pthread_sigmask(how sighow, ign, oset *sigset_t) int32 { | |
| 50 | 53 | return int32(call5(pthread_sigmaskABI0, uintptr(how), uintptr(unsafe.Pointer(ign)), uintptr(unsafe.Pointer(oset)), 0, 0)) | |
| 51 | 54 | } | |
| 52 | 55 | ||
| @@ -62,7 +65,7 @@ func sigfillset(set *sigset_t) int32 { | |||
| 62 | 65 | return int32(call5(sigfillsetABI0, uintptr(unsafe.Pointer(set)), 0, 0, 0, 0)) | |
| 63 | 66 | } | |
| 64 | 67 | ||
| 65 | - | func nanosleep(ts *timespec, rem *timespec) int32 { | |
| 68 | + | func nanosleep(ts, rem *syscall.Timespec) int32 { | |
| 66 | 69 | return int32(call5(nanosleepABI0, uintptr(unsafe.Pointer(ts)), uintptr(unsafe.Pointer(rem)), 0, 0, 0)) | |
| 67 | 70 | } | |
| 68 | 71 | ||
| @@ -5,7 +5,10 @@ | |||||
| 5 | 5 | ||||
| 6 | package fakecgo | 6 | package fakecgo | ||
| 7 | 7 | ||||
| 8 | - | import "unsafe" | 8 | + | import ( |
| 9 | + | "syscall" | |||
| 10 | + | "unsafe" | |||
| 11 | + | ) | |||
| 9 | 12 | ||||
| 10 | // setg_trampoline calls setg with the G provided | 13 | // setg_trampoline calls setg with the G provided | ||
| 11 | func setg_trampoline(setg uintptr, G uintptr) | 14 | func setg_trampoline(setg uintptr, G uintptr) | ||
| @@ -26,7 +29,7 @@ func free(ptr unsafe.Pointer) { | |||||
| 26 | call5(freeABI0, uintptr(ptr), 0, 0, 0, 0) | 29 | call5(freeABI0, uintptr(ptr), 0, 0, 0, 0) | ||
| 27 | } | 30 | } | ||
| 28 | 31 | ||||
| 29 | - | func setenv(name *byte, value *byte, overwrite int32) int32 { | 32 | + | func setenv(name, value *byte, overwrite int32) int32 { |
| 30 | return int32(call5(setenvABI0, uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(value)), uintptr(overwrite), 0, 0)) | 33 | return int32(call5(setenvABI0, uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(value)), uintptr(overwrite), 0, 0)) | ||
| 31 | } | 34 | } | ||
| 32 | 35 | ||||
| @@ -46,7 +49,7 @@ func pthread_detach(thread pthread_t) int32 { | |||||
| 46 | return int32(call5(pthread_detachABI0, uintptr(thread), 0, 0, 0, 0)) | 49 | return int32(call5(pthread_detachABI0, uintptr(thread), 0, 0, 0, 0)) | ||
| 47 | } | 50 | } | ||
| 48 | 51 | ||||
| 49 | - | func pthread_sigmask(how sighow, ign *sigset_t, oset *sigset_t) int32 { | 52 | + | func pthread_sigmask(how sighow, ign, oset *sigset_t) int32 { |
| 50 | return int32(call5(pthread_sigmaskABI0, uintptr(how), uintptr(unsafe.Pointer(ign)), uintptr(unsafe.Pointer(oset)), 0, 0)) | 53 | return int32(call5(pthread_sigmaskABI0, uintptr(how), uintptr(unsafe.Pointer(ign)), uintptr(unsafe.Pointer(oset)), 0, 0)) | ||
| 51 | } | 54 | } | ||
| 52 | 55 | ||||
| @@ -62,7 +65,7 @@ func sigfillset(set *sigset_t) int32 { | |||||
| 62 | return int32(call5(sigfillsetABI0, uintptr(unsafe.Pointer(set)), 0, 0, 0, 0)) | 65 | return int32(call5(sigfillsetABI0, uintptr(unsafe.Pointer(set)), 0, 0, 0, 0)) | ||
| 63 | } | 66 | } | ||
| 64 | 67 | ||||
| 65 | - | func nanosleep(ts *timespec, rem *timespec) int32 { | 68 | + | func nanosleep(ts, rem *syscall.Timespec) int32 { |
| 66 | return int32(call5(nanosleepABI0, uintptr(unsafe.Pointer(ts)), uintptr(unsafe.Pointer(rem)), 0, 0, 0)) | 69 | return int32(call5(nanosleepABI0, uintptr(unsafe.Pointer(ts)), uintptr(unsafe.Pointer(rem)), 0, 0, 0)) | ||
| 67 | } | 70 | } | ||
| 68 | 71 | ||||
M internal/fakecgo/trampolines_amd64.s +0 -2
| @@ -2,7 +2,6 @@ | |||
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | 4 | //go:build darwin | |
| 5 | - | // +build darwin | |
| 6 | 5 | ||
| 7 | 6 | /* | |
| 8 | 7 | trampoline for emulating required C functions for cgo in go (see cgo.go) | |
| @@ -29,7 +28,6 @@ return value will be in AX | |||
| 29 | 28 | ||
| 30 | 29 | // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions. | |
| 31 | 30 | ||
| 32 | - | // TODO: put <> to make these private | |
| 33 | 31 | TEXT x_cgo_init_trampoline(SB), NOSPLIT, $16 | |
| 34 | 32 | MOVQ DI, 0(SP) | |
| 35 | 33 | MOVQ SI, 8(SP) | |
| @@ -2,7 +2,6 @@ | |||||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | //go:build darwin | 4 | //go:build darwin | ||
| 5 | - | // +build darwin | |||
| 6 | 5 | ||||
| 7 | /* | 6 | /* | ||
| 8 | trampoline for emulating required C functions for cgo in go (see cgo.go) | 7 | trampoline for emulating required C functions for cgo in go (see cgo.go) | ||
| @@ -29,7 +28,6 @@ return value will be in AX | |||||
| 29 | 28 | ||||
| 30 | // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions. | 29 | // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions. | ||
| 31 | 30 | ||||
| 32 | - | // TODO: put <> to make these private | |||
| 33 | TEXT x_cgo_init_trampoline(SB), NOSPLIT, $16 | 31 | TEXT x_cgo_init_trampoline(SB), NOSPLIT, $16 | ||
| 34 | MOVQ DI, 0(SP) | 32 | MOVQ DI, 0(SP) | ||
| 35 | MOVQ SI, 8(SP) | 33 | MOVQ SI, 8(SP) | ||
M internal/fakecgo/trampolines_arm64.s +0 -2
| @@ -2,14 +2,12 @@ | |||
| 2 | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | |
| 3 | 3 | ||
| 4 | 4 | //go:build darwin | |
| 5 | - | // +build darwin | |
| 6 | 5 | ||
| 7 | 6 | #include "textflag.h" | |
| 8 | 7 | #include "go_asm.h" | |
| 9 | 8 | ||
| 10 | 9 | // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions. | |
| 11 | 10 | ||
| 12 | - | // TODO: put <> to make these private | |
| 13 | 11 | TEXT x_cgo_init_trampoline(SB), NOSPLIT, $0-0 | |
| 14 | 12 | MOVD R0, 8(RSP) | |
| 15 | 13 | MOVD R1, 16(RSP) | |
| @@ -2,14 +2,12 @@ | |||||
| 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | 2 | // SPDX-FileCopyrightText: 2022 The Ebitengine Authors | ||
| 3 | 3 | ||||
| 4 | //go:build darwin | 4 | //go:build darwin | ||
| 5 | - | // +build darwin | |||
| 6 | 5 | ||||
| 7 | #include "textflag.h" | 6 | #include "textflag.h" | ||
| 8 | #include "go_asm.h" | 7 | #include "go_asm.h" | ||
| 9 | 8 | ||||
| 10 | // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions. | 9 | // these trampolines map the gcc ABI to Go ABI and then calls into the Go equivalent functions. | ||
| 11 | 10 | ||||
| 12 | - | // TODO: put <> to make these private | |||
| 13 | TEXT x_cgo_init_trampoline(SB), NOSPLIT, $0-0 | 11 | TEXT x_cgo_init_trampoline(SB), NOSPLIT, $0-0 | ||
| 14 | MOVD R0, 8(RSP) | 12 | MOVD R0, 8(RSP) | ||
| 15 | MOVD R1, 16(RSP) | 13 | MOVD R1, 16(RSP) | ||