Skip to content

Commit

Permalink
Fix data races in TcpClientListener
Browse files Browse the repository at this point in the history
Regarding of Helgrind report.
Changed type of TcpClientListener::thread_stop_requested_
from bool to std::atomic_bool.
The same for remove_devices_on_terminate_
  • Loading branch information
AKalinich-Luxoft committed Sep 4, 2020
1 parent 062d3b0 commit 0bbf89e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#define SRC_COMPONENTS_TRANSPORT_MANAGER_INCLUDE_TRANSPORT_MANAGER_TCP_TCP_CLIENT_LISTENER_H_

#include "transport_manager/transport_adapter/client_connection_listener.h"

#include <atomic>

#include "utils/lock.h"
#include "utils/threads/thread_delegate.h"

Expand Down Expand Up @@ -149,8 +152,8 @@ class TcpClientListener : public ClientConnectionListener {
bool started_;
threads::Thread* thread_;
int socket_;
bool thread_stop_requested_;
bool remove_devices_on_terminate_;
std::atomic_bool thread_stop_requested_;
std::atomic_bool remove_devices_on_terminate_;
int pipe_fds_[2];
NetworkInterfaceListener* interface_listener_;
const std::string designated_interface_;
Expand Down
10 changes: 5 additions & 5 deletions src/components/transport_manager/src/tcp/tcp_client_listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ TcpClientListener::TcpClientListener(TransportAdapterController* controller,

TransportAdapter::Error TcpClientListener::Init() {
SDL_LOG_AUTO_TRACE();
thread_stop_requested_ = false;
thread_stop_requested_.store(false);

if (!IsListeningOnSpecificInterface()) {
// Network interface is not specified. We will listen on all interfaces
Expand Down Expand Up @@ -343,7 +343,7 @@ void TcpClientListener::StopLoop() {
return;
}

thread_stop_requested_ = true;
thread_stop_requested_.store(true);

char dummy[1] = {0};
int ret = write(pipe_fds_[1], dummy, sizeof(dummy));
Expand Down Expand Up @@ -459,7 +459,7 @@ TransportAdapter::Error TcpClientListener::StartListeningThread() {
}
}

thread_stop_requested_ = false;
thread_stop_requested_.store(false);

if (!thread_->Start()) {
return TransportAdapter::FAIL;
Expand Down Expand Up @@ -538,7 +538,7 @@ bool TcpClientListener::StartOnNetworkInterface() {
}
}

remove_devices_on_terminate_ = true;
remove_devices_on_terminate_.store(true);

if (TransportAdapter::OK != StartListeningThread()) {
SDL_LOG_WARN("Failed to start TCP client listener");
Expand All @@ -564,7 +564,7 @@ bool TcpClientListener::StopOnNetworkInterface() {
socket_ = -1;
}

remove_devices_on_terminate_ = false;
remove_devices_on_terminate_.store(false);

SDL_LOG_INFO("TCP server socket on " << designated_interface_
<< " stopped");
Expand Down

0 comments on commit 0bbf89e

Please sign in to comment.