|
| 1 | +/* |
| 2 | +Copyright (c) 2015, Alex Man-fui Lee |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | +* Redistributions of source code must retain the above copyright notice, this |
| 9 | + list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | +* Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + this list of conditions and the following disclaimer in the documentation |
| 13 | + and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | +* Neither the name of disruptor4cpp nor the names of its |
| 16 | + contributors may be used to endorse or promote products derived from |
| 17 | + this software without specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +#ifndef DISRUPTOR4CPP_BATCH_EVENT_PROCESSOR_H_ |
| 32 | +#define DISRUPTOR4CPP_BATCH_EVENT_PROCESSOR_H_ |
| 33 | + |
| 34 | +#include <atomic> |
| 35 | +#include <cstdint> |
| 36 | +#include <memory> |
| 37 | +#include <stdexcept> |
| 38 | + |
| 39 | +#include "event_handler.h" |
| 40 | +#include "exceptions/alert_exception.h" |
| 41 | +#include "exceptions/timeout_exception.h" |
| 42 | +#include "sequence.h" |
| 43 | + |
| 44 | +namespace disruptor4cpp |
| 45 | +{ |
| 46 | + template <typename TRingBuffer> |
| 47 | + class batch_event_processor |
| 48 | + { |
| 49 | + public: |
| 50 | + batch_event_processor(TRingBuffer& ring_buffer, |
| 51 | + typename TRingBuffer::sequence_barrier_type& sequence_barrier, |
| 52 | + event_handler<typename TRingBuffer::event_type>& evt_handler) |
| 53 | + : sequence_(), |
| 54 | + ring_buffer_(ring_buffer), |
| 55 | + sequence_barrier_(sequence_barrier), |
| 56 | + event_handler_(evt_handler), |
| 57 | + running_(false) |
| 58 | + { |
| 59 | + } |
| 60 | + |
| 61 | + batch_event_processor(TRingBuffer& ring_buffer, |
| 62 | + std::unique_ptr<typename TRingBuffer::sequence_barrier_type> sequence_barrier_ptr, |
| 63 | + event_handler<typename TRingBuffer::event_type>& evt_handler) |
| 64 | + : sequence_(), |
| 65 | + ring_buffer_(ring_buffer), |
| 66 | + sequence_barrier_(*sequence_barrier_ptr), |
| 67 | + event_handler_(evt_handler), |
| 68 | + sequence_barrier_ptr_(std::move(sequence_barrier_ptr)), |
| 69 | + running_(false) |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + typename TRingBuffer::sequence_type& get_sequence() |
| 74 | + { |
| 75 | + return sequence_; |
| 76 | + } |
| 77 | + |
| 78 | + void halt() |
| 79 | + { |
| 80 | + running_.store(false, std::memory_order_release); |
| 81 | + sequence_barrier_.alert(); |
| 82 | + } |
| 83 | + |
| 84 | + bool is_running() const |
| 85 | + { |
| 86 | + return running_.load(std::memory_order_acquire); |
| 87 | + } |
| 88 | + |
| 89 | + void run() |
| 90 | + { |
| 91 | + bool expected_running_state = false; |
| 92 | + if (!running_.compare_exchange_strong(expected_running_state, true)) |
| 93 | + throw std::runtime_error("Thread is already running"); |
| 94 | + |
| 95 | + sequence_barrier_.clear_alert(); |
| 96 | + notify_start(); |
| 97 | + |
| 98 | + typename TRingBuffer::event_type* event; |
| 99 | + int64_t next_sequence = sequence_.get() + 1; |
| 100 | + try |
| 101 | + { |
| 102 | + while (true) |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + const int64_t available_sequence = sequence_barrier_.wait_for(next_sequence); |
| 107 | + while (next_sequence <= available_sequence) |
| 108 | + { |
| 109 | + event = &ring_buffer_[next_sequence]; |
| 110 | + event_handler_.on_event(*event, next_sequence, next_sequence == available_sequence); |
| 111 | + next_sequence++; |
| 112 | + } |
| 113 | + sequence_.set(available_sequence); |
| 114 | + } |
| 115 | + catch (timeout_exception& timeout_ex) |
| 116 | + { |
| 117 | + notify_timeout(sequence_.get()); |
| 118 | + } |
| 119 | + catch (alert_exception& alert_ex) |
| 120 | + { |
| 121 | + if (!running_.load(std::memory_order_acquire)) |
| 122 | + break; |
| 123 | + } |
| 124 | + catch (std::exception& ex) |
| 125 | + { |
| 126 | + event_handler_.on_event_exception(ex, next_sequence, event); |
| 127 | + sequence_.set(next_sequence); |
| 128 | + next_sequence++; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + catch (...) |
| 133 | + { |
| 134 | + notify_shutdown(); |
| 135 | + running_.store(false, std::memory_order_release); |
| 136 | + throw; |
| 137 | + } |
| 138 | + notify_shutdown(); |
| 139 | + running_.store(false, std::memory_order_release); |
| 140 | + } |
| 141 | + |
| 142 | + private: |
| 143 | + void notify_timeout(int64_t available_sequence) |
| 144 | + { |
| 145 | + try |
| 146 | + { |
| 147 | + event_handler_.on_timeout(available_sequence); |
| 148 | + } |
| 149 | + catch (std::exception& ex) |
| 150 | + { |
| 151 | + event_handler_.on_event_exception(ex, available_sequence, nullptr); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + void notify_start() |
| 156 | + { |
| 157 | + try |
| 158 | + { |
| 159 | + event_handler_.on_start(); |
| 160 | + } |
| 161 | + catch (std::exception& ex) |
| 162 | + { |
| 163 | + event_handler_.on_start_exception(ex); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + void notify_shutdown() |
| 168 | + { |
| 169 | + try |
| 170 | + { |
| 171 | + event_handler_.on_shutdown(); |
| 172 | + } |
| 173 | + catch (std::exception& ex) |
| 174 | + { |
| 175 | + event_handler_.on_shutdown_exception(ex); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + typename TRingBuffer::sequence_type sequence_; |
| 180 | + TRingBuffer& ring_buffer_; |
| 181 | + typename TRingBuffer::sequence_barrier_type& sequence_barrier_; |
| 182 | + event_handler<typename TRingBuffer::event_type>& event_handler_; |
| 183 | + std::unique_ptr<typename TRingBuffer::sequence_barrier_type> sequence_barrier_ptr_; |
| 184 | + std::atomic<bool> running_; |
| 185 | + }; |
| 186 | +} |
| 187 | + |
| 188 | +#endif |
0 commit comments