xrpld
Loading...
Searching...
No Matches
Workers.h
1#pragma once
2
3#include <xrpl/beast/core/LockFreeStack.h>
4#include <xrpl/core/detail/semaphore.h>
5
6#include <atomic>
7#include <condition_variable>
8#include <mutex>
9#include <string>
10#include <thread>
11
12namespace xrpl {
13
14namespace perf {
15class PerfLog;
16} // namespace perf
17
61{
62public:
66 struct Callback
67 {
68 virtual ~Callback() = default;
69 Callback() = default;
70 Callback(Callback const&) = delete;
72 operator=(Callback const&) = delete;
73
85 virtual void
86 processTask(int instance) = 0;
87 };
88
97 explicit Workers(
98 Callback& callback,
99 perf::PerfLog* perfLog,
100 std::string threadNames = "Worker",
101 int numberOfThreads = static_cast<int>(std::thread::hardware_concurrency()));
102
103 ~Workers();
104
114 [[nodiscard]] int
115 getNumberOfThreads() const noexcept;
116
121 void
122 setNumberOfThreads(int numberOfThreads);
123
133 void
134 stop();
135
145 void
146 addTask();
147
153 [[nodiscard]] int
154 numberOfCurrentlyRunningTasks() const noexcept;
155
156 //--------------------------------------------------------------------------
157
158private:
160 {
161 explicit PausedTag() = default;
162 };
163
164 /* A Worker executes tasks on its provided thread.
165
166 These are the states:
167
168 Active: Running the task processing loop.
169 Idle: Active, but blocked on waiting for a task.
170 Paused: Blocked waiting to exit or become active.
171 */
172 class Worker : public beast::LockFreeStack<Worker>::Node,
173 public beast::LockFreeStack<Worker, PausedTag>::Node
174 {
175 public:
176 Worker(Workers& workers, std::string threadName, int const instance);
177
178 ~Worker();
179
180 void
181 notify();
182
183 private:
184 void
185 run();
186
187 private:
190 int const instance_;
191
195 int wakeCount_{0}; // how many times to un-pause
196 bool shouldExit_{false};
197 };
198
199private:
200 static void
202
203private:
206 std::string threadNames_; // The name to give each thread
207 std::condition_variable cv_; // signaled when all threads paused
209 bool allPaused_{true};
210 semaphore semaphore_; // each pending task is 1 resource
211 int numberOfThreads_{0}; // how many we want active now
212 std::atomic<int> activeCount_; // to know when all are paused
213 std::atomic<int> pauseCount_; // how many threads need to pause now
214 std::atomic<int> runningTaskCount_; // how many calls to processTask() active
215 beast::LockFreeStack<Worker> everyone_; // holds all created workers
217};
218
219} // namespace xrpl
Multiple Producer, Multiple Consumer (MPMC) intrusive stack.
std::string const threadName_
Definition Workers.h:189
std::thread thread_
Definition Workers.h:192
std::mutex mutex_
Definition Workers.h:193
int const instance_
Definition Workers.h:190
Worker(Workers &workers, std::string threadName, int const instance)
Definition Workers.cpp:140
std::condition_variable wakeup_
Definition Workers.h:194
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Definition Workers.cpp:47
void stop()
Pause all threads and wait until they are paused.
Definition Workers.cpp:98
int numberOfThreads_
Definition Workers.h:211
std::atomic< int > activeCount_
Definition Workers.h:212
std::mutex mut_
Definition Workers.h:208
beast::LockFreeStack< Worker, PausedTag > paused_
Definition Workers.h:216
int numberOfCurrentlyRunningTasks() const noexcept
Get the number of currently executing calls of Callback::processTask.
Definition Workers.cpp:118
std::atomic< int > pauseCount_
Definition Workers.h:213
Callback & callback_
Definition Workers.h:204
void addTask()
Add a task to be performed.
Definition Workers.cpp:112
Workers(Callback &callback, perf::PerfLog *perfLog, std::string threadNames="Worker", int numberOfThreads=static_cast< int >(std::thread::hardware_concurrency()))
Create the object.
Definition Workers.cpp:13
perf::PerfLog * perfLog_
Definition Workers.h:205
static void deleteWorkers(beast::LockFreeStack< Worker > &stack)
Definition Workers.cpp:124
std::string threadNames_
Definition Workers.h:206
semaphore semaphore_
Definition Workers.h:210
bool allPaused_
Definition Workers.h:209
std::condition_variable cv_
Definition Workers.h:207
std::atomic< int > runningTaskCount_
Definition Workers.h:214
beast::LockFreeStack< Worker > everyone_
Definition Workers.h:215
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition Workers.cpp:37
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition PerfLog.h:31
T hardware_concurrency(T... args)
Dummy class for unit tests.
Definition Workers.h:14
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BasicSemaphore< std::mutex, std::condition_variable > semaphore
Definition semaphore.h:94
Called to perform tasks as needed.
Definition Workers.h:67
Callback(Callback const &)=delete
virtual void processTask(int instance)=0
Perform a task.
Callback & operator=(Callback const &)=delete
virtual ~Callback()=default