Repository navigation
Expand file tree
/
Copy pathWorkerPool.h
More file actions
159 lines (136 loc) · 6.01 KB
/
Copy pathWorkerPool.h
File metadata and controls
159 lines (136 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#include <atomic>
#include <cstdint>
#include <functional>
#include <memory>
#include "Disruptor/BlockingWaitStrategy.h"
#include "Disruptor/InvalidOperationException.h"
#include "Disruptor/IExecutor.h"
#include "Disruptor/RingBuffer.h"
#include "Disruptor/Sequence.h"
#include "Disruptor/WorkProcessor.h"
namespace Disruptor
{
/**
* WorkerPool contains a pool of WorkProcessor<T> that will consume sequences so jobs can be farmed out across a pool of workers. Each of the WorkProcessor<T>
* manage and calls a IWorkHandler<T> to process the events.
*
* \tparam T event to be processed by a pool of workers
*/
template <class T>
class WorkerPool
{
public:
/**
* Create a worker pool to enable an array of IWorkHandler<T> to consume published sequences. This option requires a pre-configured RingBuffer<T> which must have
* Sequencer::setGatingSequences() called before the work pool is started.
*
* \param ringBuffer ringBuffer of events to be consumed.
* \param sequenceBarrier sequenceBarrier on which the workers will depend.
* \param exceptionHandler exceptionHandler to callback when an error occurs which is not handled by the<see cref="IWorkHandler{T}"/>s.
* \param workHandlers workHandlers to distribute the work load across.
*/
WorkerPool(const std::shared_ptr< RingBuffer< T > >& ringBuffer,
const std::shared_ptr< ISequenceBarrier >& sequenceBarrier,
const std::shared_ptr< IExceptionHandler< T > >& exceptionHandler,
const std::vector< std::shared_ptr< IWorkHandler< T > > >& workHandlers)
{
m_ringBuffer = ringBuffer;
m_workProcessors.resize(workHandlers.size());
for (auto i = 0u; i < workHandlers.size(); ++i)
{
m_workProcessors[i] = WorkProcessor< T >::create(ringBuffer, sequenceBarrier, workHandlers[i], exceptionHandler, m_workSequence);
}
}
/**
* Construct a work pool with an internal RingBuffer<T> for convenience. This option does not require Sequencer::setGatingSequences() to be called before the work pool is started.
*
* \param eventFactory eventFactory for filling the<see cref="RingBuffer{T}"/>
* \param exceptionHandler exceptionHandler to callback when an error occurs which is not handled by the<see cref="IWorkHandler{T}"/>s.
* \param workHandlers workHandlers to distribute the work load across.
*/
WorkerPool(const std::function< T() >& eventFactory,
const std::shared_ptr< IExceptionHandler< T > >& exceptionHandler,
const std::vector< std::shared_ptr< IWorkHandler< T > > >& workHandlers)
{
m_ringBuffer = RingBuffer< T >::createMultiProducer(eventFactory, 1024, std::make_shared< BlockingWaitStrategy >());
auto barrier = m_ringBuffer->newBarrier();
m_workProcessors.resize(workHandlers.size());
for (auto i = 0u; i < workHandlers.size(); ++i)
{
m_workProcessors[i] = WorkProcessor< T >::create(m_ringBuffer, barrier, workHandlers[i], exceptionHandler, m_workSequence);
}
m_ringBuffer->addGatingSequences(getWorkerSequences());
}
/**
* Get an array of Sequences representing the progress of the workers.
*/
std::vector< std::shared_ptr< ISequence > > getWorkerSequences()
{
std::vector< std::shared_ptr< ISequence > > sequences(m_workProcessors.size() + 1);
for (auto i = 0u; i < m_workProcessors.size(); ++i)
{
sequences[i] = m_workProcessors[i]->sequence();
}
sequences[sequences.size() - 1] = m_workSequence;
return sequences;
}
/**
* Start the worker pool processing events in sequence.
*
* \returns the RingBuffer<T> used for the work queue.
*/
std::shared_ptr< RingBuffer< T > > start(const std::shared_ptr< IExecutor >& executor)
{
if (std::atomic_exchange(&m_running, 1) != 0)
{
DISRUPTOR_THROW_INVALID_OPERATION_EXCEPTION("WorkerPool has already been started and cannot be restarted until halted");
}
auto cursor = m_ringBuffer->cursor();
m_workSequence->setValue(cursor);
for (auto&& workProcessor : m_workProcessors)
{
workProcessor->sequence()->setValue(cursor);
executor->execute([workProcessor] { workProcessor->run(); });
}
return m_ringBuffer;
}
/**
* Wait for the RingBuffer<T> to drain of published events then halt the workers.
*/
void drainAndHalt()
{
auto workerSequences = getWorkerSequences();
while (m_ringBuffer->cursor() > Util::getMinimumSequence(workerSequences))
{
std::this_thread::sleep_for(std::chrono::milliseconds(0));
}
for (auto&& workProcessor : m_workProcessors)
{
workProcessor->halt();
}
m_running = 0;
}
/**
* Halt all workers immediately at then end of their current cycle.
*/
void halt()
{
for (auto&& workProcessor : m_workProcessors)
{
workProcessor->halt();
}
m_running = 0;
}
bool isRunning() const
{
return m_running == 1;
}
private:
std::atomic< std::int32_t > m_running {0};
std::shared_ptr< Sequence > m_workSequence = std::make_shared< Sequence >();
std::shared_ptr< RingBuffer< T > > m_ringBuffer;
// WorkProcessors are created to wrap each of the provided WorkHandlers
std::vector< std::shared_ptr< WorkProcessor< T > > > m_workProcessors;
};
} // namespace Disruptor