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.
Этот коммит содержится в:
Ayke van Laethem 2018-08-29 19:58:21 +02:00
родитель d620f0d188
коммит d4f5700625
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
3 изменённых файлов: 26 добавлений и 17 удалений

Просмотреть файл

@ -78,7 +78,8 @@ clean:
@rm -rf build @rm -rf build
fmt: fmt:
go fmt . ./src/examples/hello ./src/runtime @go fmt . ./src/examples/hello
@go fmt ./src/runtime/*.go
gen-device: gen-device-nrf gen-device: gen-device-nrf

Просмотреть файл

@ -2,14 +2,13 @@
package runtime package runtime
// #include "runtime_nrf.h"
import "C"
import ( import (
"device/arm" "device/arm"
"device/nrf" "device/nrf"
) )
func _Cfunc_rtc_sleep(ticks uint32)
const Microsecond = 1 const Microsecond = 1
func init() { func init() {
@ -51,7 +50,7 @@ func sleep(d Duration) {
for ticks64 != 0 { for ticks64 != 0 {
monotime() // update timestamp monotime() // update timestamp
ticks := uint32(ticks64) & 0x7fffff // 23 bits (to be on the safe side) 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) ticks64 -= Duration(ticks)
} }
} }

Просмотреть файл

@ -6,20 +6,28 @@ import (
"unsafe" "unsafe"
) )
// #include <stdio.h>
// #include <stdlib.h>
// #include <unistd.h>
// #include <time.h>
import "C"
const Microsecond = 1 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) { func putchar(c byte) {
C.putchar(C.int(c)) _Cfunc_putchar(int(c))
} }
func sleep(d Duration) { func sleep(d Duration) {
C.usleep(C.useconds_t(d)) _Cfunc_usleep(uint(d))
} }
// Return monotonic time in microseconds. // Return monotonic time in microseconds.
@ -27,17 +35,18 @@ func sleep(d Duration) {
// TODO: use nanoseconds? // TODO: use nanoseconds?
// TODO: noescape // TODO: noescape
func monotime() uint64 { func monotime() uint64 {
var ts C.struct_timespec ts := timespec{}
C.clock_gettime(C.CLOCK_MONOTONIC, &ts) _Cfunc_clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
return uint64(ts.tv_sec)*1000*1000 + uint64(ts.tv_nsec)/1000 return uint64(ts.tv_sec)*1000*1000 + uint64(ts.tv_nsec)/1000
} }
func abort() { func abort() {
C.abort() // panic() exits with exit code 2.
_Cfunc_exit(2)
} }
func alloc(size uintptr) unsafe.Pointer { func alloc(size uintptr) unsafe.Pointer {
buf := C.calloc(1, C.size_t(size)) buf := _Cfunc_calloc(1, size)
if buf == nil { if buf == nil {
panic("cannot allocate memory") panic("cannot allocate memory")
} }