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
25#include <xrpl/beast/core/LockFreeStack.h>
26
27#include <atomic>
28#include <condition_variable>
29#include <mutex>
30#include <string>
31#include <thread>
32
33namespace ripple {
34
35namespace perf {
36class PerfLog;
37}
38
82{
83public:
85 struct Callback
86 {
87 virtual ~Callback() = default;
88 Callback() = default;
89 Callback(Callback const&) = delete;
91 operator=(Callback const&) = delete;
92
103 virtual void
104 processTask(int instance) = 0;
105 };
106
114 explicit Workers(
115 Callback& callback,
116 perf::PerfLog* perfLog,
117 std::string const& threadNames = "Worker",
118 int numberOfThreads =
119 static_cast<int>(std::thread::hardware_concurrency()));
120
121 ~Workers();
122
131 int
132 getNumberOfThreads() const noexcept;
133
137 void
138 setNumberOfThreads(int numberOfThreads);
139
148 void
149 stop();
150
159 void
160 addTask();
161
166 int
167 numberOfCurrentlyRunningTasks() const noexcept;
168
169 //--------------------------------------------------------------------------
170
171private:
173 {
174 explicit PausedTag() = default;
175 };
176
177 /* A Worker executes tasks on its provided thread.
178
179 These are the states:
180
181 Active: Running the task processing loop.
182 Idle: Active, but blocked on waiting for a task.
183 Paused: Blocked waiting to exit or become active.
184 */
185 class Worker : public beast::LockFreeStack<Worker>::Node,
186 public beast::LockFreeStack<Worker, PausedTag>::Node
187 {
188 public:
189 Worker(
190 Workers& workers,
191 std::string const& threadName,
192 int const instance);
193
194 ~Worker();
195
196 void
197 notify();
198
199 private:
200 void
201 run();
202
203 private:
206 int const instance_;
207
211 int wakeCount_; // how many times to un-pause
213 };
214
215private:
216 static void
218
219private:
222 std::string m_threadNames; // The name to give each thread
223 std::condition_variable m_cv; // signaled when all threads paused
226 semaphore m_semaphore; // each pending task is 1 resource
227 int m_numberOfThreads; // how many we want active now
228 std::atomic<int> m_activeCount; // to know when all are paused
229 std::atomic<int> m_pauseCount; // how many threads need to pause now
231 m_runningTaskCount; // how many calls to processTask() active
232 beast::LockFreeStack<Worker> m_everyone; // holds all created workers
234 m_paused; // holds just paused workers
235};
236
237} // namespace ripple
238
239#endif
Multiple Producer, Multiple Consumer (MPMC) intrusive stack.
std::string const threadName_
Definition Workers.h:205
std::thread thread_
Definition Workers.h:208
std::condition_variable wakeup_
Definition Workers.h:210
Workers is effectively a thread pool.
Definition Workers.h:82
std::mutex m_mut
Definition Workers.h:224
std::condition_variable m_cv
Definition Workers.h:223
int getNumberOfThreads() const noexcept
Retrieve the desired number of threads.
Definition Workers.cpp:54
Callback & m_callback
Definition Workers.h:220
static void deleteWorkers(beast::LockFreeStack< Worker > &stack)
Definition Workers.cpp:141
beast::LockFreeStack< Worker, PausedTag > m_paused
Definition Workers.h:234
int numberOfCurrentlyRunningTasks() const noexcept
Get the number of currently executing calls of Callback::processTask.
Definition Workers.cpp:135
void addTask()
Add a task to be performed.
Definition Workers.cpp:129
int m_numberOfThreads
Definition Workers.h:227
std::string m_threadNames
Definition Workers.h:222
beast::LockFreeStack< Worker > m_everyone
Definition Workers.h:232
std::atomic< int > m_activeCount
Definition Workers.h:228
std::atomic< int > m_pauseCount
Definition Workers.h:229
void stop()
Pause all threads and wait until they are paused.
Definition Workers.cpp:115
perf::PerfLog * perfLog_
Definition Workers.h:221
std::atomic< int > m_runningTaskCount
Definition Workers.h:231
semaphore m_semaphore
Definition Workers.h:226
void setNumberOfThreads(int numberOfThreads)
Set the desired number of threads.
Definition Workers.cpp:64
Singleton class that maintains performance counters and optionally writes Json-formatted data to a di...
Definition PerfLog.h:52
T hardware_concurrency(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Called to perform tasks as needed.
Definition Workers.h:86
virtual ~Callback()=default
Callback & operator=(Callback const &)=delete
virtual void processTask(int instance)=0
Perform a task.
Callback(Callback const &)=delete