Rewriting mutex, condition_variable, and thread.

pull/1185/head
Nicolas "Pixel" Noble 10 years ago
parent ae96ae2bad
commit ff2828be3d
  1. 1
      Makefile
  2. 49
      include/grpc++/config.h
  3. 46
      include/grpc++/impl/sync.h
  4. 49
      include/grpc++/impl/sync_cxx11.h
  5. 98
      include/grpc++/impl/sync_no_cxx11.h
  6. 46
      include/grpc++/impl/thd.h
  7. 45
      include/grpc++/impl/thd_cxx11.h
  8. 81
      include/grpc++/impl/thd_no_cxx11.h
  9. 5
      include/grpc++/server.h
  10. 10
      src/cpp/server/server.cc
  11. 9
      src/cpp/server/server_context.cc
  12. 11
      src/cpp/server/thread_pool.cc
  13. 9
      src/cpp/server/thread_pool.h
  14. 1
      templates/Makefile.template
  15. 2
      vsprojects/vs2010/global.props

@ -218,7 +218,6 @@ ifeq ($(HAS_CXX11),true)
CXXFLAGS += -std=c++11 CXXFLAGS += -std=c++11
else else
CXXFLAGS += -std=c++0x CXXFLAGS += -std=c++0x
DEFINES += GRPC_OLD_CXX
endif endif
CPPFLAGS += -g -Wall -Wextra -Werror -Wno-long-long -Wno-unused-parameter CPPFLAGS += -g -Wall -Wextra -Werror -Wno-long-long -Wno-unused-parameter
LDFLAGS += -g LDFLAGS += -g

