diff --git a/CMakeLists_Headers.txt b/CMakeLists_Headers.txt index e8c19eb0..0b736062 100644 --- a/CMakeLists_Headers.txt +++ b/CMakeLists_Headers.txt @@ -67,7 +67,6 @@ set(INCLUDE_HEADERS src/manager/WFFacilities.inl src/util/EncodeStream.h src/util/LRUCache.h - src/util/Mutex.h src/util/StringUtil.h src/util/URIParser.h src/util/MD5Util.h diff --git a/src/manager/UpstreamManager.cc b/src/manager/UpstreamManager.cc index 69fe0207..19721c1c 100644 --- a/src/manager/UpstreamManager.cc +++ b/src/manager/UpstreamManager.cc @@ -29,7 +29,6 @@ #include #include "list.h" #include "rbtree.h" -#include "Mutex.h" #include "URIParser.h" #include "StringUtil.h" #include "EndpointParams.h" @@ -39,6 +38,34 @@ #define MTTR_SECOND 30 #define VIRTUAL_GROUP_SIZE 16 +namespace //anoymous namespace, for safe, avoid conflict +{ +// RAII: YES +class ReadLock +{ +public: + ReadLock(pthread_rwlock_t& rwlock): rwlock_(&rwlock) { pthread_rwlock_rdlock(rwlock_); } + ReadLock(pthread_rwlock_t *rwlock): rwlock_(rwlock) { pthread_rwlock_rdlock(rwlock_); } + ~ReadLock() { pthread_rwlock_unlock(rwlock_); } + +private: + pthread_rwlock_t *rwlock_; +}; + +// RAII: YES +class WriteLock +{ +public: + WriteLock(pthread_rwlock_t& rwlock): rwlock_(&rwlock) { pthread_rwlock_wrlock(rwlock_); } + WriteLock(pthread_rwlock_t *rwlock): rwlock_(rwlock) { pthread_rwlock_wrlock(rwlock_); } + ~WriteLock() { pthread_rwlock_unlock(rwlock_); } + +private: + pthread_rwlock_t *rwlock_; +}; + +} + class UpstreamAddress; class UpstreamGroup; class Upstream; diff --git a/src/manager/WFGlobal.cc b/src/manager/WFGlobal.cc index 28977c29..7072ed31 100644 --- a/src/manager/WFGlobal.cc +++ b/src/manager/WFGlobal.cc @@ -36,7 +36,6 @@ #include "DNSCache.h" #include "RouteManager.h" #include "Executor.h" -#include "Mutex.h" #include "WFTask.h" #include "WFTaskError.h" @@ -426,42 +425,48 @@ public: ExecQueue *get_exec_queue(const std::string& queue_name) { - ExecQueue *queue; - ExecQueueMap::iterator iter; + ExecQueue *queue = NULL; + pthread_rwlock_rdlock(&rwlock_); + const auto iter = queue_map_.find(queue_name); + + if (iter != queue_map_.cend()) + queue = iter->second; + + pthread_rwlock_unlock(&rwlock_); + + if (!queue) { - ReadLock lock(mutex_); - - iter = queue_map_.find(queue_name); - if (iter != queue_map_.end()) - return iter->second; - } - - queue = new ExecQueue(); - if (queue->init() >= 0) - { - WriteLock lock(mutex_); - auto ret = queue_map_.emplace(queue_name, queue); - - if (!ret.second) + queue = new ExecQueue(); + if (queue->init() < 0) { - queue->deinit(); delete queue; - queue = ret.first->second; + queue = NULL; } + else + { + pthread_rwlock_wrlock(&rwlock_); + const auto ret = queue_map_.emplace(queue_name, queue); - return queue; + if (!ret.second) + { + queue->deinit(); + delete queue; + queue = ret.first->second; + } + + pthread_rwlock_unlock(&rwlock_); + } } - delete queue; - return NULL; + return queue; } Executor *get_compute_executor() { return &compute_executor_; } private: __ExecManager(): - mutex_(PTHREAD_RWLOCK_INITIALIZER) + rwlock_(PTHREAD_RWLOCK_INITIALIZER) { int compute_threads = __WFGlobal::get_instance()-> get_global_settings()-> @@ -486,7 +491,7 @@ private: } private: - pthread_rwlock_t mutex_; + pthread_rwlock_t rwlock_; ExecQueueMap queue_map_; Executor compute_executor_; }; diff --git a/src/util/Mutex.h b/src/util/Mutex.h deleted file mode 100644 index 6f120598..00000000 --- a/src/util/Mutex.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - Copyright (c) 2019 Sogou, Inc. - - Licensed 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. - - Authors: Wu Jiaxu (wujiaxu@sogou-inc.com) -*/ - -#ifndef _MUTEX_H_ -#define _MUTEX_H_ - -#include - -/** - * @file Mutex.h - * @brief RAII style pthread lock - */ - -// RAII: YES -class Lock -{ -public: - Lock(pthread_mutex_t& mutex): mutex_(&mutex) { pthread_mutex_lock(mutex_); } - Lock(pthread_mutex_t *mutex): mutex_(mutex) { pthread_mutex_lock(mutex_); } - ~Lock() { pthread_mutex_unlock(mutex_); } - -private: - pthread_mutex_t *mutex_; -}; - -// RAII: YES -class ReadLock -{ -public: - ReadLock(pthread_rwlock_t& rwlock): rwlock_(&rwlock) { pthread_rwlock_rdlock(rwlock_); } - ReadLock(pthread_rwlock_t *rwlock): rwlock_(rwlock) { pthread_rwlock_rdlock(rwlock_); } - ~ReadLock() { pthread_rwlock_unlock(rwlock_); } - -private: - pthread_rwlock_t *rwlock_; -}; - -// RAII: YES -class WriteLock -{ -public: - WriteLock(pthread_rwlock_t& rwlock): rwlock_(&rwlock) { pthread_rwlock_wrlock(rwlock_); } - WriteLock(pthread_rwlock_t *rwlock): rwlock_(rwlock) { pthread_rwlock_wrlock(rwlock_); } - ~WriteLock() { pthread_rwlock_unlock(rwlock_); } - -private: - pthread_rwlock_t *rwlock_; -}; - -#endif -