-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathclaim_strategy.h
158 lines (135 loc) · 5.82 KB
/
claim_strategy.h
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
// Copyright (c) 2011-2015, Francois Saint-Jacques
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the disruptor-- nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL FRANCOIS SAINT-JACQUES BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef DISRUPTOR_CLAIM_STRATEGY_H_ // NOLINT
#define DISRUPTOR_CLAIM_STRATEGY_H_ // NOLINT
#include <thread>
#include "disruptor/sequence.h"
#include "disruptor/ring_buffer.h"
namespace disruptor {
/*
// Strategy employed by a {@link Publisher} to wait claim and publish sequences
// on the sequencer.
//
class ClaimStrategy {
public:
// Wait for the given sequence to be available for consumption.
//
// @param dependents dependents sequences to wait on (mostly consumers).
// @param delta sequences to claim [default: 1].
//
// @return last claimed sequence.
int64_t IncrementAndGet(const std::vector<Sequence*>& dependents,
size_t delta = 1);
// Verify in a non-blocking way that there exists claimable sequences.
//
// @param dependents dependents sequences to wait on (mostly consumers).
//
// @return last claimed sequence.
bool HasAvailableCapacity(const std::vector<Sequence*>& dependents);
void SynchronizePublishing(const int64_t& sequence, const Sequence& cursor,
const size_t& delta) {}
};
*/
template <size_t N>
class SingleThreadedStrategy;
using kDefaultClaimStrategy = SingleThreadedStrategy<kDefaultRingBufferSize>;
// Optimised strategy can be used when there is a single publisher thread.
template <size_t N = kDefaultRingBufferSize>
class SingleThreadedStrategy {
public:
SingleThreadedStrategy()
: last_claimed_sequence_(kInitialCursorValue),
last_consumer_sequence_(kInitialCursorValue) {}
int64_t IncrementAndGet(const std::vector<Sequence*>& dependents,
size_t delta = 1) {
const int64_t next_sequence = (last_claimed_sequence_ += delta);
const int64_t wrap_point = next_sequence - N;
if (last_consumer_sequence_ < wrap_point) {
while (GetMinimumSequence(dependents) < wrap_point) {
// TODO: configurable yield strategy
std::this_thread::yield();
}
}
return next_sequence;
}
bool HasAvailableCapacity(const std::vector<Sequence*>& dependents) {
const int64_t wrap_point = last_claimed_sequence_ + 1L - N;
if (wrap_point > last_consumer_sequence_) {
const int64_t min_sequence = GetMinimumSequence(dependents);
last_consumer_sequence_ = min_sequence;
if (wrap_point > min_sequence) return false;
}
return true;
}
void SynchronizePublishing(const int64_t& sequence, const Sequence& cursor,
const size_t& delta) {}
private:
// We do not need to use atomic values since this function is called by a
// single publisher.
int64_t last_claimed_sequence_;
int64_t last_consumer_sequence_;
DISALLOW_COPY_MOVE_AND_ASSIGN(SingleThreadedStrategy);
};
// Optimised strategy can be used when there is a single publisher thread.
template <size_t N = kDefaultRingBufferSize>
class MultiThreadedStrategy {
public:
MultiThreadedStrategy() {}
int64_t IncrementAndGet(const std::vector<Sequence*>& dependents,
size_t delta = 1) {
const int64_t next_sequence = last_claimed_sequence_.IncrementAndGet(delta);
const int64_t wrap_point = next_sequence - N;
if (last_consumer_sequence_.sequence() < wrap_point) {
while (GetMinimumSequence(dependents) < wrap_point) {
// TODO: configurable yield strategy
std::this_thread::yield();
}
}
return next_sequence;
}
bool HasAvailableCapacity(const std::vector<Sequence*>& dependents) {
const int64_t wrap_point = last_claimed_sequence_.sequence() + 1L - N;
if (wrap_point > last_consumer_sequence_.sequence()) {
const int64_t min_sequence = GetMinimumSequence(dependents);
last_consumer_sequence_.set_sequence(min_sequence);
if (wrap_point > min_sequence) return false;
}
return true;
}
void SynchronizePublishing(const int64_t& sequence, const Sequence& cursor,
const size_t& delta) {
int64_t my_first_sequence = sequence - delta;
while (cursor.sequence() < my_first_sequence) {
// TODO: configurable yield strategy
std::this_thread::yield();
}
}
private:
Sequence last_claimed_sequence_;
Sequence last_consumer_sequence_;
DISALLOW_COPY_MOVE_AND_ASSIGN(MultiThreadedStrategy);
};
}; // namespace disruptor
#endif // DISRUPTOR_CLAIM_STRATEGY_H_ NOLINT