Skip to content

Commit

Permalink
Replace the boost::condition_variable used in fc's task scheduler with
Browse files Browse the repository at this point in the history
a std::condition_variable.  The boost::condition_variable's wait_for/
wait_until methods misbehaved when run under
`faketime --exclude-monotonic`, which we are using in our CI tests.
That would cause any thread with a task scheduled in the future to
busy-wait for the scheduled time to arrive instead of sleeping.
std::condition_variables don't have this problem.
  • Loading branch information
emfrias committed Mar 7, 2024
1 parent 81aa892 commit 9996da7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions libraries/fc/src/thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ namespace fc {
}

void thread::poke() {
boost::unique_lock<boost::mutex> lock(my->task_ready_mutex);
std::unique_lock<std::mutex> lock(my->task_ready_mutex);
my->task_ready.notify_one();
}

Expand All @@ -364,7 +364,7 @@ namespace fc {
// to aquire the lock and therefore there should be no contention on this lock except
// when *this thread is about to block on a wait condition.
if( this != &current() && !stale_head ) {
boost::unique_lock<boost::mutex> lock(my->task_ready_mutex);
std::unique_lock<std::mutex> lock(my->task_ready_mutex);
my->task_ready.notify_one();
}
}
Expand Down
12 changes: 6 additions & 6 deletions libraries/fc/src/thread/thread_d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <fc/time.hpp>
#include <boost/thread.hpp>
#include "context.hpp"
#include <boost/thread/condition_variable.hpp>
#include <condition_variable>
#include <boost/thread.hpp>
#include <boost/atomic.hpp>
#include <vector>
Expand Down Expand Up @@ -75,8 +75,8 @@ namespace fc {
fc::thread& self;
boost::thread* boost_thread;
stack_allocator stack_alloc;
boost::condition_variable task_ready;
boost::mutex task_ready_mutex;
std::condition_variable task_ready;
std::mutex task_ready_mutex;

boost::atomic<task_base*> task_in_queue;
std::vector<task_base*> task_pqueue; // heap of tasks that have never started, ordered by proirity & scheduling time
Expand Down Expand Up @@ -582,7 +582,7 @@ namespace fc {
clear_free_list();

{ // lock scope
boost::unique_lock<boost::mutex> lock(task_ready_mutex);
std::unique_lock<std::mutex> lock(task_ready_mutex);
if( has_next_task() )
continue;
time_point timeout_time = check_for_timeouts();
Expand Down Expand Up @@ -613,8 +613,8 @@ namespace fc {
* that takes an absolute time like fc::promise::wait_until(), so we can't always
* do the right thing here.
*/
task_ready.wait_until( lock, boost::chrono::steady_clock::now() +
boost::chrono::microseconds(timeout_time.time_since_epoch().count() - time_point::now().time_since_epoch().count()) );
task_ready.wait_until( lock, std::chrono::steady_clock::now() +
std::chrono::microseconds(timeout_time.time_since_epoch().count() - time_point::now().time_since_epoch().count()) );
}
}
}
Expand Down

0 comments on commit 9996da7

Please sign in to comment.