diff options
author | Dan Wittmer <wittmer@google.com> | 2017-11-16 16:45:23 -0800 |
---|---|---|
committer | Dan Wittmer <wittmer@google.com> | 2017-11-16 16:45:23 -0800 |
commit | 6d18fcd3ab5815af5044d416f20f7b684d950e17 (patch) | |
tree | 0e4ff04e8e437ae3acd7de47c832b69ee1562c94 | |
parent | 728f1d2c44b112800d3d420a30033d1e3e291b92 (diff) |
Add ServerStartedCondition to hold the mutex, condition variable and condition. Changes allow callers to correctly handle spurious wakeups.
-rw-r--r-- | test/cpp/interop/interop_server.cc | 8 | ||||
-rw-r--r-- | test/cpp/interop/server_helper.h | 12 |
2 files changed, 15 insertions, 5 deletions
diff --git a/test/cpp/interop/interop_server.cc b/test/cpp/interop/interop_server.cc index bcef9c38b7..5b9e229804 100644 --- a/test/cpp/interop/interop_server.cc +++ b/test/cpp/interop/interop_server.cc @@ -323,7 +323,7 @@ void grpc::testing::interop::RunServer( void grpc::testing::interop::RunServer( std::shared_ptr<ServerCredentials> creds, const int port, - std::condition_variable *server_started_condition) { + ServerStartedCondition *server_started_condition) { GPR_ASSERT(port != 0); std::ostringstream server_address; server_address << "0.0.0.0:" << port; @@ -342,7 +342,11 @@ void grpc::testing::interop::RunServer( gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); // Signal that the server has started. - if (server_started_condition) server_started_condition->notify_all(); + if (server_started_condition) { + std::unique_lock<std::mutex> lock(server_started_condition->mutex); + server_started_condition->server_started = true; + server_started_condition->condition.notify_all(); + } while (!gpr_atm_no_barrier_load(&g_got_sigint)) { gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), diff --git a/test/cpp/interop/server_helper.h b/test/cpp/interop/server_helper.h index bdbea8f653..ab5de07c0e 100644 --- a/test/cpp/interop/server_helper.h +++ b/test/cpp/interop/server_helper.h @@ -52,6 +52,12 @@ namespace interop { extern gpr_atm g_got_sigint; +struct ServerStartedCondition { + std::mutex mutex; + std::condition_variable condition; + bool server_started = false; +}; + /// Run gRPC interop server using port FLAGS_port. /// /// \param creds The credentials associated with the server. @@ -61,11 +67,11 @@ void RunServer(std::shared_ptr<ServerCredentials> creds); /// /// \param creds The credentials associated with the server. /// \param port Port to use for the server. -/// \param server_started_condition (optional) Condition variable used to notify -/// when the server has started. +/// \param server_started_condition (optional) Struct holding mutex, condition +/// variable, and condition used to notify when the server has started. void RunServer(std::shared_ptr<ServerCredentials> creds, int port, - std::condition_variable *server_started_condition); + ServerStartedCondition *server_started_condition); } // namespace interop } // namespace testing |