Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save actsasflinn/352706 to your computer and use it in GitHub Desktop.
Save actsasflinn/352706 to your computer and use it in GitHub Desktop.
Kyoto Cabinet patch fix compatibility on Mac OSX Snow Leopard (Kyoto Cabinet 0.9.9)
--- kcthread.cc.orig 2010-04-01 22:59:17.000000000 -0400
+++ kcthread.cc 2010-04-01 23:04:41.000000000 -0400
@@ -178,12 +178,12 @@ Mutex::Mutex(Type type) {
break;
}
case ERRORCHECK: {
- if (::pthread_mutexattr_settype(&attr, ::PTHREAD_MUTEX_ERRORCHECK) != 0)
+ if (::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0)
throw std::runtime_error("pthread_mutexattr_settype");
break;
}
case RECURSIVE: {
- if (::pthread_mutexattr_settype(&attr, ::PTHREAD_MUTEX_RECURSIVE) != 0)
+ if (::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
throw std::runtime_error("pthread_mutexattr_settype");
break;
}
@@ -250,7 +250,11 @@ bool Mutex::lock_try(double sec) {
ts.tv_sec = std::time(NULL) + 1;
ts.tv_nsec = 0;
}
+#if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 200112L
int32_t ecode = ::pthread_mutex_timedlock(mutex, &ts);
+#else
+ int32_t ecode = ::pthread_mutex_trylock(mutex);
+#endif
if (ecode == 0) return true;
if (ecode != ETIMEDOUT) throw std::runtime_error("pthread_mutex_timedlock");
return false;
@@ -272,12 +276,15 @@ void Mutex::unlock() {
SpinLock::SpinLock() : opq_(NULL) {
#if defined(_KC_GCCATOMIC)
#else
- ::pthread_spinlock_t* spin = new ::pthread_spinlock_t;
- if (::pthread_spin_init(spin, ::PTHREAD_PROCESS_PRIVATE) != 0) {
- delete spin;
- throw std::runtime_error("pthread_spin_init");
+ ::pthread_mutex_t* mutex = new ::pthread_mutex_t;
+ ::pthread_mutexattr_t attr;
+ if (::pthread_mutexattr_init(&attr) != 0) throw std::runtime_error("pthread_mutexattr_init");
+ ::pthread_mutexattr_settype(&attr, PTHREAD_PROCESS_PRIVATE);
+ if (::pthread_mutex_init(mutex, &attr) != 0) {
+ delete mutex;
+ throw std::runtime_error("pthread_mutex_init");
}
- opq_ = (void*)spin;
+ opq_ = (void*)mutex;
#endif
}
@@ -288,9 +295,9 @@ SpinLock::SpinLock() : opq_(NULL) {
SpinLock::~SpinLock() {
#if defined(_KC_GCCATOMIC)
#else
- ::pthread_spinlock_t* spin = (::pthread_spinlock_t*)opq_;
- ::pthread_spin_destroy(spin);
- delete spin;
+ ::pthread_mutex_t* mutex = (::pthread_mutex_t*)opq_;
+ ::pthread_mutex_destroy(mutex);
+ delete mutex;
#endif
}
@@ -304,8 +311,8 @@ void SpinLock::lock() {
::sched_yield();
}
#else
- ::pthread_spinlock_t* spin = (::pthread_spinlock_t*)opq_;
- if (::pthread_spin_lock(spin) != 0) throw std::runtime_error("pthread_spin_lock");
+ ::pthread_mutex_t* mutex = (::pthread_mutex_t*)opq_;
+ if (::pthread_mutex_lock(mutex) != 0) throw std::runtime_error("pthread_mutex_lock");
#endif
}
@@ -317,10 +324,10 @@ bool SpinLock::lock_try() {
#if defined(_KC_GCCATOMIC)
return __sync_bool_compare_and_swap(&opq_, 0, 1);
#else
- ::pthread_spinlock_t* spin = (::pthread_spinlock_t*)opq_;
- int32_t ecode = ::pthread_spin_trylock(spin);
+ ::pthread_mutex_t* mutex = (::pthread_mutex_t*)opq_;
+ int32_t ecode = ::pthread_mutex_trylock(mutex);
if (ecode == 0) return true;
- if (ecode != EBUSY) throw std::runtime_error("pthread_spin_trylock");
+ if (ecode != EBUSY) throw std::runtime_error("pthread_mutex_trylock");
return false;
#endif
}
@@ -333,8 +340,8 @@ void SpinLock::unlock() {
#if defined(_KC_GCCATOMIC)
(void)__sync_lock_test_and_set(&opq_, 0);
#else
- ::pthread_spinlock_t* spin = (::pthread_spinlock_t*)opq_;
- if (::pthread_spin_unlock(spin) != 0) throw std::runtime_error("pthread_spin_unlock");
+ ::pthread_mutex_t* mutex = (::pthread_mutex_t*)opq_;
+ if (::pthread_mutex_unlock(mutex) != 0) throw std::runtime_error("pthread_mutex_unlock");
#endif
}
@@ -422,7 +429,7 @@ struct SpinRWLockCore {
int32_t wc; ///< count of writers
int32_t rc; ///< count of readers
#else
- ::pthread_spinlock_t sem; ///< semaphore
+ ::pthread_mutex_t sem; ///< semaphore
int32_t wc; ///< count of writers
int32_t rc; ///< count of readers
#endif
@@ -455,8 +462,11 @@ SpinRWLock::SpinRWLock() : opq_(NULL) {
opq_ = (void*)core;
#else
SpinRWLockCore* core = new SpinRWLockCore;
- if (::pthread_spin_init(&core->sem, ::PTHREAD_PROCESS_PRIVATE) != 0)
- throw std::runtime_error("pthread_spin_init");
+ ::pthread_mutexattr_t attr;
+ if (::pthread_mutexattr_init(&attr) != 0) throw std::runtime_error("pthread_mutexattr_init");
+ ::pthread_mutexattr_settype(&attr, PTHREAD_PROCESS_PRIVATE);
+ if (::pthread_mutex_init(&core->sem, &attr) != 0)
+ throw std::runtime_error("pthread_mutex_init");
core->wc = 0;
core->rc = 0;
opq_ = (void*)core;
@@ -473,7 +483,7 @@ SpinRWLock::~SpinRWLock() {
delete core;
#else
SpinRWLockCore* core = (SpinRWLockCore*)opq_;
- ::pthread_spin_destroy(&core->sem);
+ ::pthread_mutex_destroy(&core->sem);
delete core;
#endif
}
@@ -595,7 +605,7 @@ static void spinrwlocklock(SpinRWLockCor
::sched_yield();
}
#else
- if (::pthread_spin_lock(&core->sem) != 0) throw std::runtime_error("pthread_spin_lock");
+ if (::pthread_mutex_lock(&core->sem) != 0) throw std::runtime_error("pthread_mutex_lock");
#endif
}
@@ -607,7 +617,7 @@ static void spinrwlockunlock(SpinRWLockC
#if defined(_KC_GCCATOMIC)
(void)__sync_lock_test_and_set(&core->sem, 0);
#else
- if (::pthread_spin_unlock(&core->sem) != 0) throw std::runtime_error("pthread_spin_unlock");
+ if (::pthread_mutex_unlock(&core->sem) != 0) throw std::runtime_error("pthread_mutex_unlock");
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment