diff options
author | iAndrew5 <abyzhynar@luxoft.com> | 2015-12-17 14:47:23 +0200 |
---|---|---|
committer | iAndrew5 <abyzhynar@luxoft.com> | 2015-12-17 15:13:11 +0200 |
commit | 49bbdae3cff828bf95edb94bf7dc016d91be7216 (patch) | |
tree | 52b5b8ad900deaca5f9371fecafb9db2c156f9e5 /src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc | |
parent | 20bec5aeb327497d54e72db407fc42f4e575559b (diff) | |
download | sdl_core-49bbdae3cff828bf95edb94bf7dc016d91be7216.tar.gz |
hotfix/Data_race_in_ThreadedSocketConnection_Transmit
Duplicated code moved to separate method
Renamed local variable frames_to_send to frames_to_send_local to
avoid confusion between framaes_to_send and frames_to_send_
Diffstat (limited to 'src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc')
-rw-r--r-- | src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc b/src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc index cc476c745a..f520841a47 100644 --- a/src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc +++ b/src/components/transport_manager/src/transport_adapter/threaded_socket_connection.cc @@ -184,6 +184,12 @@ void ThreadedSocketConnection::threadMain() { } } +bool ThreadedSocketConnection::IsFramesToSendQueueEmpty() const { + // Check Frames queue is empty or not + sync_primitives::AutoLock auto_lock(frames_to_send_mutex_); + return frames_to_send_.empty(); +} + void ThreadedSocketConnection::Transmit() { LOG4CXX_AUTO_TRACE(logger_); @@ -191,15 +197,10 @@ void ThreadedSocketConnection::Transmit() { pollfd poll_fds[kPollFdsSize]; poll_fds[0].fd = socket_; - bool is_queue_empty = true; - { - // Check Frames queue is empty or not - sync_primitives::AutoLock auto_lock(frames_to_send_mutex_); - is_queue_empty = frames_to_send_.empty(); - } + const bool is_queue_empty_on_poll = IsFramesToSendQueueEmpty(); poll_fds[0].events = POLLIN | POLLPRI - | (is_queue_empty ? 0 : POLLOUT); + | (is_queue_empty_on_poll ? 0 : POLLOUT); poll_fds[1].fd = read_fd_; poll_fds[1].events = POLLIN | POLLPRI; @@ -240,11 +241,7 @@ void ThreadedSocketConnection::Transmit() { return; } - { - // Check Frames queue is empty or not - sync_primitives::AutoLock auto_lock(frames_to_send_mutex_); - is_queue_empty = frames_to_send_.empty(); - } + const bool is_queue_empty = IsFramesToSendQueueEmpty(); // Send data if possible if (!is_queue_empty && (poll_fds[0].revents | POLLOUT)) { @@ -303,17 +300,17 @@ bool ThreadedSocketConnection::Receive() { bool ThreadedSocketConnection::Send() { LOG4CXX_AUTO_TRACE(logger_); - FrameQueue frames_to_send; + FrameQueue frames_to_send_local; { sync_primitives::AutoLock auto_lock(frames_to_send_mutex_); - std::swap(frames_to_send, frames_to_send_); + std::swap(frames_to_send_local, frames_to_send_); } size_t offset = 0; - while (!frames_to_send.empty()) { + while (!frames_to_send_local.empty()) { LOG4CXX_INFO(logger_, "frames_to_send is not empty"); - ::protocol_handler::RawMessagePtr frame = frames_to_send.front(); + ::protocol_handler::RawMessagePtr frame = frames_to_send_local.front(); const ssize_t bytes_sent = ::send(socket_, frame->data() + offset, frame->data_size() - offset, 0); @@ -321,14 +318,14 @@ bool ThreadedSocketConnection::Send() { LOG4CXX_DEBUG(logger_, "bytes_sent >= 0"); offset += bytes_sent; if (offset == frame->data_size()) { - frames_to_send.pop(); + frames_to_send_local.pop(); offset = 0; controller_->DataSendDone(device_handle(), application_handle(), frame); } } else { LOG4CXX_DEBUG(logger_, "bytes_sent < 0"); LOG4CXX_ERROR_WITH_ERRNO(logger_, "Send failed for connection " << this); - frames_to_send.pop(); + frames_to_send_local.pop(); offset = 0; controller_->DataSendFailed(device_handle(), application_handle(), frame, DataSendError()); |