Merge pull request #8 from alpc62/master

remove util/Mutex.h
This commit is contained in:
xiehan
2020-08-01 01:35:46 +08:00
committed by GitHub
4 changed files with 57 additions and 92 deletions
-1
View File
@@ -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
+28 -1
View File
@@ -29,7 +29,6 @@
#include <chrono>
#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;
+29 -24
View File
@@ -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_;
};
-66
View File
@@ -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 <pthread.h>
/**
* @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