diff options
author | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2015-04-03 03:16:46 +0200 |
---|---|---|
committer | Nicolas "Pixel" Noble <pixel@nobis-crew.org> | 2015-04-04 02:06:13 +0200 |
commit | ff2828be3dcb22f09d05117eaa1dddea17703ecf (patch) | |
tree | b0aa009edfaa3d10e4a7382bfe6205f787a13db7 /src/cpp/server | |
parent | ae96ae2baddcb015366b094ff484ff7eb9fbbc1d (diff) |
Rewriting mutex, condition_variable, and thread.
Diffstat (limited to 'src/cpp/server')
-rw-r--r-- | src/cpp/server/server.cc | 10 | ||||
-rw-r--r-- | src/cpp/server/server_context.cc | 9 | ||||
-rw-r--r-- | src/cpp/server/thread_pool.cc | 11 | ||||
-rw-r--r-- | src/cpp/server/thread_pool.h | 9 |
4 files changed, 23 insertions, 16 deletions
diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index bd0a23739c..046133c5eb 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -183,7 +183,7 @@ Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned) Server::~Server() { { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); if (started_ && !shutdown_) { lock.unlock(); Shutdown(); @@ -259,7 +259,7 @@ bool Server::Start() { } void Server::Shutdown() { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); if (started_ && !shutdown_) { shutdown_ = true; grpc_server_shutdown(server_); @@ -273,7 +273,7 @@ void Server::Shutdown() { } void Server::Wait() { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); while (num_running_cb_ != 0) { callback_cv_.wait(lock); } @@ -405,7 +405,7 @@ void Server::RequestAsyncGenericCall(GenericServerContext* context, void Server::ScheduleCallback() { { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); num_running_cb_++; } thread_pool_->ScheduleCallback(std::bind(&Server::RunRpc, this)); @@ -426,7 +426,7 @@ void Server::RunRpc() { } { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); num_running_cb_--; if (shutdown_) { callback_cv_.notify_all(); diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index 0fe4b4d8e3..ffd6d30d5d 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -34,6 +34,7 @@ #include <grpc++/server_context.h> #include <grpc++/impl/call.h> +#include <grpc++/impl/sync.h> #include <grpc/grpc.h> #include <grpc/support/log.h> #include "src/cpp/util/time.h" @@ -55,14 +56,14 @@ class ServerContext::CompletionOp GRPC_FINAL : public CallOpBuffer { void Unref(); private: - std::mutex mu_; + grpc::mutex mu_; int refs_; bool finalized_; bool cancelled_; }; void ServerContext::CompletionOp::Unref() { - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); if (--refs_ == 0) { lock.unlock(); delete this; @@ -71,13 +72,13 @@ void ServerContext::CompletionOp::Unref() { bool ServerContext::CompletionOp::CheckCancelled(CompletionQueue* cq) { cq->TryPluck(this); - std::lock_guard<std::mutex> g(mu_); + grpc::lock_guard<grpc::mutex> g(mu_); return finalized_ ? cancelled_ : false; } bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { GPR_ASSERT(CallOpBuffer::FinalizeResult(tag, status)); - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); finalized_ = true; if (!*status) cancelled_ = true; if (--refs_ == 0) { diff --git a/src/cpp/server/thread_pool.cc b/src/cpp/server/thread_pool.cc index 80c96111b1..e8d0e89ed2 100644 --- a/src/cpp/server/thread_pool.cc +++ b/src/cpp/server/thread_pool.cc @@ -31,6 +31,9 @@ * */ +#include <grpc++/impl/sync.h> +#include <grpc++/impl/thd.h> + #include "src/cpp/server/thread_pool.h" namespace grpc { @@ -38,7 +41,7 @@ namespace grpc { void ThreadPool::ThreadFunc() { for (;;) { // Wait until work is available or we are shutting down. - std::unique_lock<std::mutex> lock(mu_); + grpc::unique_lock<grpc::mutex> lock(mu_); if (!shutdown_ && callbacks_.empty()) { cv_.wait(lock); } @@ -57,13 +60,13 @@ void ThreadPool::ThreadFunc() { ThreadPool::ThreadPool(int num_threads) : shutdown_(false) { for (int i = 0; i < num_threads; i++) { - threads_.push_back(std::thread(&ThreadPool::ThreadFunc, this)); + threads_.push_back(grpc::thread(&ThreadPool::ThreadFunc, this)); } } ThreadPool::~ThreadPool() { { - std::lock_guard<std::mutex> lock(mu_); + grpc::lock_guard<grpc::mutex> lock(mu_); shutdown_ = true; cv_.notify_all(); } @@ -73,7 +76,7 @@ ThreadPool::~ThreadPool() { } void ThreadPool::ScheduleCallback(const std::function<void()>& callback) { - std::lock_guard<std::mutex> lock(mu_); + grpc::lock_guard<grpc::mutex> lock(mu_); callbacks_.push(callback); cv_.notify_one(); } diff --git a/src/cpp/server/thread_pool.h b/src/cpp/server/thread_pool.h index c773cdb629..0f24d6e9b3 100644 --- a/src/cpp/server/thread_pool.h +++ b/src/cpp/server/thread_pool.h @@ -35,6 +35,9 @@ #define GRPC_INTERNAL_CPP_SERVER_THREAD_POOL_H #include <grpc++/config.h> + +#include <grpc++/impl/sync.h> +#include <grpc++/impl/thd.h> #include <grpc++/thread_pool_interface.h> #include <queue> @@ -50,11 +53,11 @@ class ThreadPool GRPC_FINAL : public ThreadPoolInterface { void ScheduleCallback(const std::function<void()>& callback) GRPC_OVERRIDE; private: - std::mutex mu_; - std::condition_variable cv_; + grpc::mutex mu_; + grpc::condition_variable cv_; bool shutdown_; std::queue<std::function<void()>> callbacks_; - std::vector<std::thread> threads_; + std::vector<grpc::thread> threads_; void ThreadFunc(); }; |