Skip to content

Commit ddd0acd

Browse files
committed
Create a scheduler thread for lightweight tasks
1 parent 68d370b commit ddd0acd

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

src/bitcoind.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "init.h"
99
#include "main.h"
1010
#include "noui.h"
11+
#include "scheduler.h"
1112
#include "util.h"
1213

1314
#include <boost/algorithm/string/predicate.hpp>
@@ -55,6 +56,7 @@ void WaitForShutdown(boost::thread_group* threadGroup)
5556
bool AppInit(int argc, char* argv[])
5657
{
5758
boost::thread_group threadGroup;
59+
CScheduler scheduler;
5860

5961
bool fRet = false;
6062

@@ -142,7 +144,7 @@ bool AppInit(int argc, char* argv[])
142144
#endif
143145
SoftSetBoolArg("-server", true);
144146

145-
fRet = AppInit2(threadGroup);
147+
fRet = AppInit2(threadGroup, scheduler);
146148
}
147149
catch (const std::exception& e) {
148150
PrintExceptionContinue(&e, "AppInit()");

src/init.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "net.h"
2020
#include "rpcserver.h"
2121
#include "script/standard.h"
22+
#include "scheduler.h"
2223
#include "txdb.h"
2324
#include "ui_interface.h"
2425
#include "util.h"
@@ -564,7 +565,7 @@ bool InitSanityCheck(void)
564565
/** Initialize bitcoin.
565566
* @pre Parameters should be parsed and config file should be read.
566567
*/
567-
bool AppInit2(boost::thread_group& threadGroup)
568+
bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
568569
{
569570
// ********************************************************* Step 1: setup
570571
#ifdef _MSC_VER
@@ -890,6 +891,10 @@ bool AppInit2(boost::thread_group& threadGroup)
890891
threadGroup.create_thread(&ThreadScriptCheck);
891892
}
892893

894+
// Start the lightweight task scheduler thread
895+
CScheduler::Function serviceLoop = boost::bind(&CScheduler::serviceQueue, &scheduler);
896+
threadGroup.create_thread(boost::bind(&TraceThread<CScheduler::Function>, "scheduler", serviceLoop));
897+
893898
/* Start the RPC server already. It will be started in "warmup" mode
894899
* and not really process calls already (but it will signify connections
895900
* that the server is there and will be ready later). Warmup mode will

src/init.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <string>
1010

11+
class CScheduler;
1112
class CWallet;
1213

1314
namespace boost
@@ -20,7 +21,7 @@ extern CWallet* pwalletMain;
2021
void StartShutdown();
2122
bool ShutdownRequested();
2223
void Shutdown();
23-
bool AppInit2(boost::thread_group& threadGroup);
24+
bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler);
2425

2526
/** The help message mode determines what help message to show */
2627
enum HelpMessageMode {

src/qt/bitcoin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "init.h"
2727
#include "main.h"
2828
#include "rpcserver.h"
29+
#include "scheduler.h"
2930
#include "ui_interface.h"
3031
#include "util.h"
3132

@@ -178,6 +179,7 @@ public slots:
178179

179180
private:
180181
boost::thread_group threadGroup;
182+
CScheduler scheduler;
181183

182184
/// Pass fatal exception message to UI thread
183185
void handleRunawayException(const std::exception *e);
@@ -258,7 +260,7 @@ void BitcoinCore::initialize()
258260
try
259261
{
260262
qDebug() << __func__ << ": Running AppInit2 in thread";
261-
int rv = AppInit2(threadGroup);
263+
int rv = AppInit2(threadGroup, scheduler);
262264
if(rv)
263265
{
264266
/* Start a dummy RPC thread if no RPC thread is active yet

0 commit comments

Comments
 (0)