diff options
author | cyy <cyyever@outlook.com> | 2019-01-05 13:45:07 +0800 |
---|---|---|
committer | James E. King III <jking@apache.org> | 2019-01-07 08:11:37 -0500 |
commit | c109e019790a87ef1f874dfac3482ac45a57d3ab (patch) | |
tree | 5c010cc93d0c3ff0cd6c16e93595b69b46efe194 /lib/cpp/src/thrift/concurrency | |
parent | 83b65f06fa380ed94669bdb461344f4f6b591191 (diff) | |
download | thrift-c109e019790a87ef1f874dfac3482ac45a57d3ab.tar.gz |
remove boost::thread and boost::mutex code
Diffstat (limited to 'lib/cpp/src/thrift/concurrency')
-rw-r--r-- | lib/cpp/src/thrift/concurrency/BoostMonitor.cpp | 214 | ||||
-rw-r--r-- | lib/cpp/src/thrift/concurrency/BoostMutex.cpp | 73 | ||||
-rw-r--r-- | lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp | 162 | ||||
-rw-r--r-- | lib/cpp/src/thrift/concurrency/BoostThreadFactory.h | 63 | ||||
-rw-r--r-- | lib/cpp/src/thrift/concurrency/PlatformThreadFactory.h | 8 | ||||
-rw-r--r-- | lib/cpp/src/thrift/concurrency/Thread.h | 11 |
6 files changed, 4 insertions, 527 deletions
diff --git a/lib/cpp/src/thrift/concurrency/BoostMonitor.cpp b/lib/cpp/src/thrift/concurrency/BoostMonitor.cpp deleted file mode 100644 index ebfa0b9dd..000000000 --- a/lib/cpp/src/thrift/concurrency/BoostMonitor.cpp +++ /dev/null @@ -1,214 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include <thrift/thrift-config.h> - -#include <thrift/concurrency/Monitor.h> -#include <thrift/concurrency/Exception.h> -#include <thrift/concurrency/Util.h> -#include <thrift/transport/PlatformSocket.h> -#include <thrift/stdcxx.h> - -#include <assert.h> -#include <boost/thread.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> - -namespace apache { -namespace thrift { -namespace concurrency { - -/** - * Monitor implementation using the boost thread library - * - * @version $Id:$ - */ -class Monitor::Impl : public boost::condition_variable_any { - -public: - Impl() : ownedMutex_(new Mutex()), mutex_(NULL) { init(ownedMutex_.get()); } - - Impl(Mutex* mutex) : mutex_(NULL) { init(mutex); } - - Impl(Monitor* monitor) : mutex_(NULL) { init(&(monitor->mutex())); } - - Mutex& mutex() { return *mutex_; } - void lock() { mutex().lock(); } - void unlock() { mutex().unlock(); } - - /** - * Exception-throwing version of waitForTimeRelative(), called simply - * wait(int64) for historical reasons. Timeout is in milliseconds. - * - * If the condition occurs, this function returns cleanly; on timeout or - * error an exception is thrown. - */ - void wait(int64_t timeout_ms) { - int result = waitForTimeRelative(timeout_ms); - if (result == THRIFT_ETIMEDOUT) { - throw TimedOutException(); - } else if (result != 0) { - throw TException("Monitor::wait() failed"); - } - } - - /** - * Waits until the specified timeout in milliseconds for the condition to - * occur, or waits forever if timeout_ms == 0. - * - * Returns 0 if condition occurs, THRIFT_ETIMEDOUT on timeout, or an error code. - */ - int waitForTimeRelative(int64_t timeout_ms) { - if (timeout_ms == 0LL) { - return waitForever(); - } - - assert(mutex_); - boost::timed_mutex* mutexImpl - = reinterpret_cast<boost::timed_mutex*>(mutex_->getUnderlyingImpl()); - assert(mutexImpl); - - boost::timed_mutex::scoped_lock lock(*mutexImpl, boost::adopt_lock); - int res - = timed_wait(lock, boost::get_system_time() + boost::posix_time::milliseconds(timeout_ms)) - ? 0 - : THRIFT_ETIMEDOUT; - lock.release(); - return res; - } - - /** - * Waits until the absolute time specified using struct THRIFT_TIMESPEC. - * Returns 0 if condition occurs, THRIFT_ETIMEDOUT on timeout, or an error code. - */ - int waitForTime(const THRIFT_TIMESPEC* abstime) { - struct timeval temp; - temp.tv_sec = static_cast<long>(abstime->tv_sec); - temp.tv_usec = static_cast<long>(abstime->tv_nsec) / 1000; - return waitForTime(&temp); - } - - /** - * Waits until the absolute time specified using struct timeval. - * Returns 0 if condition occurs, THRIFT_ETIMEDOUT on timeout, or an error code. - */ - int waitForTime(const struct timeval* abstime) { - assert(mutex_); - boost::timed_mutex* mutexImpl = static_cast<boost::timed_mutex*>(mutex_->getUnderlyingImpl()); - assert(mutexImpl); - - struct timeval currenttime; - Util::toTimeval(currenttime, Util::currentTime()); - - long tv_sec = static_cast<long>(abstime->tv_sec - currenttime.tv_sec); - long tv_usec = static_cast<long>(abstime->tv_usec - currenttime.tv_usec); - if (tv_sec < 0) - tv_sec = 0; - if (tv_usec < 0) - tv_usec = 0; - - boost::timed_mutex::scoped_lock lock(*mutexImpl, boost::adopt_lock); - int res = timed_wait(lock, - boost::get_system_time() + boost::posix_time::seconds(tv_sec) - + boost::posix_time::microseconds(tv_usec)) - ? 0 - : THRIFT_ETIMEDOUT; - lock.release(); - return res; - } - - /** - * Waits forever until the condition occurs. - * Returns 0 if condition occurs, or an error code otherwise. - */ - int waitForever() { - assert(mutex_); - boost::timed_mutex* mutexImpl - = reinterpret_cast<boost::timed_mutex*>(mutex_->getUnderlyingImpl()); - assert(mutexImpl); - - boost::timed_mutex::scoped_lock lock(*mutexImpl, boost::adopt_lock); - ((boost::condition_variable_any*)this)->wait(lock); - lock.release(); - return 0; - } - - void notify() { notify_one(); } - - void notifyAll() { notify_all(); } - -private: - void init(Mutex* mutex) { mutex_ = mutex; } - - stdcxx::scoped_ptr<Mutex> ownedMutex_; - Mutex* mutex_; -}; - -Monitor::Monitor() : impl_(new Monitor::Impl()) { -} -Monitor::Monitor(Mutex* mutex) : impl_(new Monitor::Impl(mutex)) { -} -Monitor::Monitor(Monitor* monitor) : impl_(new Monitor::Impl(monitor)) { -} - -Monitor::~Monitor() { - delete impl_; -} - -Mutex& Monitor::mutex() const { - return const_cast<Monitor::Impl*>(impl_)->mutex(); -} - -void Monitor::lock() const { - const_cast<Monitor::Impl*>(impl_)->lock(); -} - -void Monitor::unlock() const { - const_cast<Monitor::Impl*>(impl_)->unlock(); -} - -void Monitor::wait(int64_t timeout) const { - const_cast<Monitor::Impl*>(impl_)->wait(timeout); -} - -int Monitor::waitForTime(const THRIFT_TIMESPEC* abstime) const { - return const_cast<Monitor::Impl*>(impl_)->waitForTime(abstime); -} - -int Monitor::waitForTime(const timeval* abstime) const { - return const_cast<Monitor::Impl*>(impl_)->waitForTime(abstime); -} - -int Monitor::waitForTimeRelative(int64_t timeout_ms) const { - return const_cast<Monitor::Impl*>(impl_)->waitForTimeRelative(timeout_ms); -} - -int Monitor::waitForever() const { - return const_cast<Monitor::Impl*>(impl_)->waitForever(); -} - -void Monitor::notify() const { - const_cast<Monitor::Impl*>(impl_)->notify(); -} - -void Monitor::notifyAll() const { - const_cast<Monitor::Impl*>(impl_)->notifyAll(); -} -} -} -} // apache::thrift::concurrency diff --git a/lib/cpp/src/thrift/concurrency/BoostMutex.cpp b/lib/cpp/src/thrift/concurrency/BoostMutex.cpp deleted file mode 100644 index 4e556df17..000000000 --- a/lib/cpp/src/thrift/concurrency/BoostMutex.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include <thrift/thrift-config.h> - -#include <thrift/concurrency/Mutex.h> -#include <thrift/concurrency/Util.h> -#include <thrift/Thrift.h> - -#include <cassert> -#include <boost/thread.hpp> -#include <boost/thread/mutex.hpp> -#include <boost/date_time/posix_time/posix_time.hpp> - -namespace apache { -namespace thrift { -namespace concurrency { - -/** - * Implementation of Mutex class using boost::timed_mutex - * - * Methods throw boost::lock_error on error. - * - * @version $Id:$ - */ -class Mutex::impl : public boost::timed_mutex {}; - -Mutex::Mutex(Initializer init) : impl_(new Mutex::impl()) { - THRIFT_UNUSED_VARIABLE(init); -} - -void* Mutex::getUnderlyingImpl() const { - return impl_.get(); -} - -void Mutex::lock() const { - impl_->lock(); -} - -bool Mutex::trylock() const { - return impl_->try_lock(); -} - -bool Mutex::timedlock(int64_t ms) const { - return impl_->timed_lock(boost::get_system_time() + boost::posix_time::milliseconds(ms)); -} - -void Mutex::unlock() const { - impl_->unlock(); -} - -void Mutex::DEFAULT_INITIALIZER(void* arg) { - THRIFT_UNUSED_VARIABLE(arg); -} -} -} -} // apache::thrift::concurrency diff --git a/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp b/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp deleted file mode 100644 index d7d8d54e9..000000000 --- a/lib/cpp/src/thrift/concurrency/BoostThreadFactory.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include <thrift/thrift-config.h> - -#if USE_BOOST_THREAD - -#include <thrift/concurrency/BoostThreadFactory.h> -#include <thrift/concurrency/Exception.h> -#include <thrift/stdcxx.h> -#include <cassert> - -#include <boost/thread.hpp> - -namespace apache { -namespace thrift { - -using stdcxx::bind; -using stdcxx::scoped_ptr; -using stdcxx::shared_ptr; -using stdcxx::weak_ptr; - -namespace concurrency { - -/** - * The boost thread class. - * - * @version $Id:$ - */ -class BoostThread : public Thread { -public: - enum STATE { uninitialized, starting, started, stopping, stopped }; - - static void* threadMain(void* arg); - -private: - scoped_ptr<boost::thread> thread_; - Monitor monitor_; - STATE state_; - weak_ptr<BoostThread> self_; - bool detached_; - -public: - BoostThread(bool detached, shared_ptr<Runnable> runnable) - : state_(uninitialized), detached_(detached) { - this->Thread::runnable(runnable); - } - - ~BoostThread() { - if (!detached_ && thread_->joinable()) { - try { - join(); - } catch (...) { - // We're really hosed. - } - } - } - - STATE getState() const - { - Synchronized sync(monitor_); - return state_; - } - - void setState(STATE newState) - { - Synchronized sync(monitor_); - state_ = newState; - - // unblock start() with the knowledge that the thread has actually - // started running, which avoids a race in detached threads. - if (newState == started) { - monitor_.notify(); - } - } - - void start() { - // Create reference - shared_ptr<BoostThread>* selfRef = new shared_ptr<BoostThread>(); - *selfRef = self_.lock(); - - setState(starting); - - Synchronized sync(monitor_); - - thread_.reset(new boost::thread(bind(threadMain, (void*)selfRef))); - - if (detached_) - thread_->detach(); - - // Wait for the thread to start and get far enough to grab everything - // that it needs from the calling context, thus absolving the caller - // from being required to hold on to runnable indefinitely. - monitor_.wait(); - } - - void join() { - if (!detached_ && getState() != uninitialized) { - thread_->join(); - } - } - - Thread::id_t getId() { return thread_.get() ? thread_->get_id() : boost::thread::id(); } - - shared_ptr<Runnable> runnable() const { return Thread::runnable(); } - - void runnable(shared_ptr<Runnable> value) { Thread::runnable(value); } - - void weakRef(shared_ptr<BoostThread> self) { - assert(self.get() == this); - self_ = weak_ptr<BoostThread>(self); - } -}; - -void* BoostThread::threadMain(void* arg) { - shared_ptr<BoostThread> thread = *(shared_ptr<BoostThread>*)arg; - delete reinterpret_cast<shared_ptr<BoostThread>*>(arg); - - thread->setState(started); - thread->runnable()->run(); - - if (thread->getState() != stopping && thread->getState() != stopped) { - thread->setState(stopping); - } - return (void*)0; -} - -BoostThreadFactory::BoostThreadFactory(bool detached) - : ThreadFactory(detached) { -} - -shared_ptr<Thread> BoostThreadFactory::newThread(shared_ptr<Runnable> runnable) const { - shared_ptr<BoostThread> result = shared_ptr<BoostThread>(new BoostThread(isDetached(), runnable)); - result->weakRef(result); - runnable->thread(result); - return result; -} - -Thread::id_t BoostThreadFactory::getCurrentThreadId() const { - return boost::this_thread::get_id(); -} -} -} -} // apache::thrift::concurrency - -#endif // USE_BOOST_THREAD diff --git a/lib/cpp/src/thrift/concurrency/BoostThreadFactory.h b/lib/cpp/src/thrift/concurrency/BoostThreadFactory.h deleted file mode 100644 index bf11a708b..000000000 --- a/lib/cpp/src/thrift/concurrency/BoostThreadFactory.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef _THRIFT_CONCURRENCY_BOOSTTHREADFACTORY_H_ -#define _THRIFT_CONCURRENCY_BOOSTTHREADFACTORY_H_ 1 - -#include <thrift/concurrency/Monitor.h> -#include <thrift/concurrency/Thread.h> -#include <thrift/stdcxx.h> - -namespace apache { -namespace thrift { -namespace concurrency { - -/** - * A thread factory to create posix threads - * - * @version $Id:$ - */ -class BoostThreadFactory : public ThreadFactory { - -public: - /** - * Boost thread factory. All threads created by a factory are reference-counted - * via stdcxx::shared_ptr. The factory guarantees that threads and the Runnable tasks they - * host will be properly cleaned up once the last strong reference to both is given up. - * - * Threads are created with the specified boost policy, priority, stack-size. A detachable thread - * is not joinable. - * - * By default threads are not joinable. - */ - - BoostThreadFactory(bool detached = true); - - // From ThreadFactory; - stdcxx::shared_ptr<Thread> newThread(stdcxx::shared_ptr<Runnable> runnable) const; - - // From ThreadFactory; - Thread::id_t getCurrentThreadId() const; -}; - -} -} -} // apache::thrift::concurrency - -#endif // #ifndef _THRIFT_CONCURRENCY_BOOSTTHREADFACTORY_H_ diff --git a/lib/cpp/src/thrift/concurrency/PlatformThreadFactory.h b/lib/cpp/src/thrift/concurrency/PlatformThreadFactory.h index 545b57270..99b44033b 100644 --- a/lib/cpp/src/thrift/concurrency/PlatformThreadFactory.h +++ b/lib/cpp/src/thrift/concurrency/PlatformThreadFactory.h @@ -22,9 +22,7 @@ // clang-format off #include <thrift/thrift-config.h> -#if USE_BOOST_THREAD -# include <thrift/concurrency/BoostThreadFactory.h> -#elif USE_STD_THREAD +#if USE_STD_THREAD # include <thrift/concurrency/StdThreadFactory.h> #else # include <thrift/concurrency/PosixThreadFactory.h> @@ -36,9 +34,7 @@ namespace thrift { namespace concurrency { // clang-format off -#if USE_BOOST_THREAD - typedef BoostThreadFactory PlatformThreadFactory; -#elif USE_STD_THREAD +#if USE_STD_THREAD typedef StdThreadFactory PlatformThreadFactory; #else typedef PosixThreadFactory PlatformThreadFactory; diff --git a/lib/cpp/src/thrift/concurrency/Thread.h b/lib/cpp/src/thrift/concurrency/Thread.h index 788623bda..7e2d251e0 100644 --- a/lib/cpp/src/thrift/concurrency/Thread.h +++ b/lib/cpp/src/thrift/concurrency/Thread.h @@ -25,9 +25,7 @@ #include <thrift/thrift-config.h> -#if USE_BOOST_THREAD -#include <boost/thread.hpp> -#elif USE_STD_THREAD +#if USE_STD_THREAD #include <thread> #else #ifdef HAVE_PTHREAD_H @@ -80,12 +78,7 @@ private: class Thread { public: -#if USE_BOOST_THREAD - typedef boost::thread::id id_t; - - static inline bool is_current(id_t t) { return t == boost::this_thread::get_id(); } - static inline id_t get_current() { return boost::this_thread::get_id(); } -#elif USE_STD_THREAD +#if USE_STD_THREAD typedef std::thread::id id_t; static inline bool is_current(id_t t) { return t == std::this_thread::get_id(); } |