20 #include <ripple/core/impl/Workers.h>
21 #include <ripple/basics/PerfLog.h>
22 #include <ripple/beast/core/CurrentThreadName.h>
32 : m_callback (callback)
34 , m_threadNames (threadNames)
37 , m_numberOfThreads (0)
40 , m_runningTaskCount (0)
63 static int instance {0};
74 for (
int i = 0; i < amount; ++i)
79 if (worker !=
nullptr)
98 for (
int i = 0; i < amount; ++i)
138 if (worker !=
nullptr)
154 : m_workers {workers}
155 , threadName_ {threadName}
156 , instance_ {instance}
158 , shouldExit_ {
false}
171 wakeup_.notify_one();
179 wakeup_.notify_one();
184 bool shouldExit =
true;
190 if (++m_workers.m_activeCount == 1)
193 m_workers.m_allPaused =
false;
203 m_workers.m_semaphore.wait ();
208 int pauseCount = m_workers.m_pauseCount.load ();
213 pauseCount = --m_workers.m_pauseCount;
223 ++m_workers.m_pauseCount;
230 ++m_workers.m_runningTaskCount;
231 m_workers.m_callback.processTask (instance_);
232 --m_workers.m_runningTaskCount;
239 m_workers.m_paused.push_front (
this);
244 if (--m_workers.m_activeCount == 0)
247 m_workers.m_allPaused =
true;
248 m_workers.m_cv.notify_all();
262 wakeup_.wait (lock, [
this] {
return this->wakeCount_ > 0;});
264 shouldExit = shouldExit_;
267 }
while (! shouldExit);
std::condition_variable m_cv
static void deleteWorkers(beast::LockFreeStack< Worker > &stack)
std::atomic< int > m_pauseCount
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
beast::LockFreeStack< Worker, PausedTag > m_paused
beast::LockFreeStack< Worker > m_everyone
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Worker(Workers &workers, std::string const &threadName, int const instance)
Called to perform tasks as needed.
std::string m_threadNames
std::atomic< int > m_runningTaskCount
virtual void resizeJobs(int const resize)=0
Ensure enough room to store each currently executing job.
void pauseAllThreadsAndWait()
Pause all threads and wait until they are paused.
Workers(Callback &callback, perf::PerfLog *perfLog, std::string const &threadNames="Worker", int numberOfThreads=static_cast< int >(std::thread::hardware_concurrency()))
Create the object.
void notify()
Increment the count and unblock one waiting thread.
A group of threads that process tasks.
Element * pop_front()
Pop an element off the stack.
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void addTask()
Add a task to be performed.
int numberOfCurrentlyRunningTasks() const noexcept
Get the number of currently executing calls of Callback::processTask.
Multiple Producer, Multiple Consumer (MPMC) intrusive stack.