Changeset View
Changeset View
Standalone View
Standalone View
src/eventloop.cpp
// Copyright (c) 2020 The Bitcoin developers | // Copyright (c) 2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <eventloop.h> | #include <eventloop.h> | ||||
#include <scheduler.h> | #include <scheduler.h> | ||||
EventLoop::~EventLoop() { | EventLoop::~EventLoop() { | ||||
stopEventLoop(); | stopEventLoop(); | ||||
} | } | ||||
bool EventLoop::startEventLoop(CScheduler &scheduler, | bool EventLoop::startEventLoop(CScheduler &scheduler, | ||||
std::function<void()> runEventLoop, | std::function<void()> runEventLoop, | ||||
int64_t deltaMilliSeconds) { | std::chrono::milliseconds delta) { | ||||
LOCK(cs_running); | LOCK(cs_running); | ||||
if (running) { | if (running) { | ||||
// Do not start the event loop twice. | // Do not start the event loop twice. | ||||
return false; | return false; | ||||
} | } | ||||
running = true; | running = true; | ||||
// Start the event loop. | // Start the event loop. | ||||
scheduler.scheduleEvery( | scheduler.scheduleEvery( | ||||
[this, runEventLoop]() -> bool { | [this, runEventLoop]() -> bool { | ||||
runEventLoop(); | runEventLoop(); | ||||
if (!stopRequest) { | if (!stopRequest) { | ||||
return true; | return true; | ||||
} | } | ||||
LOCK(cs_running); | LOCK(cs_running); | ||||
running = false; | running = false; | ||||
cond_running.notify_all(); | cond_running.notify_all(); | ||||
// A stop request was made. | // A stop request was made. | ||||
return false; | return false; | ||||
}, | }, | ||||
deltaMilliSeconds); | delta); | ||||
return true; | return true; | ||||
} | } | ||||
bool EventLoop::stopEventLoop() { | bool EventLoop::stopEventLoop() { | ||||
WAIT_LOCK(cs_running, lock); | WAIT_LOCK(cs_running, lock); | ||||
if (!running) { | if (!running) { | ||||
return false; | return false; | ||||
Show All 13 Lines |