From 72bf90d770ce5b2653fd482928646cd6a9f5f6d7 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Mon, 15 Jun 2015 15:46:02 -0400 Subject: Fix scheduler build with some boost versions. Some boost versions have a conflicting overload of wait_until that returns void. Explicitly use a template here to avoid hitting that overload. --- src/scheduler.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/scheduler.cpp') diff --git a/src/scheduler.cpp b/src/scheduler.cpp index c42eb7244..d5bb588b7 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -50,8 +50,10 @@ void CScheduler::serviceQueue() // Keep waiting until timeout } #else + // Some boost versions have a conflicting overload of wait_until that returns void. + // Explicitly use a template here to avoid hitting that overload. while (!shouldStop() && !taskQueue.empty() && - newTaskScheduled.wait_until(lock, taskQueue.begin()->first) != boost::cv_status::timeout) { + newTaskScheduled.wait_until<>(lock, taskQueue.begin()->first) != boost::cv_status::timeout) { // Keep waiting until timeout } #endif -- cgit v1.2.3 From fb08d92312312c6c896327d264a8e9915fdafae7 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 17 Aug 2015 17:30:46 -0400 Subject: Make sure we re-acquire lock if a task throws --- src/scheduler.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/scheduler.cpp') diff --git a/src/scheduler.cpp b/src/scheduler.cpp index d5bb588b7..06115f561 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -6,6 +6,7 @@ #include #include +#include #include CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false) @@ -65,11 +66,12 @@ void CScheduler::serviceQueue() Function f = taskQueue.begin()->second; taskQueue.erase(taskQueue.begin()); - // Unlock before calling f, so it can reschedule itself or another task - // without deadlocking: - lock.unlock(); - f(); - lock.lock(); + { + // Unlock before calling f, so it can reschedule itself or another task + // without deadlocking: + boost::reverse_lock > rlock(lock); + f(); + } } catch (...) { --nThreadsServicingQueue; throw; -- cgit v1.2.3 From 86270c816411680c33a60adfa768c7a647fce08f Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 3 Sep 2015 12:53:00 -0400 Subject: Replace boost::reverse_lock with our own. --- src/scheduler.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/scheduler.cpp') diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 06115f561..184ddc28a 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -4,9 +4,10 @@ #include "scheduler.h" +#include "reverselock.h" + #include #include -#include #include CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false) @@ -69,7 +70,7 @@ void CScheduler::serviceQueue() { // Unlock before calling f, so it can reschedule itself or another task // without deadlocking: - boost::reverse_lock > rlock(lock); + reverse_lock > rlock(lock); f(); } } catch (...) { -- cgit v1.2.3 From 166e4b0dfa283fbdedc9a6a1e83296500c853a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Jan=C3=ADk?= Date: Fri, 6 May 2016 11:01:51 +0200 Subject: Notify other serviceQueue thread we are finished to prevent deadlocks. --- src/scheduler.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/scheduler.cpp') diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 184ddc28a..52777b61f 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -79,6 +79,7 @@ void CScheduler::serviceQueue() } } --nThreadsServicingQueue; + newTaskScheduled.notify_one(); } void CScheduler::stop(bool drain) -- cgit v1.2.3 From 12519bf62b8c49b1c1744eca6ea5b3162a61f962 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Fri, 18 Nov 2016 13:08:30 +0100 Subject: test: Fix use-after-free in scheduler tests Make a copy of the boost time-point to wait for, otherwise the head of the queue may be deleted by another thread while this one is waiting, while the boost function still has a reference to it. Although this problem is in non-test code, this is not an actual problem outside of the tests because we use the thread scheduler with only one service thread, so there will never be threads fighting at the head of the queue. The old boost fallback escapes this problem because it passes a scalar value to wait_until instead of a const object reference. Found by running the tests in LLVM-4.0-master asan. --- src/scheduler.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/scheduler.cpp') diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 52777b61f..27c03f154 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -54,9 +54,10 @@ void CScheduler::serviceQueue() #else // Some boost versions have a conflicting overload of wait_until that returns void. // Explicitly use a template here to avoid hitting that overload. - while (!shouldStop() && !taskQueue.empty() && - newTaskScheduled.wait_until<>(lock, taskQueue.begin()->first) != boost::cv_status::timeout) { - // Keep waiting until timeout + while (!shouldStop() && !taskQueue.empty()) { + boost::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first; + if (newTaskScheduled.wait_until<>(lock, timeToWaitFor) == boost::cv_status::timeout) + break; // Exit loop after timeout, it means we reached the time of the event } #endif // If there are multiple threads, the queue can empty while we're waiting (another -- cgit v1.2.3 From 27765b6403cece54320374b37afb01a0cfe571c3 Mon Sep 17 00:00:00 2001 From: isle2983 Date: Sat, 31 Dec 2016 11:01:21 -0700 Subject: Increment MIT Licence copyright header year on files modified in 2016 Edited via: $ contrib/devtools/copyright_header.py update . --- src/scheduler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/scheduler.cpp') diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 27c03f154..b01170074 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2015 The Bitcoin Core developers +// Copyright (c) 2015-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -- cgit v1.2.3