20#include <xrpld/core/detail/Workers.h>
21#include <xrpld/perflog/PerfLog.h>
23#include <xrpl/beast/core/CurrentThreadName.h>
24#include <xrpl/beast/utility/instrumentation.h>
33 : m_callback(callback)
35 , m_threadNames(threadNames)
38 , m_numberOfThreads(0)
41 , m_runningTaskCount(0)
66 static int instance{0};
78 for (
int i = 0; i < amount; ++i)
83 if (worker !=
nullptr)
102 for (
int i = 0; i < amount; ++i)
125 "ripple::Workers::stop : zero running tasks");
147 if (worker !=
nullptr)
166 , threadName_{threadName}
167 , instance_{instance}
182 wakeup_.notify_one();
191 wakeup_.notify_one();
197 bool shouldExit =
true;
203 if (++m_workers.m_activeCount == 1)
206 m_workers.m_allPaused =
false;
216 m_workers.m_semaphore.wait();
221 int pauseCount = m_workers.m_pauseCount.load();
226 pauseCount = --m_workers.m_pauseCount;
236 ++m_workers.m_pauseCount;
243 ++m_workers.m_runningTaskCount;
244 m_workers.m_callback.processTask(instance_);
245 --m_workers.m_runningTaskCount;
252 m_workers.m_paused.push_front(
this);
257 if (--m_workers.m_activeCount == 0)
260 m_workers.m_allPaused =
true;
261 m_workers.m_cv.notify_all();
275 wakeup_.wait(lock, [
this] {
return this->wakeCount_ > 0; });
277 shouldExit = shouldExit_;
280 }
while (!shouldExit);
Multiple Producer, Multiple Consumer (MPMC) intrusive stack.
Element * pop_front()
Pop an element off the stack.
Worker(Workers &workers, std::string const &threadName, int const instance)
Workers is effectively a thread pool.
Workers(Callback &callback, perf::PerfLog *perfLog, std::string const &threadNames="Worker", int numberOfThreads=static_cast< int >(std::thread::hardware_concurrency()))
Create the object.
std::condition_variable m_cv
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
static void deleteWorkers(beast::LockFreeStack< Worker > &stack)
beast::LockFreeStack< Worker, PausedTag > m_paused
int numberOfCurrentlyRunningTasks() const noexcept
Get the number of currently executing calls of Callback::processTask.
void addTask()
Add a task to be performed.
std::string m_threadNames
beast::LockFreeStack< Worker > m_everyone
std::atomic< int > m_pauseCount
void stop()
Pause all threads and wait until they are paused.
std::atomic< int > m_runningTaskCount
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
void notify()
Increment the count and unblock one waiting thread.
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
virtual void resizeJobs(int const resize)=0
Ensure enough room to store each currently executing job.
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Called to perform tasks as needed.