From b092916a2ae7380ee22902d8c3cb217d6a7dc758 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Tue, 7 Apr 2015 01:17:14 +0200 Subject: [PATCH] Addressing concerns. --- include/grpc++/impl/sync.h | 3 +-- include/grpc++/impl/sync_no_cxx11.h | 13 ++++++++----- include/grpc++/impl/thd.h | 3 +-- include/grpc++/impl/thd_no_cxx11.h | 16 ++++++++++++---- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/include/grpc++/impl/sync.h b/include/grpc++/impl/sync.h index 64d1003685e..2f41d2bdebb 100644 --- a/include/grpc++/impl/sync.h +++ b/include/grpc++/impl/sync.h @@ -37,10 +37,9 @@ #include #ifdef GRPC_CXX0X_NO_THREAD -#include +#include #else #include - #endif #endif // GRPCXX_IMPL_SYNC_H diff --git a/include/grpc++/impl/sync_no_cxx11.h b/include/grpc++/impl/sync_no_cxx11.h index 3bf4fb8e3d3..5636373b814 100644 --- a/include/grpc++/impl/sync_no_cxx11.h +++ b/include/grpc++/impl/sync_no_cxx11.h @@ -56,13 +56,14 @@ class mutex { template class lock_guard { public: - lock_guard(mutex &mu) : mu_(mu), locked(true) { gpr_mu_lock(&mu.mu_); } - ~lock_guard() { unlock(); } - void lock() { + lock_guard(mutex &mu) : mu_(mu), locked(true) { gpr_mu_lock(&mu.mu_); } + ~lock_guard() { unlock_internal(); } + protected: + void lock_internal() { if (!locked) gpr_mu_lock(&mu_.mu_); locked = true; } - void unlock() { + void unlock_internal() { if (locked) gpr_mu_unlock(&mu_.mu_); locked = false; } @@ -75,7 +76,9 @@ class lock_guard { template class unique_lock : public lock_guard { public: - unique_lock(mutex &mu) : lock_guard(mu) { } + unique_lock(mutex &mu) : lock_guard(mu) { } + void lock() { lock_internal(); } + void unlock() { unlock_internal(); } }; class condition_variable { diff --git a/include/grpc++/impl/thd.h b/include/grpc++/impl/thd.h index 6a4c86a4903..4c4578a92da 100644 --- a/include/grpc++/impl/thd.h +++ b/include/grpc++/impl/thd.h @@ -37,10 +37,9 @@ #include #ifdef GRPC_CXX0X_NO_THREAD -#include +#include #else #include - #endif #endif // GRPCXX_IMPL_THD_H diff --git a/include/grpc++/impl/thd_no_cxx11.h b/include/grpc++/impl/thd_no_cxx11.h index f54cc1b6dee..a01b931df86 100644 --- a/include/grpc++/impl/thd_no_cxx11.h +++ b/include/grpc++/impl/thd_no_cxx11.h @@ -42,15 +42,22 @@ class thread { public: template thread(void (T::*fptr)(), T *obj) { func_ = new thread_function(fptr, obj); + joined_ = false; start(); } - ~thread() { delete func_; } - void join() { gpr_thd_join(thd); } + ~thread() { + if (!joined_) std::terminate(); + delete func_; + } + void join() { + gpr_thd_join(thd_); + joined_ = true; + } 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); + gpr_thd_new(&thd_, thread_func, (void *) func_, &options); } static void thread_func(void *arg) { thread_function_base *func = (thread_function_base *) arg; @@ -73,7 +80,8 @@ class thread { T *obj_; }; thread_function_base *func_; - gpr_thd_id thd; + gpr_thd_id thd_; + bool joined_; }; } // namespace grpc