20 #include <ripple/basics/PerfLog.h>
21 #include <ripple/beast/core/CurrentThreadName.h>
22 #include <ripple/core/impl/Workers.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)
144 if (worker !=
nullptr)
163 , threadName_{threadName}
164 , instance_{instance}
179 wakeup_.notify_one();
188 wakeup_.notify_one();
194 bool shouldExit =
true;
200 if (++m_workers.m_activeCount == 1)
203 m_workers.m_allPaused =
false;
213 m_workers.m_semaphore.wait();
218 int pauseCount = m_workers.m_pauseCount.load();
223 pauseCount = --m_workers.m_pauseCount;
233 ++m_workers.m_pauseCount;
240 ++m_workers.m_runningTaskCount;
241 m_workers.m_callback.processTask(instance_);
242 --m_workers.m_runningTaskCount;
249 m_workers.m_paused.push_front(
this);
254 if (--m_workers.m_activeCount == 0)
257 m_workers.m_allPaused =
true;
258 m_workers.m_cv.notify_all();
272 wakeup_.wait(lock, [
this] {
return this->wakeCount_ > 0; });
274 shouldExit = shouldExit_;
277 }
while (!shouldExit);
std::condition_variable m_cv
void stop()
Pause all threads and wait until they are paused.
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.
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.
Workers is effectively a thread pool.
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.