github.com/moontrade/nogc@v0.1.7/collections/tree/lock.cc (about)

     1  #include <stdlib.h>
     2  #include <string.h>
     3  //#include <atomic>
     4  #include "lock.h"
     5  #include "RWSpinLock.h"
     6  
     7  #include <thread>
     8  #include <shared_mutex>
     9  
    10  void moontrade_ordered_sleep(uint64_t nanos) {
    11      std::this_thread::sleep_for((std::chrono::nanoseconds) nanos);
    12  }
    13  
    14  //class InternalRWSpinLock {
    15  //
    16  //public:
    17  //    InternalRWSpinLock() {
    18  //    }
    19  //    void lock() {
    20  //        _lock.lock();
    21  //    }
    22  //    void unlock() {
    23  //        _lock.unlock();
    24  //    }
    25  //    void lock_shared() {
    26  //        _lock.lock_shared();
    27  //    }
    28  //    void unlock_shared() {
    29  //        _lock.unlock_shared();
    30  //    }
    31  //private:
    32  //    RWSpinLock _lock;
    33  //};
    34  
    35  uint64_t moontrade_ordered_rwlock_size() {
    36      return (uint64_t)sizeof(RWSpinLock);
    37  }
    38  void moontrade_ordered_rwlock_new(size_t arg0, size_t arg1) {
    39      *((size_t*)arg0) = (size_t)(void*)(new RWSpinLock());
    40  }
    41  void moontrade_ordered_rwlock_try_lock(size_t lock, size_t arg1) {
    42      *((bool*)(arg1)) = ((RWSpinLock*)(void*)(lock))->try_lock();
    43  }
    44  void moontrade_ordered_rwlock_lock(size_t lock, size_t arg1) {
    45      ((RWSpinLock*)(lock))->lock();
    46  }
    47  void moontrade_ordered_rwlock_unlock(size_t lock, size_t arg1) {
    48      ((RWSpinLock*)(lock))->unlock();
    49  }
    50  void moontrade_ordered_rwlock_unlock_and_lock_shared(size_t lock, size_t arg1) {
    51      ((RWSpinLock*)(void*)(lock))->unlock_and_lock_shared();
    52  }
    53  void moontrade_ordered_rwlock_try_lock_shared(size_t lock, size_t arg1) {
    54      *((bool*)(arg1)) = ((RWSpinLock*)(void*)(lock))->try_lock_shared();
    55  }
    56  void moontrade_ordered_rwlock_lock_shared(size_t lock, size_t arg1) {
    57      ((RWSpinLock*)(void*)(lock))->lock_shared();
    58  }
    59  void moontrade_ordered_rwlock_unlock_shared(size_t lock, size_t arg1) {
    60      ((RWSpinLock*)(void*)(lock))->unlock_shared();
    61  }
    62  void moontrade_ordered_rwlock_destroy(size_t lock, size_t arg1) {
    63      delete (RWSpinLock*)(void*)(lock);
    64  }
    65  
    66  uint64_t moontrade_ordered_rwticketlock_size() {
    67      return (uint64_t)sizeof(RWTicketSpinLock64);
    68  }
    69  void moontrade_ordered_rwticketlock_new(size_t arg0, size_t arg1) {
    70      *((size_t*)arg0) = (size_t)(void*)(new RWTicketSpinLock64());
    71  }
    72  void moontrade_ordered_rwticketlock_lock(size_t lock, size_t arg1) {
    73      ((RWTicketSpinLock64*)(void*)(lock))->lock();
    74  }
    75  void moontrade_ordered_rwticketlock_lock_aggressive(size_t lock, size_t arg1) {
    76      ((RWTicketSpinLock64*)(void*)(lock))->writeLockAggressive();
    77  }
    78  void moontrade_ordered_rwticketlock_lock_nice(size_t lock, size_t arg1) {
    79      ((RWTicketSpinLock64*)(void*)(lock))->writeLockNice();
    80  }
    81  void moontrade_ordered_rwticketlock_unlock(size_t lock, size_t arg1) {
    82      ((RWTicketSpinLock64*)(void*)(lock))->unlock();
    83  }
    84  void moontrade_ordered_rwticketlock_unlock_and_lock_shared(size_t lock, size_t arg1) {
    85      ((RWTicketSpinLock64*)(void*)(lock))->unlock_and_lock_shared();
    86  }
    87  void moontrade_ordered_rwticketlock_try_lock(size_t lock, size_t arg1) {
    88      *((bool*)(arg1)) = ((RWTicketSpinLock64*)(void*)(lock))->try_lock();
    89  }
    90  void moontrade_ordered_rwticketlock_try_lock_shared(size_t lock, size_t arg1) {
    91      *((bool*)(arg1)) = ((RWTicketSpinLock64*)(void*)(lock))->try_lock_shared();
    92  }
    93  void moontrade_ordered_rwticketlock_lock_shared(size_t lock, size_t arg1) {
    94      ((RWTicketSpinLock64*)(void*)(lock))->lock_shared();
    95  }
    96  void moontrade_ordered_rwticketlock_unlock_shared(size_t lock, size_t arg1) {
    97      ((RWTicketSpinLock64*)(void*)(lock))->unlock_shared();
    98  }
    99  void moontrade_ordered_rwticketlock_destroy(size_t lock, size_t arg1) {
   100      delete (RWTicketSpinLock64*)(void*)(lock);
   101  }
   102  
   103  static void rwlock_lock(void* lock) {
   104      ((RWSpinLock*)(lock))->lock();
   105  }
   106  static void rwlock_unlock(void* lock) {
   107      ((RWSpinLock*)(lock))->unlock();
   108  }
   109  static void rwlock_lock_shared(void* lock) {
   110      ((RWSpinLock*)(lock))->lock_shared();
   111  }
   112  static void rwlock_unlock_shared(void* lock) {
   113      ((RWSpinLock*)(lock))->unlock_shared();
   114  }
   115  
   116  static inline void rwticketlock_lock(void* lock) {
   117      ((RWTicketSpinLock64*)(lock))->lock();
   118  }
   119  static inline void rwticketlock_unlock(void* lock) {
   120      ((RWTicketSpinLock64*)(lock))->unlock();
   121  }
   122  static inline void rwticketlock_lock_shared(void* lock) {
   123      ((RWTicketSpinLock64*)(lock))->lock_shared();
   124  }
   125  static inline void rwticketlock_unlock_shared(void* lock) {
   126      ((RWTicketSpinLock64*)(lock))->unlock_shared();
   127  }
   128  
   129  const struct moontrade_ordered_lock_api moontrade_ordered_lock_unfair_api = {
   130      .fair = 0,
   131      .lock = rwlock_lock,
   132      .unlock = rwlock_unlock,
   133      .lock_shared = rwlock_lock_shared,
   134      .unlock_shared = rwlock_unlock_shared,
   135  };
   136  
   137  const struct moontrade_ordered_lock_api moontrade_ordered_lock_fair_api = {
   138      .fair = 1,
   139      .lock = rwticketlock_lock,
   140      .unlock = rwticketlock_unlock,
   141      .lock_shared = rwticketlock_lock_shared,
   142      .unlock_shared = rwticketlock_unlock_shared,
   143  };