From 0f77a4511bc448ef3ba30f5e25d7dc61e6011a05 Mon Sep 17 00:00:00 2001 From: Eric Gross Date: Tue, 4 Oct 2022 16:38:24 -0500 Subject: [PATCH] Hack up sync within client --- src/grpc_client.cc | 25 +++++++++++++++++-------- src/grpc_client.h | 1 + 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/grpc_client.cc b/src/grpc_client.cc index 9df99fd6..6ac80c96 100644 --- a/src/grpc_client.cc +++ b/src/grpc_client.cc @@ -207,10 +207,12 @@ int32_t ClientCleanUpProc(grpc_labview::gRPCid* clientId) { return -1; } - - for (auto activeClientCall = client->ActiveClientCalls.begin(); activeClientCall != client->ActiveClientCalls.end(); activeClientCall++) { - (*activeClientCall)->_context.TryCancel(); + std::lock_guard lock(client->clientLock); + for (auto activeClientCall = client->ActiveClientCalls.begin(); activeClientCall != client->ActiveClientCalls.end(); activeClientCall++) + { + (*activeClientCall)->_context.TryCancel(); + } } return CloseClient(client.get()); } @@ -263,6 +265,7 @@ LIBRARY_EXPORT int32_t ClientUnaryCall( return 0; }); + std::lock_guard lock(client->clientLock); client->ActiveClientCalls.push_back(clientCall); return 0; } @@ -299,6 +302,7 @@ LIBRARY_EXPORT int32_t CompleteClientUnaryCall2( { } } + std::lock_guard lock(call->_client->clientLock); call->_client->ActiveClientCalls.remove(call.get()); return result; } @@ -346,6 +350,7 @@ LIBRARY_EXPORT int32_t ClientBeginClientStreamingCall( auto writer = grpc::internal::ClientWriterFactory::Create(client->Channel.get(), method, &clientCall->_context, clientCall->_response.get()); clientCall->_writer = std::shared_ptr>(writer); + std::lock_guard lock(client->clientLock); client->ActiveClientCalls.push_back(clientCall); return 0; } @@ -389,8 +394,9 @@ LIBRARY_EXPORT int32_t ClientBeginServerStreamingCall( auto reader = grpc::internal::ClientReaderFactory::Create(client->Channel.get(), method, &clientCall->_context, *clientCall->_request.get()); clientCall->_reader = std::shared_ptr>(reader); + std::lock_guard lock(client->clientLock); client->ActiveClientCalls.push_back(clientCall); - return 0; + return 0; } //--------------------------------------------------------------------- @@ -429,6 +435,7 @@ LIBRARY_EXPORT int32_t ClientBeginBidiStreamingCall( auto readerWriter = grpc::internal::ClientReaderWriterFactory::Create(client->Channel.get(), method, &clientCall->_context); clientCall->_readerWriter = std::shared_ptr>(readerWriter); + std::lock_guard lock(client->clientLock); client->ActiveClientCalls.push_back(clientCall); return 0; } @@ -540,8 +547,10 @@ LIBRARY_EXPORT int32_t FinishClientCompleteClientStreamingCall( { } } - - call->_client->ActiveClientCalls.remove(call.get()); + { + std::lock_guard lock(call->_client->clientLock); + call->_client->ActiveClientCalls.remove(call.get()); + } grpc_labview::gPointerManager.UnregisterPointer(callId); return result; } @@ -597,7 +606,7 @@ LIBRARY_EXPORT int32_t ClientCompleteStreamingCall( { } } - + std::lock_guard lock(call->_client->clientLock); call->_client->ActiveClientCalls.remove(call.get()); return result; } @@ -632,7 +641,7 @@ LIBRARY_EXPORT int32_t ClientCancelCall( { } } - + std::lock_guard lock(call->_client->clientLock); call->_client->ActiveClientCalls.remove(call.get()); return result; } \ No newline at end of file diff --git a/src/grpc_client.h b/src/grpc_client.h index 90df3c5a..be3d6133 100644 --- a/src/grpc_client.h +++ b/src/grpc_client.h @@ -30,6 +30,7 @@ namespace grpc_labview public: std::shared_ptr Channel; std::list ActiveClientCalls; + std::mutex clientLock; }; //---------------------------------------------------------------------