From d4f57006253f1a01f1569e6dc35ec727731afc41 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 29 Aug 2018 19:58:21 +0200 Subject: [PATCH] Remove use of CGo in the runtime CGo depends on syscall, which (in the standard library) depends on sync, which depends on the runtime. There are also other import cycles. To be able to use the syscall package from upstream, stop using CGo. --- Makefile | 3 ++- src/runtime/runtime_nrf.go | 7 +++---- src/runtime/runtime_unix.go | 33 +++++++++++++++++++++------------ 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index fb90f074..c5bd5c05 100644 --- a/Makefile +++ b/Makefile @@ -78,7 +78,8 @@ clean: @rm -rf build fmt: - go fmt . ./src/examples/hello ./src/runtime + @go fmt . ./src/examples/hello + @go fmt ./src/runtime/*.go gen-device: gen-device-nrf diff --git a/src/runtime/runtime_nrf.go b/src/runtime/runtime_nrf.go index 0250113a..93af1b1f 100644 --- a/src/runtime/runtime_nrf.go +++ b/src/runtime/runtime_nrf.go @@ -2,14 +2,13 @@ package runtime -// #include "runtime_nrf.h" -import "C" - import ( "device/arm" "device/nrf" ) +func _Cfunc_rtc_sleep(ticks uint32) + const Microsecond = 1 func init() { @@ -51,7 +50,7 @@ func sleep(d Duration) { for ticks64 != 0 { monotime() // update timestamp ticks := uint32(ticks64) & 0x7fffff // 23 bits (to be on the safe side) - C.rtc_sleep(C.uint32_t(ticks)) // TODO: not accurate (must be d / 30.5175...) + _Cfunc_rtc_sleep(ticks) // TODO: not accurate (must be d / 30.5175...) ticks64 -= Duration(ticks) } } diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index aedb197a..ca062df3 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -6,20 +6,28 @@ import ( "unsafe" ) -// #include -// #include -// #include -// #include -import "C" - const Microsecond = 1 +func _Cfunc_putchar(c int) int +func _Cfunc_usleep(usec uint) int +func _Cfunc_calloc(nmemb, size uintptr) unsafe.Pointer +func _Cfunc_exit(status int) +func _Cfunc_clock_gettime(clk_id uint, ts *timespec) + +// TODO: Linux/amd64-specific +type timespec struct { + tv_sec int64 + tv_nsec int64 +} + +const CLOCK_MONOTONIC_RAW = 4 + func putchar(c byte) { - C.putchar(C.int(c)) + _Cfunc_putchar(int(c)) } func sleep(d Duration) { - C.usleep(C.useconds_t(d)) + _Cfunc_usleep(uint(d)) } // Return monotonic time in microseconds. @@ -27,17 +35,18 @@ func sleep(d Duration) { // TODO: use nanoseconds? // TODO: noescape func monotime() uint64 { - var ts C.struct_timespec - C.clock_gettime(C.CLOCK_MONOTONIC, &ts) + ts := timespec{} + _Cfunc_clock_gettime(CLOCK_MONOTONIC_RAW, &ts) return uint64(ts.tv_sec)*1000*1000 + uint64(ts.tv_nsec)/1000 } func abort() { - C.abort() + // panic() exits with exit code 2. + _Cfunc_exit(2) } func alloc(size uintptr) unsafe.Pointer { - buf := C.calloc(1, C.size_t(size)) + buf := _Cfunc_calloc(1, size) if buf == nil { panic("cannot allocate memory") }