Skip to content

Commit

Permalink
Wednothd Connection: reimplement wait_and_receive_data with std::cond…
Browse files Browse the repository at this point in the history
…itional_variable
  • Loading branch information
Vultraz committed Aug 6, 2019
1 parent bced41a commit 115cf7d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/wesnothd_connection.cpp
Expand Up @@ -66,6 +66,7 @@ wesnothd_connection::wesnothd_connection(const std::string& host, const std::str
, handshake_response_()
, recv_queue_()
, recv_queue_mutex_()
, recv_queue_lock_()
, payload_size_(0)
, bytes_to_write_(0)
, bytes_written_(0)
Expand Down Expand Up @@ -357,6 +358,7 @@ void wesnothd_connection::handle_read(const boost::system::error_code& ec, std::
{
std::lock_guard<std::mutex> lock(recv_queue_mutex_);
recv_queue_.emplace(std::move(data));
recv_queue_lock_.notify_all();
}

recv();
Expand Down Expand Up @@ -425,12 +427,9 @@ bool wesnothd_connection::receive_data(config& result)

bool wesnothd_connection::wait_and_receive_data(config& data)
{
while(!has_data_received()) {
SDL_Delay(1);
std::lock_guard<std::mutex> lock(last_error_mutex_);
if(last_error_) {
break;
}
{
std::unique_lock<std::mutex> lock(recv_queue_mutex_);
recv_queue_lock_.wait(lock, [this]() { return has_data_received(); });
}

return receive_data(data);
Expand Down
3 changes: 3 additions & 0 deletions src/wesnothd_connection.hpp
Expand Up @@ -35,6 +35,7 @@

#include <boost/asio.hpp>

#include <condition_variable>
#include <deque>
#include <future>
#include <list>
Expand Down Expand Up @@ -165,6 +166,8 @@ class wesnothd_connection

std::mutex recv_queue_mutex_;

std::condition_variable recv_queue_lock_;

uint32_t payload_size_;

// TODO: do i need to guard the following 4 values with a mutex?
Expand Down

0 comments on commit 115cf7d

Please sign in to comment.