提交 82f4ae7f 编写于 作者: L Liu Yiqun

Add alternative implementation of SpinLock and ThreadBarrier,

 using the same implemetation as mac os.
上级 38fa74ed
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag) include(CheckCCompilerFlag)
include(CheckCXXSymbolExists) include(CheckCXXSymbolExists)
include(CheckTypeSize)
function(CheckCompilerCXX11Flag) function(CheckCompilerCXX11Flag)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
...@@ -83,6 +84,17 @@ if(NOT UINT64_MAX_EXISTS) ...@@ -83,6 +84,17 @@ if(NOT UINT64_MAX_EXISTS)
endif() endif()
endif() endif()
SET(CMAKE_EXTRA_INCLUDE_FILES "pthread.h")
CHECK_TYPE_SIZE(pthread_spinlock_t SPINLOCK_FOUND)
CHECK_TYPE_SIZE(pthread_barrier_t BARRIER_FOUND)
if(SPINLOCK_FOUND)
add_definitions(-DPADDLE_USE_PTHREAD_SPINLOCK)
endif(SPINLOCK_FOUND)
if(BARRIER_FOUND)
add_definitions(-DPADDLE_USE_PTHREAD_BARRIER)
endif(BARRIER_FOUND)
SET(CMAKE_EXTRA_INCLUDE_FILES "")
# Common flags. the compiler flag used for C/C++ sources whenever release or debug # Common flags. the compiler flag used for C/C++ sources whenever release or debug
# Do not care if this flag is support for gcc. # Do not care if this flag is support for gcc.
set(COMMON_FLAGS set(COMMON_FLAGS
......
...@@ -15,6 +15,7 @@ limitations under the License. */ ...@@ -15,6 +15,7 @@ limitations under the License. */
#include "paddle/utils/Locks.h" #include "paddle/utils/Locks.h"
#include <semaphore.h> #include <semaphore.h>
#include <unistd.h> #include <unistd.h>
#include "paddle/utils/Logging.h"
namespace paddle { namespace paddle {
class SemaphorePrivate { class SemaphorePrivate {
...@@ -26,7 +27,10 @@ Semaphore::Semaphore(int initValue) : m(new SemaphorePrivate()) { ...@@ -26,7 +27,10 @@ Semaphore::Semaphore(int initValue) : m(new SemaphorePrivate()) {
sem_init(&m->sem, 0, initValue); sem_init(&m->sem, 0, initValue);
} }
Semaphore::~Semaphore() { sem_destroy(&m->sem); } Semaphore::~Semaphore() {
sem_destroy(&m->sem);
delete m;
}
bool Semaphore::timeWait(struct timespec* ts) { bool Semaphore::timeWait(struct timespec* ts) {
return (0 == sem_timedwait(&m->sem, ts)); return (0 == sem_timedwait(&m->sem, ts));
...@@ -36,70 +40,101 @@ void Semaphore::wait() { sem_wait(&m->sem); } ...@@ -36,70 +40,101 @@ void Semaphore::wait() { sem_wait(&m->sem); }
void Semaphore::post() { sem_post(&m->sem); } void Semaphore::post() { sem_post(&m->sem); }
#ifdef PADDLE_USE_PTHREAD_SPINLOCK
class SpinLockPrivate { class SpinLockPrivate {
public: public:
inline SpinLockPrivate() { inline SpinLockPrivate() { pthread_spin_init(&lock_, 0); }
#ifndef __ANDROID__ inline ~SpinLockPrivate() { pthread_spin_destroy(&lock_); }
pthread_spin_init(&lock_, 0);
inline void lock() { pthread_spin_lock(&lock_); }
inline void unlock() { pthread_spin_unlock(&lock_); }
pthread_spinlock_t lock_;
char padding_[64 - sizeof(pthread_spinlock_t)];
};
#else #else
lock_ = 0;
#endif #include <atomic>
class SpinLockPrivate {
public:
inline void lock() {
while (lock_.test_and_set(std::memory_order_acquire)) {
} }
inline ~SpinLockPrivate() {
#ifndef __ANDROID__
pthread_spin_destroy(&lock_);
#endif
} }
#ifndef __ANDROID__ inline void unlock() { lock_.clear(std::memory_order_release); }
pthread_spinlock_t lock_;
#else std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
unsigned long lock_; char padding_[64 - sizeof(lock_)]; // Padding to cache line size
#endif
char padding_[64 - sizeof(lock_)];
}; };
SpinLock::SpinLock() : m(new SpinLockPrivate()) {} #endif
SpinLock::SpinLock() : m(new SpinLockPrivate()) {}
SpinLock::~SpinLock() { delete m; } SpinLock::~SpinLock() { delete m; }
void SpinLock::lock() { m->lock(); }
void SpinLock::unlock() { m->unlock(); }
void SpinLock::lock() { #ifdef PADDLE_USE_PTHREAD_BARRIER
#ifndef __ANDROID__
pthread_spin_lock(&m->lock_);
#endif
}
void SpinLock::unlock() {
#ifndef __ANDROID__
pthread_spin_unlock(&m->lock_);
#endif
}
class ThreadBarrierPrivate { class ThreadBarrierPrivate {
public: public:
#ifndef __ANDROID__
pthread_barrier_t barrier_; pthread_barrier_t barrier_;
#else
unsigned long barrier_; inline explicit ThreadBarrierPrivate(int count) {
#endif pthread_barrier_init(&barrier_, nullptr, count);
}
inline ~ThreadBarrierPrivate() { pthread_barrier_destroy(&barrier_); }
inline void wait() { pthread_barrier_wait(&barrier_); }
}; };
ThreadBarrier::ThreadBarrier(int count) : m(new ThreadBarrierPrivate()) { #else
#ifndef __ANDROID__
pthread_barrier_init(&m->barrier_, nullptr, count);
#endif
}
ThreadBarrier::~ThreadBarrier() { class ThreadBarrierPrivate {
#ifndef __ANDROID__ public:
pthread_barrier_destroy(&m->barrier_); pthread_mutex_t mutex_;
#endif pthread_cond_t cond_;
delete m; int count_;
} int tripCount_;
inline explicit ThreadBarrierPrivate(int cnt) : count_(0), tripCount_(cnt) {
CHECK_NE(cnt, 0);
CHECK_GE(pthread_mutex_init(&mutex_, 0), 0);
CHECK_GE(pthread_cond_init(&cond_, 0), 0);
}
inline ~ThreadBarrierPrivate() {
pthread_cond_destroy(&cond_);
pthread_mutex_destroy(&mutex_);
}
/**
* @brief wait
* @return true if the last wait
*/
inline bool wait() {
pthread_mutex_lock(&mutex_);
++count_;
if (count_ >= tripCount_) {
count_ = 0;
pthread_cond_broadcast(&cond_);
pthread_mutex_unlock(&mutex_);
return true;
} else {
pthread_cond_wait(&cond_, &mutex_);
pthread_mutex_unlock(&mutex_);
return false;
}
}
};
void ThreadBarrier::wait() {
#ifndef __ANDROID__
pthread_barrier_wait(&m->barrier_);
#endif #endif
}
ThreadBarrier::ThreadBarrier(int count) : m(new ThreadBarrierPrivate(count)) {}
ThreadBarrier::~ThreadBarrier() { delete m; }
void ThreadBarrier::wait() { m->wait(); }
} // namespace paddle } // namespace paddle
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册