rippled
Loading...
Searching...
No Matches
Workers.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_CORE_WORKERS_H_INCLUDED
21#define RIPPLE_CORE_WORKERS_H_INCLUDED
22
23#include <xrpld/core/detail/semaphore.h>
24#include <xrpl/beast/core/LockFreeStack.h>
25#include <atomic>
26#include <condition_variable>
27#include <mutex>
28#include <string>
29#include <thread>
30
31namespace ripple {
32
33namespace perf {
34class PerfLog;
35}
36
80{
81public:
83 struct Callback
84 {
85 virtual ~Callback() = default;
86 Callback() = default;
87 Callback(Callback const&) = delete;
89 operator=(Callback const&) = delete;
90
101 virtual void
102 processTask(int instance) = 0;
103 };
104
112 explicit Workers(
113 Callback& callback,
114 perf::PerfLog* perfLog,
115 std::string const& threadNames = "Worker",
116 int numberOfThreads =
117 static_cast<int>(std::thread::hardware_concurrency()));
118
119 ~Workers();
120
129 int
130 getNumberOfThreads() const noexcept;
131
135 void
136 setNumberOfThreads(int numberOfThreads);
137
146 void
147 stop();
148
157 void
158 addTask();
159
164 int
165 numberOfCurrentlyRunningTasks() const noexcept;
166
167 //--------------------------------------------------------------------------
168
169private:
171 {
172 explicit PausedTag() = default;
173 };
174
175 /* A Worker executes tasks on its provided thread.
176
177 These are the states:
178
179 Active: Running the task processing loop.
180 Idle: Active, but blocked on waiting for a task.
181 Paused: Blocked waiting to exit or become active.
182 */
183 class Worker : public beast::LockFreeStack<Worker>::Node,
184 public beast::LockFreeStack<Worker, PausedTag>::Node
185 {
186 public:
187 Worker(
188 Workers& workers,
189 std::string const& threadName,
190 int const instance);
191
192 ~Worker();
193
194 void
195 notify();
196
197 private:
198 void
199 run();
200
201 private:
204 int const instance_;
205
209 int wakeCount_; // how many times to un-pause
211 };
212
213private:
214 static void
216
217private:
220 std::string m_threadNames; // The name to give each thread
221 std::condition_variable m_cv; // signaled when all threads paused
224 semaphore m_semaphore; // each pending task is 1 resource
225 int m_numberOfThreads; // how many we want active now
226 std::atomic<int> m_activeCount; // to know when all are paused
227 std::atomic<int> m_pauseCount; // how many threads need to pause now
229 m_runningTaskCount; // how many calls to processTask() active
230 beast::LockFreeStack<Worker> m_everyone; // holds all created workers
232 m_paused; // holds just paused workers
233};
234
235} // namespace ripple
236
237#endif
Multiple Producer, Multiple Consumer (MPMC) intrusive stack.
std::string const threadName_
Definition: Workers.h:203
std::thread thread_
Definition: Workers.h:206
std::condition_variable wakeup_
Definition: Workers.h:208
Workers is effectively a thread pool.
Definition: Workers.h:80
std::mutex m_mut
Definition: Workers.h:222
std::condition_variable m_cv
Definition: Workers.h:221
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition: Workers.cpp:53
Callback & m_callback
Definition: Workers.h:218
static void deleteWorkers(beast::LockFreeStack< Worker > &stack)
Definition: Workers.cpp:140
beast::LockFreeStack< Worker, PausedTag > m_paused
Definition: Workers.h:232
int numberOfCurrentlyRunningTasks() const noexcept
Get the number of currently executing calls of Callback::processTask.
Definition: Workers.cpp:134
void addTask()
Add a task to be performed.
Definition: Workers.cpp:128
int m_numberOfThreads
Definition: Workers.h:225
std::string m_threadNames
Definition: Workers.h:220
beast::LockFreeStack< Worker > m_everyone
Definition: Workers.h:230
bool m_allPaused
Definition: Workers.h:223
std::atomic< int > m_activeCount
Definition: Workers.h:226
std::atomic< int > m_pauseCount
Definition: Workers.h:227
void stop()
Pause all threads and wait until they are paused.
Definition: Workers.cpp:114
perf::PerfLog * perfLog_
Definition: Workers.h:219
std::atomic< int > m_runningTaskCount
Definition: Workers.h:229
semaphore m_semaphore
Definition: Workers.h:224
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Definition: Workers.cpp:63
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition: PerfLog.h:49
T hardware_concurrency(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
Called to perform tasks as needed.
Definition: Workers.h:84
virtual ~Callback()=default
Callback & operator=(Callback const &)=delete
virtual void processTask(int instance)=0
Perform a task.
Callback(Callback const &)=delete