github.com/moontrade/nogc@v0.1.7/sync/sync_cgo_unsafe.go (about) 1 //go:build !tinygo && (amd64 || arm64) 2 3 package synchronization 4 5 /* 6 #cgo LDFLAGS: -L. -lstdc++ 7 #cgo CXXFLAGS: -std=c++20 -I. 8 #include <stdlib.h> 9 #include <pthread.h> 10 #include "lock.h" 11 //#include "RWSpinLock.h" 12 13 typedef struct { 14 uint64_t nanos; 15 } moontrade_sleep_t; 16 17 void do_moontrade_sleep(size_t arg0, size_t arg1) { 18 moontrade_sleep_t* args = (moontrade_sleep_t*)(void*)arg0; 19 moontrade_lock_sleep(args->nanos); 20 } 21 22 */ 23 import "C" 24 import ( 25 "github.com/moontrade/nogc/unsafecgo" 26 "time" 27 "unsafe" 28 ) 29 30 // RWLock is a C++ based Read/Write spinlock tuned for performance. It performs slightly better than 31 // a Go sync.RWMutex, however both native code and Go code can use it. If staying in Go, use sync.RWMutex. 32 // It will lock a Go scheduler thread up so use it intelligently. 33 type RWLock struct { 34 ptr uintptr 35 } 36 37 type RWLockPointer struct { 38 RWLock 39 } 40 41 func NewLock() RWLockPointer { 42 var result uintptr 43 unsafecgo.NonBlocking((*byte)(C.moontrade_rwlock_new), uintptr(unsafe.Pointer(&result)), 0) 44 return RWLockPointer{RWLock{ptr: result}} 45 } 46 47 func (rw RWLockPointer) Close() error { 48 rw.Free() 49 return nil 50 } 51 52 func (rw *RWLockPointer) Free() { 53 args := struct { 54 lock uintptr 55 }{ 56 lock: uintptr(unsafe.Pointer(rw)), 57 } 58 ptr := uintptr(unsafe.Pointer(&args)) 59 unsafecgo.NonBlocking((*byte)(C.moontrade_rwlock_destroy), ptr, 0) 60 //C.moontrade_rwlock_destroy(unsafe.Pointer(rw)) 61 } 62 63 func Sleep(nanos time.Duration) { 64 C.moontrade_lock_sleep(C.uint64_t(nanos)) 65 } 66 67 func SleepUnsafe(nanos time.Duration) { 68 args := struct { 69 nanos uint64 70 }{ 71 nanos: uint64(nanos), 72 } 73 ptr := uintptr(unsafe.Pointer(&args)) 74 unsafecgo.NonBlocking((*byte)(C.do_moontrade_sleep), ptr, 0) 75 } 76 77 func (rw RWLock) Lock() { 78 C.moontrade_rwlock_lock(C.size_t(rw.ptr), 0) 79 } 80 81 func (rw RWLock) TryLock() bool { 82 var success uintptr 83 C.moontrade_rwlock_try_lock(C.size_t(rw.ptr), C.size_t(uintptr(unsafe.Pointer(&success)))) 84 return success != 0 85 } 86 87 func (rw RWLock) TryLockUnsafe() bool { 88 var success uintptr 89 unsafecgo.NonBlocking((*byte)(C.moontrade_rwlock_try_lock), rw.ptr, uintptr(unsafe.Pointer(&success))) 90 return success != 0 91 } 92 93 func (rw RWLock) LockUnsafe() { 94 unsafecgo.NonBlocking((*byte)(C.moontrade_rwlock_lock), rw.ptr, 0) 95 } 96 97 func (rw RWLock) Unlock() { 98 C.moontrade_rwlock_unlock(C.size_t(rw.ptr), 0) 99 } 100 101 func (rw RWLock) UnlockUnsafe() { 102 unsafecgo.NonBlocking((*byte)(C.moontrade_rwlock_unlock), rw.ptr, 0) 103 } 104 105 func (rw RWLock) LockShared() { 106 C.moontrade_rwlock_lock_shared(C.size_t(rw.ptr), 0) 107 } 108 109 func (rw RWLock) LockSharedUnsafe() { 110 unsafecgo.NonBlocking((*byte)(C.moontrade_rwlock_lock_shared), rw.ptr, 0) 111 } 112 113 func (rw RWLock) TryLockShared() bool { 114 var success uintptr 115 C.moontrade_rwlock_try_lock_shared(C.size_t(rw.ptr), C.size_t(uintptr(unsafe.Pointer(&success)))) 116 return success != 0 117 } 118 119 func (rw RWLock) TryLockSharedUnsafe() bool { 120 var success uintptr 121 unsafecgo.NonBlocking((*byte)(C.moontrade_rwlock_try_lock_shared), rw.ptr, uintptr(unsafe.Pointer(&success))) 122 return success != 0 123 } 124 125 func (rw RWLock) UnlockShared() { 126 C.moontrade_rwlock_unlock_shared(C.size_t(rw.ptr), 0) 127 } 128 129 func (rw RWLock) UnlockSharedUnsafe() { 130 unsafecgo.NonBlocking((*byte)(C.moontrade_rwlock_unlock_shared), rw.ptr, 0) 131 } 132 133 type RWTicketLock64 struct { 134 ptr uintptr 135 }