github.com/moontrade/nogc@v0.1.7/unsafecgo/cgo/stub.go (about)

     1  package cgo
     2  
     3  /*
     4  #include <stdio.h>
     5  #include <time.h>
     6  #include <unistd.h>
     7  
     8  void do_usleep(size_t arg0, size_t arg1) {
     9  	nanosleep((const struct timespec[]){{0, 500000000L}}, NULL);
    10  }
    11  void unsafecgo_stub() {}
    12  
    13  typedef void unsafecgo_trampoline_handler(size_t arg0, size_t arg1);
    14  
    15  void unsafecgo_cgo_call(size_t fn, size_t arg0, size_t arg1) {
    16  	((unsafecgo_trampoline_handler*)fn)(arg0, arg1);
    17  }
    18  */
    19  import "C"
    20  import "unsafe"
    21  
    22  var (
    23  	Stub   = C.unsafecgo_stub
    24  	Usleep = C.do_usleep
    25  )
    26  
    27  func CGO() {
    28  	C.unsafecgo_stub()
    29  }
    30  
    31  func NonBlocking(fn *byte, arg0, arg1 uintptr) {
    32  	Blocking(fn, arg0, arg1)
    33  	//libcCall(unsafe.Pointer(fn), unsafe.Pointer(arg0))
    34  }
    35  
    36  func Blocking(fn *byte, arg0, arg1 uintptr) {
    37  	C.unsafecgo_cgo_call((C.size_t)(uintptr(unsafe.Pointer(fn))), (C.size_t)(arg0), (C.size_t)(arg1))
    38  }
    39  
    40  ////go:linkname libcCall runtime.libcCall
    41  //func libcCall(fn, arg unsafe.Pointer) int32
    42  
    43  func DoUsleep(useconds int64) {
    44  	C.do_usleep((C.size_t)(useconds), (C.size_t)(0))
    45  }