20#include <xrpld/core/detail/Workers.h>
21#include <xrpld/perflog/PerfLog.h>
22#include <xrpl/beast/core/CurrentThreadName.h>
23#include <xrpl/beast/utility/instrumentation.h>
32 : m_callback(callback)
34 , m_threadNames(threadNames)
37 , m_numberOfThreads(0)
40 , m_runningTaskCount(0)
65 static int instance{0};
77 for (
int i = 0; i < amount; ++i)
82 if (worker !=
nullptr)
101 for (
int i = 0; i < amount; ++i)
124 "ripple::Workers::stop : zero running tasks");
146 if (worker !=
nullptr)
165 , threadName_{threadName}
166 , instance_{instance}
181 wakeup_.notify_one();
190 wakeup_.notify_one();
196 bool shouldExit =
true;
202 if (++m_workers.m_activeCount == 1)
205 m_workers.m_allPaused =
false;
215 m_workers.m_semaphore.wait();
220 int pauseCount = m_workers.m_pauseCount.load();
225 pauseCount = --m_workers.m_pauseCount;
235 ++m_workers.m_pauseCount;
242 ++m_workers.m_runningTaskCount;
243 m_workers.m_callback.processTask(instance_);
244 --m_workers.m_runningTaskCount;
251 m_workers.m_paused.push_front(
this);
256 if (--m_workers.m_activeCount == 0)
259 m_workers.m_allPaused =
true;
260 m_workers.m_cv.notify_all();
274 wakeup_.wait(lock, [
this] {
return this->wakeCount_ > 0; });
276 shouldExit = shouldExit_;
279 }
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.