github.com/moontrade/nogc@v0.1.7/sync/lock.cc (about) 1 #ifndef MOONTRADE_LOCK_IMPL 2 #define MOONTRADE_LOCK_IMPL 3 4 #include <stdlib.h> 5 #include <string.h> 6 //#include <atomic> 7 #include "lock.h" 8 #include "RWSpinLock.h" 9 10 #include <thread> 11 #include <shared_mutex> 12 13 void moontrade_lock_sleep(uint64_t nanos) { 14 std::this_thread::sleep_for((std::chrono::nanoseconds) nanos); 15 } 16 17 //class InternalRWSpinLock { 18 // 19 //public: 20 // InternalRWSpinLock() { 21 // } 22 // void lock() { 23 // _lock.lock(); 24 // } 25 // void unlock() { 26 // _lock.unlock(); 27 // } 28 // void lock_shared() { 29 // _lock.lock_shared(); 30 // } 31 // void unlock_shared() { 32 // _lock.unlock_shared(); 33 // } 34 //private: 35 // RWSpinLock _lock; 36 //}; 37 38 uint64_t moontrade_rwlock_size() { 39 return (uint64_t)sizeof(RWSpinLock); 40 } 41 void moontrade_rwlock_new(size_t arg0, size_t arg1) { 42 *((size_t*)arg0) = (size_t)(void*)(new RWSpinLock()); 43 } 44 void moontrade_rwlock_try_lock(size_t lock, size_t arg1) { 45 *((bool*)(arg1)) = ((RWSpinLock*)(void*)(lock))->try_lock(); 46 } 47 void moontrade_rwlock_lock(size_t lock, size_t arg1) { 48 ((RWSpinLock*)(void*)(lock))->lock(); 49 } 50 void moontrade_rwlock_unlock(size_t lock, size_t arg1) { 51 ((RWSpinLock*)(void*)(lock))->unlock(); 52 } 53 void moontrade_rwlock_unlock_and_lock_shared(size_t lock, size_t arg1) { 54 ((RWSpinLock*)(void*)(lock))->unlock_and_lock_shared(); 55 } 56 void moontrade_rwlock_try_lock_shared(size_t lock, size_t arg1) { 57 *((bool*)(arg1)) = ((RWSpinLock*)(void*)(lock))->try_lock_shared(); 58 } 59 void moontrade_rwlock_lock_shared(size_t lock, size_t arg1) { 60 ((RWSpinLock*)(void*)(lock))->lock_shared(); 61 } 62 void moontrade_rwlock_unlock_shared(size_t lock, size_t arg1) { 63 ((RWSpinLock*)(void*)(lock))->unlock_shared(); 64 } 65 void moontrade_rwlock_destroy(size_t lock, size_t arg1) { 66 delete (RWSpinLock*)(void*)(lock); 67 } 68 69 uint64_t moontrade_rwticketlock_size() { 70 return (uint64_t)sizeof(RWTicketSpinLock64); 71 } 72 void moontrade_rwticketlock_new(size_t arg0, size_t arg1) { 73 *((size_t*)arg0) = (size_t)(void*)(new RWTicketSpinLock64()); 74 } 75 void moontrade_rwticketlock_lock(size_t lock, size_t arg1) { 76 ((RWTicketSpinLock64*)(void*)(lock))->lock(); 77 } 78 void moontrade_rwticketlock_lock_aggressive(size_t lock, size_t arg1) { 79 ((RWTicketSpinLock64*)(void*)(lock))->writeLockAggressive(); 80 } 81 void moontrade_rwticketlock_lock_nice(size_t lock, size_t arg1) { 82 ((RWTicketSpinLock64*)(void*)(lock))->writeLockNice(); 83 } 84 void moontrade_rwticketlock_unlock(size_t lock, size_t arg1) { 85 ((RWTicketSpinLock64*)(void*)(lock))->unlock(); 86 } 87 void moontrade_rwticketlock_unlock_and_lock_shared(size_t lock, size_t arg1) { 88 ((RWTicketSpinLock64*)(void*)(lock))->unlock_and_lock_shared(); 89 } 90 void moontrade_rwticketlock_try_lock(size_t lock, size_t arg1) { 91 *((bool*)(arg1)) = ((RWTicketSpinLock64*)(void*)(lock))->try_lock(); 92 } 93 void moontrade_rwticketlock_try_lock_shared(size_t lock, size_t arg1) { 94 *((bool*)(arg1)) = ((RWTicketSpinLock64*)(void*)(lock))->try_lock_shared(); 95 } 96 void moontrade_rwticketlock_lock_shared(size_t lock, size_t arg1) { 97 ((RWTicketSpinLock64*)(void*)(lock))->lock_shared(); 98 } 99 void moontrade_rwticketlock_unlock_shared(size_t lock, size_t arg1) { 100 ((RWTicketSpinLock64*)(void*)(lock))->unlock_shared(); 101 } 102 void moontrade_rwticketlock_destroy(size_t lock, size_t arg1) { 103 delete (RWTicketSpinLock64*)(void*)(lock); 104 } 105 106 #endif //MOONTRADE_LOCK_IMPL