github.com/aykevl/tinygo@v0.5.0/src/runtime/runtime_unix.go (about)

     1  // +build darwin linux,!avr,!cortexm
     2  
     3  package runtime
     4  
     5  import (
     6  	"unsafe"
     7  )
     8  
     9  //go:export putchar
    10  func _putchar(c int) int
    11  
    12  //go:export usleep
    13  func usleep(usec uint) int
    14  
    15  //go:export malloc
    16  func malloc(size uintptr) unsafe.Pointer
    17  
    18  //go:export abort
    19  func abort()
    20  
    21  //go:export clock_gettime
    22  func clock_gettime(clk_id uint, ts *timespec)
    23  
    24  const heapSize = 1 * 1024 * 1024 // 1MB to start
    25  
    26  var (
    27  	heapStart = uintptr(malloc(heapSize))
    28  	heapEnd   = heapStart + heapSize
    29  )
    30  
    31  type timeUnit int64
    32  
    33  const tickMicros = 1
    34  
    35  // TODO: Linux/amd64-specific
    36  type timespec struct {
    37  	tv_sec  int64
    38  	tv_nsec int64
    39  }
    40  
    41  const CLOCK_MONOTONIC_RAW = 4
    42  
    43  // Entry point for Go. Initialize all packages and call main.main().
    44  //go:export main
    45  func main() int {
    46  	// Run initializers of all packages.
    47  	initAll()
    48  
    49  	// Compiler-generated call to main.main().
    50  	callMain()
    51  
    52  	// For libc compatibility.
    53  	return 0
    54  }
    55  
    56  func putchar(c byte) {
    57  	_putchar(int(c))
    58  }
    59  
    60  const asyncScheduler = false
    61  
    62  func sleepTicks(d timeUnit) {
    63  	usleep(uint(d) / 1000)
    64  }
    65  
    66  // Return monotonic time in nanoseconds.
    67  //
    68  // TODO: noescape
    69  func monotime() uint64 {
    70  	ts := timespec{}
    71  	clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
    72  	return uint64(ts.tv_sec)*1000*1000*1000 + uint64(ts.tv_nsec)
    73  }
    74  
    75  func ticks() timeUnit {
    76  	return timeUnit(monotime())
    77  }