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
fmt:
go fmt . ./src/examples/hello ./src/runtime
@go fmt . ./src/examples/hello
@go fmt ./src/runtime/*.go
gen-device: gen-device-nrf

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

@ -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)
}
}

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

@ -6,20 +6,28 @@ import (
"unsafe"
)
// #include <stdio.h>
// #include <stdlib.h>
// #include <unistd.h>
// #include <time.h>
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")
}