Repository navigation
Expand file tree
/
Copy pathNoOpEventProcessor.h
More file actions
124 lines (102 loc) · 2.9 KB
/
Copy pathNoOpEventProcessor.h
File metadata and controls
124 lines (102 loc) · 2.9 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
#pragma once
#include <atomic>
#include <memory>
#include <type_traits>
#include "Disruptor/IEventProcessor.h"
#include "Disruptor/InvalidOperationException.h"
#include "Disruptor/ISequence.h"
#include "Disruptor/RingBuffer.h"
namespace Disruptor
{
/**
* No operation version of a IEventProcessor that simply tracks a Disruptor.Sequence. This is useful in tests or for pre-filling a RingBuffer<T> from a producer.
*/
template <class T>
class NoOpEventProcessor : public IEventProcessor
{
static_assert(std::is_class< T >::value, "T should be a class");
class SequencerFollowingSequence;
public:
/**
* Construct a IEventProcessor that simply tracks a Disruptor.Sequence
* .
* \param sequencer sequencer to track.
*/
explicit NoOpEventProcessor(const std::shared_ptr< RingBuffer< T > >& sequencer)
: m_sequence(std::make_shared< SequencerFollowingSequence >(sequencer))
{
}
/**
* NoOp
*/
void run() override
{
if (std::atomic_exchange(&m_running, 1) != 0)
{
DISRUPTOR_THROW_INVALID_OPERATION_EXCEPTION("Thread is already running");
}
}
/**
*
* See IEventProcessor::sequence()
*
*/
std::shared_ptr< ISequence > sequence() const override
{
return m_sequence;
}
/**
* NoOp
*/
void halt() override
{
m_running = 0;
}
/**
*
* See IEventProcessor::isRunning()
*
*/
bool isRunning() const override
{
return m_running == 1;
}
private:
std::shared_ptr< SequencerFollowingSequence > m_sequence;
std::atomic< std::int32_t > m_running;
};
template <class T>
class NoOpEventProcessor< T >::SequencerFollowingSequence : public ISequence
{
public:
explicit SequencerFollowingSequence(const std::shared_ptr< RingBuffer< T > >& sequencer)
: m_sequencer(sequencer)
{
}
std::int64_t value() const override
{
return m_sequencer->cursor();
}
void setValue(std::int64_t /*value*/) override
{
}
bool compareAndSet(std::int64_t /*expectedSequence*/, std::int64_t /*nextSequence*/) override
{
return false;
}
std::int64_t incrementAndGet() override
{
return 0;
}
std::int64_t addAndGet(std::int64_t /*value*/) override
{
return 0;
}
void writeDescriptionTo(std::ostream& stream) const override
{
stream << "SequencerFollowingSequence";
}
private:
std::shared_ptr< RingBuffer< T > > m_sequencer;
};
} // namespace Disruptor