From 84ecbaff9e2540d06a5c1fccf547a528158dd0b1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 3 May 2015 22:50:21 +0100 Subject: [PATCH] Condition variables rather than polling FTW. --- libethereum/Client.cpp | 7 ++++--- libethereum/Client.h | 7 +++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index befebd765..e8ab0ff2f 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -658,8 +658,6 @@ void Client::noteChanged(h256Set const& _filters) void Client::doWork() { - // TODO: Use condition variable rather than this rubbish. - bool t = true; if (m_syncBlockQueue.compare_exchange_strong(t, false)) syncBlockQueue(); @@ -671,7 +669,10 @@ void Client::doWork() tick(); if (!m_syncBlockQueue && !m_syncTransactionQueue) - this_thread::sleep_for(chrono::milliseconds(20)); + { + std::unique_lock l(x_signalled); + m_signalled.wait_for(l, chrono::seconds(1)); + } } void Client::tick() diff --git a/libethereum/Client.h b/libethereum/Client.h index 90273968c..946825828 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -22,6 +22,7 @@ #pragma once #include +#include #include #include #include @@ -258,10 +259,10 @@ private: void syncTransactionQueue(); /// Magically called when m_tq needs syncing. Be nice and don't block. - void onTransactionQueueReady() { m_syncTransactionQueue = true; } + void onTransactionQueueReady() { m_syncTransactionQueue = true; m_signalled.notify_all(); } /// Magically called when m_tq needs syncing. Be nice and don't block. - void onBlockQueueReady() { m_syncBlockQueue = true; } + void onBlockQueueReady() { m_syncBlockQueue = true; m_signalled.notify_all(); } /// Called when the post state has changed (i.e. when more transactions are in it or we're mining on a new block). /// This updates m_miningInfo. @@ -309,6 +310,8 @@ private: ActivityReport m_report; // TODO!!!!!! REPLACE WITH A PROPER X-THREAD ASIO SIGNAL SYSTEM (could just be condition variables) + std::condition_variable m_signalled; + Mutex x_signalled; std::atomic m_syncTransactionQueue = {false}; std::atomic m_syncBlockQueue = {false}; };