From 9dc57402b5297a14a53f47658e72fcdab7e99f72 Mon Sep 17 00:00:00 2001 From: Aditya Agarwal Date: Sat, 31 Mar 2007 17:45:12 +0000 Subject: [PATCH] -- assert fixes for thrift concurrency lib Summary: - cannot assume that assert.h is defined Reviewed By: marc k Test Plan: everything compiles Notes: - need to reflect these changes in libfacebook/fbthread git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665073 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/src/concurrency/Monitor.cpp | 27 ++++++++++++------- lib/cpp/src/concurrency/Mutex.cpp | 6 +++-- .../src/concurrency/PosixThreadFactory.cpp | 22 ++++++++++----- lib/cpp/src/concurrency/TimerManager.cpp | 6 +++-- lib/cpp/src/concurrency/Util.h | 6 +++-- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/lib/cpp/src/concurrency/Monitor.cpp b/lib/cpp/src/concurrency/Monitor.cpp index ecd82ade..955fe9a3 100644 --- a/lib/cpp/src/concurrency/Monitor.cpp +++ b/lib/cpp/src/concurrency/Monitor.cpp @@ -32,9 +32,11 @@ class Monitor::Impl { condInitialized_(false) { try { - assert(pthread_mutex_init(&pthread_mutex_, NULL) == 0); + int ret = pthread_mutex_init(&pthread_mutex_, NULL); + assert(ret); mutexInitialized_ = true; - assert(pthread_cond_init(&pthread_cond_, NULL) == 0); + ret = pthread_cond_init(&pthread_cond_, NULL); + assert(ret); condInitialized_ = true; } catch(...) { cleanup(); @@ -50,9 +52,11 @@ class Monitor::Impl { void wait(long long timeout) const { // XXX Need to assert that caller owns mutex - assert(timeout >= 0LL); + bool bret = (timeout >= 0LL); + assert(bret); if (timeout == 0LL) { - assert(pthread_cond_wait(&pthread_cond_, &pthread_mutex_) == 0); + int iret = pthread_cond_wait(&pthread_cond_, &pthread_mutex_); + assert(iret); } else { struct timespec abstime; long long now = Util::currentTime(); @@ -61,19 +65,22 @@ class Monitor::Impl { &pthread_mutex_, &abstime); if (result == ETIMEDOUT) { - assert(Util::currentTime() >= (now + timeout)); + bret = (Util::currentTime() >= (now + timeout)); + assert(bret); } } } void notify() { // XXX Need to assert that caller owns mutex - assert(pthread_cond_signal(&pthread_cond_) == 0); + int iret = pthread_cond_signal(&pthread_cond_); + assert(iret); } void notifyAll() { // XXX Need to assert that caller owns mutex - assert(pthread_cond_broadcast(&pthread_cond_) == 0); + int iret = pthread_cond_broadcast(&pthread_cond_); + assert(iret); } private: @@ -81,12 +88,14 @@ class Monitor::Impl { void cleanup() { if (mutexInitialized_) { mutexInitialized_ = false; - assert(pthread_mutex_destroy(&pthread_mutex_) == 0); + int iret = pthread_mutex_destroy(&pthread_mutex_); + assert(iret); } if (condInitialized_) { condInitialized_ = false; - assert(pthread_cond_destroy(&pthread_cond_) == 0); + int iret = pthread_cond_destroy(&pthread_cond_); + assert(iret); } } diff --git a/lib/cpp/src/concurrency/Mutex.cpp b/lib/cpp/src/concurrency/Mutex.cpp index 3d74d756..0f650a19 100644 --- a/lib/cpp/src/concurrency/Mutex.cpp +++ b/lib/cpp/src/concurrency/Mutex.cpp @@ -20,14 +20,16 @@ namespace facebook { namespace thrift { namespace concurrency { class Mutex::impl { public: impl() : initialized_(false) { - assert(pthread_mutex_init(&pthread_mutex_, NULL) == 0); + int ret = pthread_mutex_init(&pthread_mutex_, NULL); + assert(ret); initialized_ = true; } ~impl() { if (initialized_) { initialized_ = false; - assert(pthread_mutex_destroy(&pthread_mutex_) == 0); + int ret = pthread_mutex_destroy(&pthread_mutex_); + assert(ret); } } diff --git a/lib/cpp/src/concurrency/PosixThreadFactory.cpp b/lib/cpp/src/concurrency/PosixThreadFactory.cpp index d5d0d067..a043418d 100644 --- a/lib/cpp/src/concurrency/PosixThreadFactory.cpp +++ b/lib/cpp/src/concurrency/PosixThreadFactory.cpp @@ -68,25 +68,32 @@ class PthreadThread: public Thread { state_ = starting; pthread_attr_t thread_attr; - assert(pthread_attr_init(&thread_attr) == 0); - assert(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE) == 0); + int ret = pthread_attr_init(&thread_attr); + assert(ret); + + ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); + assert(ret); // Set thread stack size - assert(pthread_attr_setstacksize(&thread_attr, MB * stackSize_) == 0); + ret = pthread_attr_setstacksize(&thread_attr, MB * stackSize_); + assert(ret); // Set thread policy - assert(pthread_attr_setschedpolicy(&thread_attr, policy_) == 0); + ret = pthread_attr_setschedpolicy(&thread_attr, policy_); + assert(ret); struct sched_param sched_param; sched_param.sched_priority = priority_; // Set thread priority - assert(pthread_attr_setschedparam(&thread_attr, &sched_param) == 0); + ret = pthread_attr_setschedparam(&thread_attr, &sched_param); + assert(ret); // Create reference shared_ptr* selfRef = new shared_ptr(); *selfRef = self_.lock(); - assert(pthread_create(&pthread_, &thread_attr, threadMain, (void*)selfRef) == 0); + ret = pthread_create(&pthread_, &thread_attr, threadMain, (void*)selfRef); + assert(ret); } void join() { @@ -101,7 +108,8 @@ class PthreadThread: public Thread { void runnable(shared_ptr value) { Thread::runnable(value); } void weakRef(shared_ptr self) { - assert(self.get() == this); + bool ret = (self.get() == this); + assert(ret); self_ = weak_ptr(self); } }; diff --git a/lib/cpp/src/concurrency/TimerManager.cpp b/lib/cpp/src/concurrency/TimerManager.cpp index 050885d4..6ab0e766 100644 --- a/lib/cpp/src/concurrency/TimerManager.cpp +++ b/lib/cpp/src/concurrency/TimerManager.cpp @@ -96,7 +96,8 @@ class TimerManager::Dispatcher: public Runnable { if (!manager_->taskMap_.empty()) { timeout = manager_->taskMap_.begin()->first - now; } - assert((timeout != 0 && manager_->taskCount_ > 0) || (timeout == 0 && manager_->taskCount_ == 0)); + bool ret = (timeout != 0 && manager_->taskCount_ > 0) || (timeout == 0 && manager_->taskCount_ == 0); + assert(ret); manager_->monitor_.wait(timeout); now = Util::currentTime(); } @@ -182,7 +183,8 @@ void TimerManager::start() { while (state_ == TimerManager::STARTING) { monitor_.wait(); } - assert(state_ != TimerManager::STARTING); + bool ret = (state_ != TimerManager::STARTING); + assert(ret); } } diff --git a/lib/cpp/src/concurrency/Util.h b/lib/cpp/src/concurrency/Util.h index 06b4d183..de4d5789 100644 --- a/lib/cpp/src/concurrency/Util.h +++ b/lib/cpp/src/concurrency/Util.h @@ -67,14 +67,16 @@ class Util { static const long long currentTime() { #if defined(HAVE_CLOCK_GETTIME) struct timespec now; - assert(clock_gettime(CLOCK_REALTIME, &now) == 0); + int ret = clock_gettime(CLOCK_REALTIME, &now); + assert(ret); return (now.tv_sec * MS_PER_S) + (now.tv_nsec / NS_PER_MS) + (now.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0) ; #elif defined(HAVE_GETTIMEOFDAY) struct timeval now; - assert(gettimeofday(&now, NULL) == 0); + int ret = gettimeofday(&now, NULL); + assert(ret); return (((long long)now.tv_sec) * MS_PER_S) + (now.tv_usec / MS_PER_S) + -- 2.17.1