mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 03:27:48 +00:00
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.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+21
-12
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user