@ -34,11 +34,46 @@
#ifndef GRPCXX_CONFIG_H #ifndef GRPCXX_CONFIG_H
#define GRPCXX_CONFIG_H #define GRPCXX_CONFIG_H
#ifdef GRPC_OLD_CXX #if !defined(GRPC_NO_AUTODETECT_PLATFORM)
#ifdef _MSC_VER
// Visual Studio 2010 is 1600.
#if _MSC_VER < 1600
#error "gRPC is only supported with Visual Studio starting at 2010"
// Visual Studio 2013 is 1800.
#elif _MSC_VER < 1800
#define GRPC_CXX0X_NO_FINAL 1
#define GRPC_CXX0X_NO_OVERRIDE 1
#define GRPC_CXX0X_NO_CHRONO 1
#define GRPC_CXX0X_NO_THREAD 1
#endif
#endif // Visual Studio
#ifndef __clang__
#ifdef __GNUC__
// nullptr was added in gcc 4.6
#if (__GNUC__ * 100 + __GNUC_MINOR__ < 406)
#define GRPC_CXX0X_NO_NULLPTR 1
#endif
// final and override were added in gcc 4.7
#if (__GNUC__ * 100 + __GNUC_MINOR__ < 407)
#define GRPC_CXX0X_NO_FINAL 1
#define GRPC_CXX0X_NO_OVERRIDE 1
#endif
#endif
#endif
#endif
#ifdef GRPC_CXX0X_NO_FINAL
#define GRPC_FINAL #define GRPC_FINAL
#define GRPC_OVERRIDE
#else #else
#define GRPC_FINAL final #define GRPC_FINAL final
#endif
#ifdef GRPC_CXX0X_NO_OVERRIDE
#define GRPC_OVERRIDE
#else
#define GRPC_OVERRIDE override #define GRPC_OVERRIDE override
#endif #endif
@ -65,15 +100,7 @@
::google::protobuf::io::ZeroCopyInputStream ::google::protobuf::io::ZeroCopyInputStream
#endif #endif
#ifndef __clang__ #ifdef GRPC_CXX0X_NO_NULLPTR
#ifdef __GNUC__
#if (__GNUC__ * 100 + __GNUC_MINOR__ < 406)
#define GRPC_NO_NULLPTR
#endif
#endif
#endif
#ifdef GRPC_NO_NULLPTR
#include <memory> #include <memory>
const class { const class {
public: public:

@ -0,0 +1,46 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef GRPCXX_IMPL_SYNC_H
#define GRPCXX_IMPL_SYNC_H
#include <grpc++/config.h>
#ifdef GRPC_CXX0X_NO_THREAD
#include <grpc++/impl/sync_nocxx11.h>
#else
#include <grpc++/impl/sync_cxx11.h>
#endif
#endif // GRPCXX_IMPL_SYNC_H

@ -0,0 +1,49 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef GRPCXX_IMPL_SYNC_CXX11_H
#define GRPCXX_IMPL_SYNC_CXX11_H
#include <condition_variable>
#include <mutex>
namespace grpc {
using std::condition_variable;
using std::mutex;
using std::lock_guard;
using std::unique_lock;
} // namespace grpc
#endif // GRPCXX_IMPL_SYNC_CXX11_H

@ -0,0 +1,98 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef GRPCXX_IMPL_SYNC_NO_CXX11_H
#define GRPCXX_IMPL_SYNC_NO_CXX11_H
#include <grpc/support/sync.h>
namespace grpc {
template<class mutex>
class lock_guard;
class condition_variable;
class mutex {
public:
mutex() { gpr_mu_init(&mu_); }
~mutex() { gpr_mu_destroy(&mu_); }
private:
::gpr_mu mu_;
template <class mutex>
friend class lock_guard;
friend class condition_variable;
};
template <class mutex>
class lock_guard {
public:
lock_guard(mutex &mu) : mu_(mu), locked(true) { gpr_mu_lock(&mu.mu_); }
~lock_guard() { unlock(); }
void lock() {
if (!locked) gpr_mu_lock(&mu_.mu_);
locked = true;
}
void unlock() {
if (locked) gpr_mu_unlock(&mu_.mu_);
locked = false;
}
private:
mutex &mu_;
bool locked;
friend class condition_variable;
};
template <class mutex>
class unique_lock : public lock_guard<mutex> {
public:
unique_lock(mutex &mu) : lock_guard(mu) { }
};
class condition_variable {
public:
condition_variable() { gpr_cv_init(&cv_); }
~condition_variable() { gpr_cv_destroy(&cv_); }
void wait(lock_guard<mutex> &mu) {
mu.locked = false;
gpr_cv_wait(&cv_, &mu.mu_.mu_, gpr_inf_future);
mu.locked = true;
}
void notify_one() { gpr_cv_signal(&cv_); }
void notify_all() { gpr_cv_broadcast(&cv_); }
private:
gpr_cv cv_;
};
} // namespace grpc
#endif // GRPCXX_IMPL_SYNC_NO_CXX11_H

@ -0,0 +1,46 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef GRPCXX_IMPL_THD_H
#define GRPCXX_IMPL_THD_H
#include <grpc++/config.h>
#ifdef GRPC_CXX0X_NO_THREAD
#include <grpc++/impl/thd_nocxx11.h>
#else
#include <grpc++/impl/thd_cxx11.h>
#endif
#endif // GRPCXX_IMPL_THD_H

@ -0,0 +1,45 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef GRPCXX_IMPL_THD_CXX11_H
#define GRPCXX_IMPL_THD_CXX11_H
#include <thread>
namespace grpc {
using std::thread;
} // namespace grpc
#endif // GRPCXX_IMPL_THD_CXX11_H

@ -0,0 +1,81 @@
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef GRPCXX_IMPL_THD_NO_CXX11_H
#define GRPCXX_IMPL_THD_NO_CXX11_H
#include <grpc/support/thd.h>
namespace grpc {
class thread {
public:
template<class T> thread(void (T::*fptr)(), T *obj) {
func_ = new thread_function<T>(fptr, obj);
start();
}
~thread() { delete func_; }
void join() { gpr_thd_join(thd); }
private:
void start() {
gpr_thd_options options = gpr_thd_options_default();
gpr_thd_options_set_joinable(&options);
gpr_thd_new(&thd, thread_func, (void *) func_, &options);
}
static void thread_func(void *arg) {
thread_function_base *func = (thread_function_base *) arg;
func->call();
}
class thread_function_base {
public:
virtual ~thread_function_base() { }
virtual void call() = 0;
};
template<class T>
class thread_function : public thread_function_base {
public:
thread_function(void (T::*fptr)(), T *obj)
: fptr_(fptr)
, obj_(obj) { }
virtual void call() { (obj_->*fptr_)(); }
private:
void (T::*fptr_)();
T *obj_;
};
thread_function_base *func_;
gpr_thd_id thd;
};
} // namespace grpc
#endif // GRPCXX_IMPL_THD_NO_CXX11_H

@ -41,6 +41,7 @@
#include <grpc++/config.h> #include <grpc++/config.h>
#include <grpc++/impl/call.h> #include <grpc++/impl/call.h>
#include <grpc++/impl/service_type.h> #include <grpc++/impl/service_type.h>
#include <grpc++/impl/sync.h>
#include <grpc++/status.h> #include <grpc++/status.h>
struct grpc_server; struct grpc_server;
@ -108,12 +109,12 @@ class Server GRPC_FINAL : private CallHook,
CompletionQueue cq_; CompletionQueue cq_;
// Sever status // Sever status
std::mutex mu_; grpc::mutex mu_;
bool started_; bool started_;
bool shutdown_; bool shutdown_;
// The number of threads which are running callbacks. // The number of threads which are running callbacks.
int num_running_cb_; int num_running_cb_;
std::condition_variable callback_cv_; grpc::condition_variable callback_cv_;
std::list<SyncRequest> sync_methods_; std::list<SyncRequest> sync_methods_;

@ -183,7 +183,7 @@ Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned)
Server::~Server() { Server::~Server() {
{ {
std::unique_lock<std::mutex> lock(mu_); grpc::unique_lock<grpc::mutex> lock(mu_);
if (started_ && !shutdown_) { if (started_ && !shutdown_) {
lock.unlock(); lock.unlock();
Shutdown(); Shutdown();
@ -259,7 +259,7 @@ bool Server::Start() {
} }
void Server::Shutdown() { void Server::Shutdown() {
std::unique_lock<std::mutex> lock(mu_); grpc::unique_lock<grpc::mutex> lock(mu_);
if (started_ && !shutdown_) { if (started_ && !shutdown_) {
shutdown_ = true; shutdown_ = true;
grpc_server_shutdown(server_); grpc_server_shutdown(server_);
@ -273,7 +273,7 @@ void Server::Shutdown() {
} }
void Server::Wait() { void Server::Wait() {
std::unique_lock<std::mutex> lock(mu_); grpc::unique_lock<grpc::mutex> lock(mu_);
while (num_running_cb_ != 0) { while (num_running_cb_ != 0) {
callback_cv_.wait(lock); callback_cv_.wait(lock);
} }
@ -405,7 +405,7 @@ void Server::RequestAsyncGenericCall(GenericServerContext* context,
void Server::ScheduleCallback() { void Server::ScheduleCallback() {
{ {
std::unique_lock<std::mutex> lock(mu_); grpc::unique_lock<grpc::mutex> lock(mu_);
num_running_cb_++; num_running_cb_++;
} }
thread_pool_->ScheduleCallback(std::bind(&Server::RunRpc, this)); 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_--; num_running_cb_--;
if (shutdown_) { if (shutdown_) {
callback_cv_.notify_all(); callback_cv_.notify_all();

@ -34,6 +34,7 @@
#include <grpc++/server_context.h> #include <grpc++/server_context.h>
#include <grpc++/impl/call.h> #include <grpc++/impl/call.h>
#include <grpc++/impl/sync.h>
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include "src/cpp/util/time.h" #include "src/cpp/util/time.h"
@ -55,14 +56,14 @@ class ServerContext::CompletionOp GRPC_FINAL : public CallOpBuffer {
void Unref(); void Unref();
private: private:
std::mutex mu_; grpc::mutex mu_;
int refs_; int refs_;
bool finalized_; bool finalized_;
bool cancelled_; bool cancelled_;
}; };
void ServerContext::CompletionOp::Unref() { void ServerContext::CompletionOp::Unref() {
std::unique_lock<std::mutex> lock(mu_); grpc::unique_lock<grpc::mutex> lock(mu_);
if (--refs_ == 0) { if (--refs_ == 0) {
lock.unlock(); lock.unlock();
delete this; delete this;
@ -71,13 +72,13 @@ void ServerContext::CompletionOp::Unref() {
bool ServerContext::CompletionOp::CheckCancelled(CompletionQueue* cq) { bool ServerContext::CompletionOp::CheckCancelled(CompletionQueue* cq) {
cq->TryPluck(this); cq->TryPluck(this);
std::lock_guard<std::mutex> g(mu_); grpc::lock_guard<grpc::mutex> g(mu_);
return finalized_ ? cancelled_ : false; return finalized_ ? cancelled_ : false;
} }
bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) { bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) {
GPR_ASSERT(CallOpBuffer::FinalizeResult(tag, status)); GPR_ASSERT(CallOpBuffer::FinalizeResult(tag, status));
std::unique_lock<std::mutex> lock(mu_); grpc::unique_lock<grpc::mutex> lock(mu_);
finalized_ = true; finalized_ = true;
if (!*status) cancelled_ = true; if (!*status) cancelled_ = true;
if (--refs_ == 0) { if (--refs_ == 0) {

@ -31,6 +31,9 @@
* *
*/ */
#include <grpc++/impl/sync.h>
#include <grpc++/impl/thd.h>
#include "src/cpp/server/thread_pool.h" #include "src/cpp/server/thread_pool.h"
namespace grpc { namespace grpc {
@ -38,7 +41,7 @@ namespace grpc {
void ThreadPool::ThreadFunc() { void ThreadPool::ThreadFunc() {
for (;;) { for (;;) {
// Wait until work is available or we are shutting down. // 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()) { if (!shutdown_ && callbacks_.empty()) {
cv_.wait(lock); cv_.wait(lock);
} }
@ -57,13 +60,13 @@ void ThreadPool::ThreadFunc() {
ThreadPool::ThreadPool(int num_threads) : shutdown_(false) { ThreadPool::ThreadPool(int num_threads) : shutdown_(false) {
for (int i = 0; i < num_threads; i++) { 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() { ThreadPool::~ThreadPool() {
{ {
std::lock_guard<std::mutex> lock(mu_); grpc::lock_guard<grpc::mutex> lock(mu_);
shutdown_ = true; shutdown_ = true;
cv_.notify_all(); cv_.notify_all();
} }
@ -73,7 +76,7 @@ ThreadPool::~ThreadPool() {
} }
void ThreadPool::ScheduleCallback(const std::function<void()>& callback) { 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); callbacks_.push(callback);
cv_.notify_one(); cv_.notify_one();
} }

@ -35,6 +35,9 @@
#define GRPC_INTERNAL_CPP_SERVER_THREAD_POOL_H #define GRPC_INTERNAL_CPP_SERVER_THREAD_POOL_H
#include <grpc++/config.h> #include <grpc++/config.h>
#include <grpc++/impl/sync.h>
#include <grpc++/impl/thd.h>
#include <grpc++/thread_pool_interface.h> #include <grpc++/thread_pool_interface.h>
#include <queue> #include <queue>
@ -50,11 +53,11 @@ class ThreadPool GRPC_FINAL : public ThreadPoolInterface {
void ScheduleCallback(const std::function<void()>& callback) GRPC_OVERRIDE; void ScheduleCallback(const std::function<void()>& callback) GRPC_OVERRIDE;
private: private:
std::mutex mu_; grpc::mutex mu_;
std::condition_variable cv_; grpc::condition_variable cv_;
bool shutdown_; bool shutdown_;
std::queue<std::function<void()>> callbacks_; std::queue<std::function<void()>> callbacks_;
std::vector<std::thread> threads_; std::vector<grpc::thread> threads_;
void ThreadFunc(); void ThreadFunc();
}; };

@ -235,7 +235,6 @@ ifeq ($(HAS_CXX11),true)
CXXFLAGS += -std=c++11 CXXFLAGS += -std=c++11
else else
CXXFLAGS += -std=c++0x CXXFLAGS += -std=c++0x
DEFINES += GRPC_OLD_CXX
endif endif
CPPFLAGS += -g -Wall -Wextra -Werror -Wno-long-long -Wno-unused-parameter CPPFLAGS += -g -Wall -Wextra -Werror -Wno-long-long -Wno-unused-parameter
LDFLAGS += -g LDFLAGS += -g

@ -6,7 +6,7 @@
<ItemDefinitionGroup> <ItemDefinitionGroup>
<ClCompile> <ClCompile>
<AdditionalIncludeDirectories>$(SolutionDir)\..\..;$(SolutionDir)\..\..\include;$(SolutionDir)\..\..\third_party\zlib;$(SolutionDir)\..\third_party;$(SolutionDir)\..\..\third_party\openssl\inc32;$(SolutionDir)\..\..\third_party\protobuf\src</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(SolutionDir)\..\..;$(SolutionDir)\..\..\include;$(SolutionDir)\..\..\third_party\zlib;$(SolutionDir)\..\third_party;$(SolutionDir)\..\..\third_party\openssl\inc32;$(SolutionDir)\..\..\third_party\protobuf\src</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GRPC_OLD_CXX;_CRT_SECURE_NO_WARNINGS;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<WarningLevel>EnableAllWarnings</WarningLevel> <WarningLevel>EnableAllWarnings</WarningLevel>
</ClCompile> </ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>

Loading…
Cancel
Save