Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/liblelantus/threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ParallelOpThreadPool {
// In case of shutdown thread list will be empty and destructor will wait for this thread completion
boost::thread::id currentId = boost::this_thread::get_id();
auto pThread = std::find_if(threads.begin(), threads.end(),
[=](const boost::thread &t) { return t.get_id() == currentId; });
[currentId](const boost::thread &t) { return t.get_id() == currentId; });
if (pThread != threads.end()) {
pThread->detach();
threads.erase(pThread);
Expand Down Expand Up @@ -79,7 +79,7 @@ class ParallelOpThreadPool {

// Post a task to the thread pool and return a future to wait for its completion
boost::future<Result> PostTask(std::function<Result()> task) {
boost::packaged_task<Result> packagedTask(std::move(task));
boost::packaged_task<Result> packagedTask(task);
boost::future<Result> ret = packagedTask.get_future();

boost::mutex::scoped_lock lock(task_queue_mutex);
Expand All @@ -91,7 +91,7 @@ class ParallelOpThreadPool {
task_queue.emplace(std::move(packagedTask));
task_queue_condition.notify_one();

return std::move(ret);
return ret;
}

int GetNumberOfThreads() const {
Expand Down Expand Up @@ -119,6 +119,11 @@ class ParallelOpThreadPool {
boost::mutex::scoped_lock lock(task_queue_mutex);
return shutdown;
}

std::size_t GetPendingTaskCount() {
boost::mutex::scoped_lock lock(task_queue_mutex);
return task_queue.size();
}
};


Expand Down
30 changes: 18 additions & 12 deletions src/spark/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct ProofCheckState {
static std::map<uint256, ProofCheckState> gCheckedSparkSpendTransactions;
static CCriticalSection cs_checkedSparkSpendTransactions;

static ParallelOpThreadPool<bool> gCheckProofThreadPool(boost::thread::hardware_concurrency());
static ParallelOpThreadPool<bool> gCheckProofThreadPool(std::min(boost::thread::hardware_concurrency(), 4u));

static CSparkState sparkState;

Expand Down Expand Up @@ -804,17 +804,23 @@ bool CheckSparkSpendTransaction(
}
else if (!fStatefulSigmaCheck && !gCheckProofThreadPool.IsPoolShutdown()) {
// not an urgent check, put the proof into the thread pool for verification
auto future = gCheckProofThreadPool.PostTask([spend, cover_sets]() {
try {
return spark::SpendTransaction::verify(*spend, cover_sets);
} catch (const std::exception &) {
return false;
}
});
auto &checkState = gCheckedSparkSpendTransactions[hashTx];
checkState.fChecked = false;
checkState.fResult = false;
checkState.checkInProgress = std::make_shared<boost::future<bool>>(std::move(future));
// don't post a request if there are too many tasks already
if (gCheckProofThreadPool.GetPendingTaskCount() < (std::size_t)gCheckProofThreadPool.GetNumberOfThreads()/2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just asking. I am not 100% sure. It wouldn't be better to have this:

if (gCheckProofThreadPool.GetPendingTaskCount() < ...) {
    gCheckProofThreadPool.PostTask(...);
}

as atomic operation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not that important right here, it's just an optimization, even if the number of tasks changes it won't cause any real damage

auto future = gCheckProofThreadPool.PostTask([spend, cover_sets]() mutable {
try {
bool result = spark::SpendTransaction::verify(*spend, cover_sets);
spend.reset();
cover_sets.clear();
return result;
} catch (const std::exception &) {
return false;
}
});
auto &checkState = gCheckedSparkSpendTransactions[hashTx];
checkState.fChecked = false;
checkState.fResult = false;
checkState.checkInProgress = std::make_shared<boost::future<bool>>(std::move(future));
}
}
}
while (fRecheckNeeded);
Expand Down
Loading