summaryrefslogtreecommitdiff
path: root/plugins/Dbx_kyoto/src/kyotocabinet/kcthread.cc
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Dbx_kyoto/src/kyotocabinet/kcthread.cc')
-rw-r--r--plugins/Dbx_kyoto/src/kyotocabinet/kcthread.cc1151
1 files changed, 0 insertions, 1151 deletions
diff --git a/plugins/Dbx_kyoto/src/kyotocabinet/kcthread.cc b/plugins/Dbx_kyoto/src/kyotocabinet/kcthread.cc
index 82d1bb9f27..e67a6ae4c4 100644
--- a/plugins/Dbx_kyoto/src/kyotocabinet/kcthread.cc
+++ b/plugins/Dbx_kyoto/src/kyotocabinet/kcthread.cc
@@ -615,1149 +615,6 @@ void SlottedMutex::unlock_all() {
}
-/**
- * Default constructor.
- */
-SpinLock::SpinLock() : opq_(NULL) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
-#elif _KC_GCCATOMIC
- _assert_(true);
-#else
- _assert_(true);
- ::pthread_spinlock_t* spin = new ::pthread_spinlock_t;
- if (::pthread_spin_init(spin, PTHREAD_PROCESS_PRIVATE) != 0)
- throw std::runtime_error("pthread_spin_init");
- opq_ = (void*)spin;
-#endif
-}
-
-
-/**
- * Destructor.
- */
-SpinLock::~SpinLock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
-#elif _KC_GCCATOMIC
- _assert_(true);
-#else
- _assert_(true);
- ::pthread_spinlock_t* spin = (::pthread_spinlock_t*)opq_;
- ::pthread_spin_destroy(spin);
- delete spin;
-#endif
-}
-
-
-/**
- * Get the lock.
- */
-void SpinLock::lock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- uint32_t wcnt = 0;
- while (::InterlockedCompareExchange((LONG*)&opq_, 1, 0) != 0) {
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- }
-#elif _KC_GCCATOMIC
- _assert_(true);
- uint32_t wcnt = 0;
- while (!__sync_bool_compare_and_swap(&opq_, 0, 1)) {
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- }
-#else
- _assert_(true);
- ::pthread_spinlock_t* spin = (::pthread_spinlock_t*)opq_;
- if (::pthread_spin_lock(spin) != 0) throw std::runtime_error("pthread_spin_lock");
-#endif
-}
-
-
-/**
- * Try to get the lock.
- */
-bool SpinLock::lock_try() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- return ::InterlockedCompareExchange((LONG*)&opq_, 1, 0) == 0;
-#elif _KC_GCCATOMIC
- _assert_(true);
- return __sync_bool_compare_and_swap(&opq_, 0, 1);
-#else
- _assert_(true);
- ::pthread_spinlock_t* spin = (::pthread_spinlock_t*)opq_;
- int32_t ecode = ::pthread_spin_trylock(spin);
- if (ecode == 0) return true;
- if (ecode != EBUSY) throw std::runtime_error("pthread_spin_trylock");
- return false;
-#endif
-}
-
-
-/**
- * Release the lock.
- */
-void SpinLock::unlock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- ::InterlockedExchange((LONG*)&opq_, 0);
-#elif _KC_GCCATOMIC
- _assert_(true);
- __sync_lock_release(&opq_);
-#else
- _assert_(true);
- ::pthread_spinlock_t* spin = (::pthread_spinlock_t*)opq_;
- if (::pthread_spin_unlock(spin) != 0) throw std::runtime_error("pthread_spin_unlock");
-#endif
-}
-
-
-/**
- * SlottedSpinLock internal.
- */
-struct SlottedSpinLockCore {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || _KC_GCCATOMIC
- uint32_t* locks; ///< primitives
- size_t slotnum; ///< number of slots
-#else
- ::pthread_spinlock_t* spins; ///< primitives
- size_t slotnum; ///< number of slots
-#endif
-};
-
-
-/**
- * Constructor.
- */
-SlottedSpinLock::SlottedSpinLock(size_t slotnum) : opq_(NULL) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || _KC_GCCATOMIC
- _assert_(true);
- SlottedSpinLockCore* core = new SlottedSpinLockCore;
- uint32_t* locks = new uint32_t[slotnum];
- for (size_t i = 0; i < slotnum; i++) {
- locks[i] = 0;
- }
- core->locks = locks;
- core->slotnum = slotnum;
- opq_ = (void*)core;
-#else
- _assert_(true);
- SlottedSpinLockCore* core = new SlottedSpinLockCore;
- ::pthread_spinlock_t* spins = new ::pthread_spinlock_t[slotnum];
- for (size_t i = 0; i < slotnum; i++) {
- if (::pthread_spin_init(spins + i, PTHREAD_PROCESS_PRIVATE) != 0)
- throw std::runtime_error("pthread_spin_init");
- }
- core->spins = spins;
- core->slotnum = slotnum;
- opq_ = (void*)core;
-#endif
-}
-
-
-/**
- * Destructor.
- */
-SlottedSpinLock::~SlottedSpinLock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || _KC_GCCATOMIC
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- delete[] core->locks;
- delete core;
-#else
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- ::pthread_spinlock_t* spins = core->spins;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- ::pthread_spin_destroy(spins + i);
- }
- delete[] spins;
- delete core;
-#endif
-}
-
-
-/**
- * Get the lock of a slot.
- */
-void SlottedSpinLock::lock(size_t idx) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- uint32_t* lock = core->locks + idx;
- uint32_t wcnt = 0;
- while (::InterlockedCompareExchange((LONG*)lock, 1, 0) != 0) {
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- }
-#elif _KC_GCCATOMIC
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- uint32_t* lock = core->locks + idx;
- uint32_t wcnt = 0;
- while (!__sync_bool_compare_and_swap(lock, 0, 1)) {
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- }
-#else
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- if (::pthread_spin_lock(core->spins + idx) != 0)
- throw std::runtime_error("pthread_spin_lock");
-#endif
-}
-
-
-/**
- * Release the lock of a slot.
- */
-void SlottedSpinLock::unlock(size_t idx) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- uint32_t* lock = core->locks + idx;
- ::InterlockedExchange((LONG*)lock, 0);
-#elif _KC_GCCATOMIC
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- uint32_t* lock = core->locks + idx;
- __sync_lock_release(lock);
-#else
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- if (::pthread_spin_unlock(core->spins + idx) != 0)
- throw std::runtime_error("pthread_spin_unlock");
-#endif
-}
-
-
-/**
- * Get the locks of all slots.
- */
-void SlottedSpinLock::lock_all() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- uint32_t* locks = core->locks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- uint32_t* lock = locks + i;
- uint32_t wcnt = 0;
- while (::InterlockedCompareExchange((LONG*)lock, 1, 0) != 0) {
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- }
- }
-#elif _KC_GCCATOMIC
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- uint32_t* locks = core->locks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- uint32_t* lock = locks + i;
- uint32_t wcnt = 0;
- while (!__sync_bool_compare_and_swap(lock, 0, 1)) {
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- }
- }
-#else
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- ::pthread_spinlock_t* spins = core->spins;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- if (::pthread_spin_lock(spins + i) != 0)
- throw std::runtime_error("pthread_spin_lock");
- }
-#endif
-}
-
-
-/**
- * Release the locks of all slots.
- */
-void SlottedSpinLock::unlock_all() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- uint32_t* locks = core->locks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- uint32_t* lock = locks + i;
- ::InterlockedExchange((LONG*)lock, 0);
- }
-#elif _KC_GCCATOMIC
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- uint32_t* locks = core->locks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- uint32_t* lock = locks + i;
- __sync_lock_release(lock);
- }
-#else
- _assert_(true);
- SlottedSpinLockCore* core = (SlottedSpinLockCore*)opq_;
- ::pthread_spinlock_t* spins = core->spins;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- if (::pthread_spin_unlock(spins + i) != 0)
- throw std::runtime_error("pthread_spin_unlock");
- }
-#endif
-}
-
-
-/**
- * Default constructor.
- */
-RWLock::RWLock() : opq_(NULL) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SpinRWLock* rwlock = new SpinRWLock;
- opq_ = (void*)rwlock;
-#else
- _assert_(true);
- ::pthread_rwlock_t* rwlock = new ::pthread_rwlock_t;
- if (::pthread_rwlock_init(rwlock, NULL) != 0) throw std::runtime_error("pthread_rwlock_init");
- opq_ = (void*)rwlock;
-#endif
-}
-
-
-/**
- * Destructor.
- */
-RWLock::~RWLock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SpinRWLock* rwlock = (SpinRWLock*)opq_;
- delete rwlock;
-#else
- _assert_(true);
- ::pthread_rwlock_t* rwlock = (::pthread_rwlock_t*)opq_;
- ::pthread_rwlock_destroy(rwlock);
- delete rwlock;
-#endif
-}
-
-
-/**
- * Get the writer lock.
- */
-void RWLock::lock_writer() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SpinRWLock* rwlock = (SpinRWLock*)opq_;
- rwlock->lock_writer();
-#else
- _assert_(true);
- ::pthread_rwlock_t* rwlock = (::pthread_rwlock_t*)opq_;
- if (::pthread_rwlock_wrlock(rwlock) != 0) throw std::runtime_error("pthread_rwlock_lock");
-#endif
-}
-
-
-/**
- * Try to get the writer lock.
- */
-bool RWLock::lock_writer_try() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SpinRWLock* rwlock = (SpinRWLock*)opq_;
- return rwlock->lock_writer_try();
-#else
- _assert_(true);
- ::pthread_rwlock_t* rwlock = (::pthread_rwlock_t*)opq_;
- int32_t ecode = ::pthread_rwlock_trywrlock(rwlock);
- if (ecode == 0) return true;
- if (ecode != EBUSY) throw std::runtime_error("pthread_rwlock_trylock");
- return false;
-#endif
-}
-
-
-/**
- * Get a reader lock.
- */
-void RWLock::lock_reader() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SpinRWLock* rwlock = (SpinRWLock*)opq_;
- rwlock->lock_reader();
-#else
- _assert_(true);
- ::pthread_rwlock_t* rwlock = (::pthread_rwlock_t*)opq_;
- if (::pthread_rwlock_rdlock(rwlock) != 0) throw std::runtime_error("pthread_rwlock_lock");
-#endif
-}
-
-
-/**
- * Try to get a reader lock.
- */
-bool RWLock::lock_reader_try() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SpinRWLock* rwlock = (SpinRWLock*)opq_;
- return rwlock->lock_reader_try();
-#else
- _assert_(true);
- ::pthread_rwlock_t* rwlock = (::pthread_rwlock_t*)opq_;
- int32_t ecode = ::pthread_rwlock_tryrdlock(rwlock);
- if (ecode == 0) return true;
- if (ecode != EBUSY) throw std::runtime_error("pthread_rwlock_trylock");
- return false;
-#endif
-}
-
-
-/**
- * Release the lock.
- */
-void RWLock::unlock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SpinRWLock* rwlock = (SpinRWLock*)opq_;
- rwlock->unlock();
-#else
- _assert_(true);
- ::pthread_rwlock_t* rwlock = (::pthread_rwlock_t*)opq_;
- if (::pthread_rwlock_unlock(rwlock) != 0) throw std::runtime_error("pthread_rwlock_unlock");
-#endif
-}
-
-
-/**
- * SlottedRWLock internal.
- */
-struct SlottedRWLockCore {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- RWLock* rwlocks; ///< primitives
- size_t slotnum; ///< number of slots
-#else
- ::pthread_rwlock_t* rwlocks; ///< primitives
- size_t slotnum; ///< number of slots
-#endif
-};
-
-
-/**
- * Constructor.
- */
-SlottedRWLock::SlottedRWLock(size_t slotnum) : opq_(NULL) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedRWLockCore* core = new SlottedRWLockCore;
- RWLock* rwlocks = new RWLock[slotnum];
- core->rwlocks = rwlocks;
- core->slotnum = slotnum;
- opq_ = (void*)core;
-#else
- _assert_(true);
- SlottedRWLockCore* core = new SlottedRWLockCore;
- ::pthread_rwlock_t* rwlocks = new ::pthread_rwlock_t[slotnum];
- for (size_t i = 0; i < slotnum; i++) {
- if (::pthread_rwlock_init(rwlocks + i, NULL) != 0)
- throw std::runtime_error("pthread_rwlock_init");
- }
- core->rwlocks = rwlocks;
- core->slotnum = slotnum;
- opq_ = (void*)core;
-#endif
-}
-
-
-/**
- * Destructor.
- */
-SlottedRWLock::~SlottedRWLock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- delete[] core->rwlocks;
- delete core;
-#else
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- ::pthread_rwlock_t* rwlocks = core->rwlocks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- ::pthread_rwlock_destroy(rwlocks + i);
- }
- delete[] rwlocks;
- delete core;
-#endif
-}
-
-
-/**
- * Get the writer lock of a slot.
- */
-void SlottedRWLock::lock_writer(size_t idx) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- core->rwlocks[idx].lock_writer();
-#else
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- if (::pthread_rwlock_wrlock(core->rwlocks + idx) != 0)
- throw std::runtime_error("pthread_rwlock_wrlock");
-#endif
-}
-
-
-/**
- * Get the reader lock of a slot.
- */
-void SlottedRWLock::lock_reader(size_t idx) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- core->rwlocks[idx].lock_reader();
-#else
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- if (::pthread_rwlock_rdlock(core->rwlocks + idx) != 0)
- throw std::runtime_error("pthread_rwlock_rdlock");
-#endif
-}
-
-
-/**
- * Release the lock of a slot.
- */
-void SlottedRWLock::unlock(size_t idx) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- core->rwlocks[idx].unlock();
-#else
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- if (::pthread_rwlock_unlock(core->rwlocks + idx) != 0)
- throw std::runtime_error("pthread_rwlock_unlock");
-#endif
-}
-
-
-/**
- * Get the writer locks of all slots.
- */
-void SlottedRWLock::lock_writer_all() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- RWLock* rwlocks = core->rwlocks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- rwlocks[i].lock_writer();
- }
-#else
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- ::pthread_rwlock_t* rwlocks = core->rwlocks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- if (::pthread_rwlock_wrlock(rwlocks + i) != 0)
- throw std::runtime_error("pthread_rwlock_wrlock");
- }
-#endif
-}
-
-
-/**
- * Get the reader locks of all slots.
- */
-void SlottedRWLock::lock_reader_all() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- RWLock* rwlocks = core->rwlocks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- rwlocks[i].lock_reader();
- }
-#else
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- ::pthread_rwlock_t* rwlocks = core->rwlocks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- if (::pthread_rwlock_rdlock(rwlocks + i) != 0)
- throw std::runtime_error("pthread_rwlock_rdlock");
- }
-#endif
-}
-
-
-/**
- * Release the locks of all slots.
- */
-void SlottedRWLock::unlock_all() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- RWLock* rwlocks = core->rwlocks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- rwlocks[i].unlock();
- }
-#else
- _assert_(true);
- SlottedRWLockCore* core = (SlottedRWLockCore*)opq_;
- ::pthread_rwlock_t* rwlocks = core->rwlocks;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- if (::pthread_rwlock_unlock(rwlocks + i) != 0)
- throw std::runtime_error("pthread_rwlock_unlock");
- }
-#endif
-}
-
-
-/**
- * SpinRWLock internal.
- */
-struct SpinRWLockCore {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- LONG sem; ///< semaphore
- uint32_t cnt; ///< count of threads
-#elif _KC_GCCATOMIC
- int32_t sem; ///< semaphore
- uint32_t cnt; ///< count of threads
-#else
- ::pthread_spinlock_t sem; ///< semaphore
- uint32_t cnt; ///< count of threads
-#endif
-};
-
-
-/**
- * Lock the semephore of SpinRWLock.
- * @param core the internal fields.
- */
-static void spinrwlocklock(SpinRWLockCore* core);
-
-
-/**
- * Unlock the semephore of SpinRWLock.
- * @param core the internal fields.
- */
-static void spinrwlockunlock(SpinRWLockCore* core);
-
-
-/**
- * Default constructor.
- */
-SpinRWLock::SpinRWLock() : opq_(NULL) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || _KC_GCCATOMIC
- _assert_(true);
- SpinRWLockCore* core = new SpinRWLockCore;
- core->sem = 0;
- core->cnt = 0;
- opq_ = (void*)core;
-#else
- _assert_(true);
- SpinRWLockCore* core = new SpinRWLockCore;
- if (::pthread_spin_init(&core->sem, PTHREAD_PROCESS_PRIVATE) != 0)
- throw std::runtime_error("pthread_spin_init");
- core->cnt = 0;
- opq_ = (void*)core;
-#endif
-}
-
-
-/**
- * Destructor.
- */
-SpinRWLock::~SpinRWLock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || _KC_GCCATOMIC
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- delete core;
-#else
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- ::pthread_spin_destroy(&core->sem);
- delete core;
-#endif
-}
-
-
-/**
- * Get the writer lock.
- */
-void SpinRWLock::lock_writer() {
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- spinrwlocklock(core);
- uint32_t wcnt = 0;
- while (core->cnt > 0) {
- spinrwlockunlock(core);
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- spinrwlocklock(core);
- }
- core->cnt = INT32MAX;
- spinrwlockunlock(core);
-}
-
-
-/**
- * Try to get the writer lock.
- */
-bool SpinRWLock::lock_writer_try() {
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- spinrwlocklock(core);
- if (core->cnt > 0) {
- spinrwlockunlock(core);
- return false;
- }
- core->cnt = INT32MAX;
- spinrwlockunlock(core);
- return true;
-}
-
-
-/**
- * Get a reader lock.
- */
-void SpinRWLock::lock_reader() {
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- spinrwlocklock(core);
- uint32_t wcnt = 0;
- while (core->cnt >= (uint32_t)INT32MAX) {
- spinrwlockunlock(core);
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- spinrwlocklock(core);
- }
- core->cnt++;
- spinrwlockunlock(core);
-}
-
-
-/**
- * Try to get a reader lock.
- */
-bool SpinRWLock::lock_reader_try() {
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- spinrwlocklock(core);
- if (core->cnt >= (uint32_t)INT32MAX) {
- spinrwlockunlock(core);
- return false;
- }
- core->cnt++;
- spinrwlockunlock(core);
- return true;
-}
-
-
-/**
- * Release the lock.
- */
-void SpinRWLock::unlock() {
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- spinrwlocklock(core);
- if (core->cnt >= (uint32_t)INT32MAX) {
- core->cnt = 0;
- } else {
- core->cnt--;
- }
- spinrwlockunlock(core);
-}
-
-
-/**
- * Promote a reader lock to the writer lock.
- */
-bool SpinRWLock::promote() {
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- spinrwlocklock(core);
- if (core->cnt > 1) {
- spinrwlockunlock(core);
- return false;
- }
- core->cnt = INT32MAX;
- spinrwlockunlock(core);
- return true;
-}
-
-
-/**
- * Demote the writer lock to a reader lock.
- */
-void SpinRWLock::demote() {
- _assert_(true);
- SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- spinrwlocklock(core);
- core->cnt = 1;
- spinrwlockunlock(core);
-}
-
-
-/**
- * Lock the semephore of SpinRWLock.
- */
-static void spinrwlocklock(SpinRWLockCore* core) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(core);
- while (::InterlockedCompareExchange(&core->sem, 1, 0) != 0) {
- ::Sleep(0);
- }
-#elif _KC_GCCATOMIC
- _assert_(core);
- while (!__sync_bool_compare_and_swap(&core->sem, 0, 1)) {
- ::sched_yield();
- }
-#else
- _assert_(core);
- if (::pthread_spin_lock(&core->sem) != 0) throw std::runtime_error("pthread_spin_lock");
-#endif
-}
-
-
-/**
- * Unlock the semephore of SpinRWLock.
- */
-static void spinrwlockunlock(SpinRWLockCore* core) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(core);
- ::InterlockedExchange(&core->sem, 0);
-#elif _KC_GCCATOMIC
- _assert_(core);
- __sync_lock_release(&core->sem);
-#else
- _assert_(core);
- if (::pthread_spin_unlock(&core->sem) != 0) throw std::runtime_error("pthread_spin_unlock");
-#endif
-}
-
-
-/**
- * SlottedRWLock internal.
- */
-struct SlottedSpinRWLockCore {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- LONG sems[LOCKSEMNUM]; ///< semaphores
-#elif _KC_GCCATOMIC
- int32_t sems[LOCKSEMNUM]; ///< semaphores
-#else
- ::pthread_spinlock_t sems[LOCKSEMNUM]; ///< semaphores
-#endif
- uint32_t* cnts; ///< counts of threads
- size_t slotnum; ///< number of slots
-};
-
-
-/**
- * Lock the semephore of SlottedSpinRWLock.
- * @param core the internal fields.
- * @param idx the index of the semaphoe.
- */
-static void slottedspinrwlocklock(SlottedSpinRWLockCore* core, size_t idx);
-
-
-/**
- * Unlock the semephore of SlottedSpinRWLock.
- * @param core the internal fields.
- * @param idx the index of the semaphoe.
- */
-static void slottedspinrwlockunlock(SlottedSpinRWLockCore* core, size_t idx);
-
-
-/**
- * Constructor.
- */
-SlottedSpinRWLock::SlottedSpinRWLock(size_t slotnum) : opq_(NULL) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- SlottedSpinRWLockCore* core = new SlottedSpinRWLockCore;
- LONG* sems = core->sems;
- uint32_t* cnts = new uint32_t[slotnum];
- for (size_t i = 0; i < LOCKSEMNUM; i++) {
- sems[i] = 0;
- }
- for (size_t i = 0; i < slotnum; i++) {
- cnts[i] = 0;
- }
- core->cnts = cnts;
- core->slotnum = slotnum;
- opq_ = (void*)core;
-#elif _KC_GCCATOMIC
- SlottedSpinRWLockCore* core = new SlottedSpinRWLockCore;
- int32_t* sems = core->sems;
- uint32_t* cnts = new uint32_t[slotnum];
- for (size_t i = 0; i < LOCKSEMNUM; i++) {
- sems[i] = 0;
- }
- for (size_t i = 0; i < slotnum; i++) {
- cnts[i] = 0;
- }
- core->cnts = cnts;
- core->slotnum = slotnum;
- opq_ = (void*)core;
-#else
- _assert_(true);
- SlottedSpinRWLockCore* core = new SlottedSpinRWLockCore;
- ::pthread_spinlock_t* sems = core->sems;
- uint32_t* cnts = new uint32_t[slotnum];
- for (size_t i = 0; i < LOCKSEMNUM; i++) {
- if (::pthread_spin_init(sems + i, PTHREAD_PROCESS_PRIVATE) != 0)
- throw std::runtime_error("pthread_spin_init");
- }
- for (size_t i = 0; i < slotnum; i++) {
- cnts[i] = 0;
- }
- core->cnts = cnts;
- core->slotnum = slotnum;
- opq_ = (void*)core;
-#endif
-}
-
-
-/**
- * Destructor.
- */
-SlottedSpinRWLock::~SlottedSpinRWLock() {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || _KC_GCCATOMIC
- _assert_(true);
- SlottedSpinRWLockCore* core = (SlottedSpinRWLockCore*)opq_;
- delete[] core->cnts;
- delete core;
-#else
- _assert_(true);
- SlottedSpinRWLockCore* core = (SlottedSpinRWLockCore*)opq_;
- ::pthread_spinlock_t* sems = core->sems;
- for (size_t i = 0; i < LOCKSEMNUM; i++) {
- ::pthread_spin_destroy(sems + i);
- }
- delete[] core->cnts;
- delete core;
-#endif
-}
-
-
-/**
- * Get the writer lock of a slot.
- */
-void SlottedSpinRWLock::lock_writer(size_t idx) {
- _assert_(true);
- SlottedSpinRWLockCore* core = (SlottedSpinRWLockCore*)opq_;
- size_t semidx = idx % LOCKSEMNUM;
- slottedspinrwlocklock(core, semidx);
- uint32_t wcnt = 0;
- while (core->cnts[idx] > 0) {
- slottedspinrwlockunlock(core, semidx);
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- slottedspinrwlocklock(core, semidx);
- }
- core->cnts[idx] = INT32MAX;
- slottedspinrwlockunlock(core, semidx);
-}
-
-
-/**
- * Get the reader lock of a slot.
- */
-void SlottedSpinRWLock::lock_reader(size_t idx) {
- _assert_(true);
- SlottedSpinRWLockCore* core = (SlottedSpinRWLockCore*)opq_;
- size_t semidx = idx % LOCKSEMNUM;
- slottedspinrwlocklock(core, semidx);
- uint32_t wcnt = 0;
- while (core->cnts[idx] >= (uint32_t)INT32MAX) {
- slottedspinrwlockunlock(core, semidx);
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- slottedspinrwlocklock(core, semidx);
- }
- core->cnts[idx]++;
- slottedspinrwlockunlock(core, semidx);
-}
-
-
-/**
- * Release the lock of a slot.
- */
-void SlottedSpinRWLock::unlock(size_t idx) {
- _assert_(true);
- SlottedSpinRWLockCore* core = (SlottedSpinRWLockCore*)opq_;
- size_t semidx = idx % LOCKSEMNUM;
- slottedspinrwlocklock(core, semidx);
- if (core->cnts[idx] >= (uint32_t)INT32MAX) {
- core->cnts[idx] = 0;
- } else {
- core->cnts[idx]--;
- }
- slottedspinrwlockunlock(core, semidx);
-}
-
-
-/**
- * Get the writer locks of all slots.
- */
-void SlottedSpinRWLock::lock_writer_all() {
- _assert_(true);
- SlottedSpinRWLockCore* core = (SlottedSpinRWLockCore*)opq_;
- uint32_t* cnts = core->cnts;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- size_t semidx = i % LOCKSEMNUM;
- slottedspinrwlocklock(core, semidx);
- uint32_t wcnt = 0;
- while (cnts[i] > 0) {
- slottedspinrwlockunlock(core, semidx);
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- slottedspinrwlocklock(core, semidx);
- }
- cnts[i] = INT32MAX;
- slottedspinrwlockunlock(core, semidx);
- }
-}
-
-
-/**
- * Get the reader locks of all slots.
- */
-void SlottedSpinRWLock::lock_reader_all() {
- _assert_(true);
- SlottedSpinRWLockCore* core = (SlottedSpinRWLockCore*)opq_;
- uint32_t* cnts = core->cnts;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- size_t semidx = i % LOCKSEMNUM;
- slottedspinrwlocklock(core, semidx);
- uint32_t wcnt = 0;
- while (cnts[i] >= (uint32_t)INT32MAX) {
- slottedspinrwlockunlock(core, semidx);
- if (wcnt >= LOCKBUSYLOOP) {
- Thread::chill();
- } else {
- Thread::yield();
- wcnt++;
- }
- slottedspinrwlocklock(core, semidx);
- }
- cnts[i]++;
- slottedspinrwlockunlock(core, semidx);
- }
-}
-
-
-/**
- * Release the locks of all slots.
- */
-void SlottedSpinRWLock::unlock_all() {
- _assert_(true);
- SlottedSpinRWLockCore* core = (SlottedSpinRWLockCore*)opq_;
- uint32_t* cnts = core->cnts;
- size_t slotnum = core->slotnum;
- for (size_t i = 0; i < slotnum; i++) {
- size_t semidx = i % LOCKSEMNUM;
- slottedspinrwlocklock(core, semidx);
- if (cnts[i] >= (uint32_t)INT32MAX) {
- cnts[i] = 0;
- } else {
- cnts[i]--;
- }
- slottedspinrwlockunlock(core, semidx);
- }
-}
-
-
-/**
- * Lock the semephore of SlottedSpinRWLock.
- */
-static void slottedspinrwlocklock(SlottedSpinRWLockCore* core, size_t idx) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(core);
- while (::InterlockedCompareExchange(core->sems + idx, 1, 0) != 0) {
- ::Sleep(0);
- }
-#elif _KC_GCCATOMIC
- _assert_(core);
- while (!__sync_bool_compare_and_swap(core->sems + idx, 0, 1)) {
- ::sched_yield();
- }
-#else
- _assert_(core);
- if (::pthread_spin_lock(core->sems + idx) != 0) throw std::runtime_error("pthread_spin_lock");
-#endif
-}
-
-
-/**
- * Unlock the semephore of SlottedSpinRWLock.
- */
-static void slottedspinrwlockunlock(SlottedSpinRWLockCore* core, size_t idx) {
-#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
- _assert_(core);
- ::InterlockedExchange(core->sems + idx, 0);
-#elif _KC_GCCATOMIC
- _assert_(core);
- __sync_lock_release(core->sems + idx);
-#else
- _assert_(core);
- if (::pthread_spin_unlock(core->sems + idx) != 0)
- throw std::runtime_error("pthread_spin_unlock");
-#endif
-}
/**
@@ -2063,10 +920,8 @@ int64_t AtomicInt64::set(int64_t val) {
return oval;
#else
_assert_(true);
- lock_.lock();
int64_t oval = value_;
value_ = val;
- lock_.unlock();
return oval;
#endif
}
@@ -2086,10 +941,8 @@ int64_t AtomicInt64::add(int64_t val) {
return oval;
#else
_assert_(true);
- lock_.lock();
int64_t oval = value_;
value_ += val;
- lock_.unlock();
return oval;
#endif
}
@@ -2110,12 +963,10 @@ bool AtomicInt64::cas(int64_t oval, int64_t nval) {
#else
_assert_(true);
bool rv = false;
- lock_.lock();
if (value_ == oval) {
value_ = nval;
rv = true;
}
- lock_.unlock();
return rv;
#endif
}
@@ -2133,9 +984,7 @@ int64_t AtomicInt64::get() const {
return __sync_fetch_and_add((int64_t*)&value_, 0);
#else
_assert_(true);
- lock_.lock();
int64_t oval = value_;
- lock_.unlock();
return oval;
#endif
}