forked from Abc-Arbitrage/Disruptor-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleProducerSequencer.h
More file actions
228 lines (192 loc) · 7.62 KB
/
SingleProducerSequencer.h
File metadata and controls
228 lines (192 loc) · 7.62 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
#include <atomic>
#include "Disruptor/InsufficientCapacityException.h"
#include "Disruptor/IWaitStrategy.h"
#include "Disruptor/Sequencer.h"
#include "Disruptor/SpinWait.h"
#include "Disruptor/Util.h"
namespace Disruptor
{
template <class T>
class SingleProducerSequencer : public Sequencer< T >
{
struct Fields
{
char padding0[56];
std::int64_t nextValue;
std::int64_t cachedValue;
char padding1[56];
Fields(std::int64_t nextValue, std::int64_t cachedValue)
: nextValue(nextValue)
, cachedValue(cachedValue)
{}
};
public:
SingleProducerSequencer(std::int32_t bufferSize, const std::shared_ptr< IWaitStrategy >& waitStrategy)
: Sequencer< T >(bufferSize, waitStrategy)
, m_fields(Sequence::InitialCursorValue, Sequence::InitialCursorValue)
{}
/**
* Has the buffer got capacity to allocate another sequence. This is a concurrent method so the response should only be taken as an indication of available capacity.
*
* \param requiredCapacity requiredCapacity in the buffer
* \returns true if the buffer has the capacity to allocate the next sequence otherwise false.
*/
bool hasAvailableCapacity(int requiredCapacity) override
{
std::int64_t nextValue = m_fields.nextValue;
std::int64_t wrapPoint = (nextValue + requiredCapacity) - this->m_bufferSize;
std::int64_t cachedGatingSequence = m_fields.cachedValue;
if (wrapPoint > cachedGatingSequence || cachedGatingSequence > nextValue)
{
auto minSequence = Util::getMinimumSequence(this->m_gatingSequences, nextValue);
m_fields.cachedValue = minSequence;
if (wrapPoint > minSequence)
{
return false;
}
}
return true;
}
/**
* Claim the next event in sequence for publishing.
*/
std::int64_t next() override
{
return next(1);
}
/**
* Claim the next n events in sequence for publishing. This is for batch event producing. Using batch producing requires a little care and some math.
* <code>
* int n = 10;
* long hi = sequencer.next(n);
* long lo = hi - (n - 1);
* for (long sequence = lo; sequence <= hi; sequence++) {
* // Do work.
* }
* sequencer.publish(lo, hi);
* </code>
*
* \param n the number of sequences to claim
* \returns the highest claimed sequence value
*/
std::int64_t next(std::int32_t n) override
{
if (n < 1)
{
DISRUPTOR_THROW_ARGUMENT_EXCEPTION("n must be > 0");
}
auto nextValue = m_fields.nextValue;
auto nextSequence = nextValue + n;
auto wrapPoint = nextSequence - this->m_bufferSize;
auto cachedGatingSequence = m_fields.cachedValue;
if (wrapPoint > cachedGatingSequence || cachedGatingSequence > nextValue)
{
this->m_cursor->setValue(nextValue);
SpinWait spinWait;
std::int64_t minSequence;
while (wrapPoint > (minSequence = Util::getMinimumSequence(this->m_gatingSequences, nextValue)))
{
this->m_waitStrategyRef.signalAllWhenBlocking();
spinWait.spinOnce();
}
m_fields.cachedValue = minSequence;
}
m_fields.nextValue = nextSequence;
return nextSequence;
}
/**
* Attempt to claim the next event in sequence for publishing. Will return the number of the slot if there is at least requiredCapacity slots available.
*
* \returns the claimed sequence value
*/
std::int64_t tryNext() override
{
return tryNext(1);
}
/**
* Attempt to claim the next event in sequence for publishing. Will return the number of the slot if there is at least availableCapacity slots available.
*
* \param n the number of sequences to claim
* \returns the claimed sequence value
*/
std::int64_t tryNext(std::int32_t n) override
{
if (n < 1)
{
DISRUPTOR_THROW_ARGUMENT_EXCEPTION("n must be > 0");
}
if (!hasAvailableCapacity(n))
{
DISRUPTOR_THROW_INSUFFICIENT_CAPACITY_EXCEPTION();
}
auto nextSequence = m_fields.nextValue + n;
m_fields.nextValue = nextSequence;
return nextSequence;
}
/**
* Get the remaining capacity for this sequencer. return The number of slots remaining.
*/
std::int64_t getRemainingCapacity() override
{
auto nextValue = m_fields.nextValue;
auto consumed = Util::getMinimumSequence(this->m_gatingSequences, nextValue);
auto produced = nextValue;
return this->bufferSize() - (produced - consumed);
}
/**
* Claim a specific sequence when only one publisher is involved.
*
* \param sequence sequence to be claimed.
*/
void claim(std::int64_t sequence) override
{
m_fields.nextValue = sequence;
}
/**
* Publish an event and make it visible to IEventProcessors
*
* \param sequence sequence to be published
*/
void publish(std::int64_t sequence) override
{
this->m_cursorRef.setValue(sequence);
this->m_waitStrategyRef.signalAllWhenBlocking();
}
/**
* Batch publish sequences. Called when all of the events have been filled.
*
* \param lo first sequence number to publish
* \param hi last sequence number to publish
*/
void publish(std::int64_t /*lo*/, std::int64_t hi) override
{
publish(hi);
}
/**
* Confirms if a sequence is published and the event is available for use; non-blocking.
*
* \param sequence sequence of the buffer to check
* \returns true if the sequence is available for use, false if not
*/
bool isAvailable(std::int64_t sequence) override
{
return sequence <= this->m_cursorRef.value();
}
/**
* Get the highest sequence number that can be safely read from the ring buffer. Depending on the implementation of the Sequencer this call may need to scan a number of values
* in the Sequencer. The scan will range from nextSequence to availableSequence. If there are no available values > nextSequence the return value will be nextSequence - 1.
* To work correctly a consumer should pass a value that is 1 higher than the last sequence that was successfully processed.
*
* \param nextSequence The sequence to start scanning from.
* \param availableSequence The sequence to scan to.
* \returns The highest value that can be safely read, will be at least nextSequence - 1</code>.
*/
std::int64_t getHighestPublishedSequence(std::int64_t /*nextSequence*/, std::int64_t availableSequence) override
{
return availableSequence;
}
protected:
Fields m_fields;
};
} // namespace Disruptor