Skip to content

Instantly share code, notes, and snippets.

@frankosterfeld
Created February 14, 2024 12:04
Show Gist options
  • Save frankosterfeld/13d0178af109abbef78665e1545d134f to your computer and use it in GitHub Desktop.
Save frankosterfeld/13d0178af109abbef78665e1545d134f to your computer and use it in GitHub Desktop.
commit 101ed4ec699a8a02b93f8703a51f3e2b5898a33b
Author: Frank Osterfeld <frank.osterfeld@kdab.com>
Date: Wed Feb 14 12:58:45 2024 +0100
test
diff --git a/core/include/gnuradio-4.0/CircularBuffer.hpp b/core/include/gnuradio-4.0/CircularBuffer.hpp
index 9ca71da..99913ae 100644
--- a/core/include/gnuradio-4.0/CircularBuffer.hpp
+++ b/core/include/gnuradio-4.0/CircularBuffer.hpp
@@ -637,7 +637,8 @@ class CircularBuffer
[[nodiscard]] constexpr signed_index_type position() const noexcept { return _readIndexCached; }
[[nodiscard]] constexpr std::size_t available() const noexcept {
- return static_cast<std::size_t>(_buffer->_claim_strategy.getHighestPublishedSequence(_readIndexCached = _readIndex->value(), _buffer->_cursor.value()) - _readIndexCached);
+ const auto last = _buffer->_claim_strategy.getHighestPublishedSequence(_readIndexCached + 1, _buffer->_cursor.value());
+ return static_cast<std::size_t>(last - _readIndexCached);
}
}; // class buffer_reader
diff --git a/core/include/gnuradio-4.0/ClaimStrategy.hpp b/core/include/gnuradio-4.0/ClaimStrategy.hpp
index 7673703..79fd229 100644
--- a/core/include/gnuradio-4.0/ClaimStrategy.hpp
+++ b/core/include/gnuradio-4.0/ClaimStrategy.hpp
@@ -160,7 +160,7 @@ class alignas(hardware_constructive_interference_size) MultiThreadedStrategy
: private MultiThreadedStrategySizeMembers<SIZE> {
Sequence &_cursor;
WAIT_STRATEGY &_waitStrategy;
- std::vector<std::int32_t> _availableBuffer; // tracks the state of each ringbuffer slot
+ std::unique_ptr<std::atomic<Sequence::signed_index_type>[]> _availableBuffer; // tracks the state of each ringbuffer slot
std::shared_ptr<Sequence> _gatingSequenceCache = std::make_shared<Sequence>();
using MultiThreadedStrategySizeMembers<SIZE>::_size;
using MultiThreadedStrategySizeMembers<SIZE>::_indexShift;
@@ -171,14 +171,16 @@ public:
explicit
MultiThreadedStrategy(Sequence &cursor, WAIT_STRATEGY &waitStrategy) requires (SIZE != std::dynamic_extent)
- : _cursor(cursor), _waitStrategy(waitStrategy), _availableBuffer(SIZE, -1) {
+ : _cursor(cursor), _waitStrategy(waitStrategy), _availableBuffer(std::make_unique<std::atomic<signed_index_type>[]>(SIZE)) {
+ std::fill(_availableBuffer.get(), _availableBuffer.get() + SIZE, -1);
}
explicit
MultiThreadedStrategy(Sequence &cursor, WAIT_STRATEGY &waitStrategy, std::size_t buffer_size)
requires (SIZE == std::dynamic_extent)
: MultiThreadedStrategySizeMembers<SIZE>(buffer_size),
- _cursor(cursor), _waitStrategy(waitStrategy), _availableBuffer(buffer_size, -1) {
+ _cursor(cursor), _waitStrategy(waitStrategy), _availableBuffer(std::make_unique<std::atomic<signed_index_type>[]>(buffer_size)) {
+ std::fill(_availableBuffer.get(), _availableBuffer.get() + buffer_size, -1);
}
MultiThreadedStrategy(const MultiThreadedStrategy &) = delete;
@@ -266,10 +268,7 @@ public:
}
[[nodiscard]] forceinline bool isAvailable(signed_index_type sequence) const noexcept {
- const auto index = calculateIndex(sequence);
- const auto flag = calculateAvailabilityFlag(sequence);
-
- return _availableBuffer[static_cast<std::size_t>(index)] == flag;
+ return _availableBuffer[calculateIndex(sequence)] == sequence;
}
[[nodiscard]] forceinline signed_index_type getHighestPublishedSequence(const signed_index_type lowerBound, const signed_index_type availableSequence) const noexcept {
@@ -283,9 +282,7 @@ public:
}
private:
- void setAvailable(signed_index_type sequence) noexcept { setAvailableBufferValue(calculateIndex(sequence), calculateAvailabilityFlag(sequence)); }
- forceinline void setAvailableBufferValue(std::size_t index, std::int32_t flag) noexcept { _availableBuffer[index] = flag; }
- [[nodiscard]] forceinline std::int32_t calculateAvailabilityFlag(const signed_index_type sequence) const noexcept { return static_cast<std::int32_t>(static_cast<signed_index_type>(sequence) >> _indexShift); }
+ void setAvailable(signed_index_type sequence) noexcept { _availableBuffer[calculateIndex(sequence)] = sequence; }
[[nodiscard]] forceinline std::size_t calculateIndex(const signed_index_type sequence) const noexcept { return static_cast<std::size_t>(static_cast<std::int32_t>(sequence) & (_size - 1)); }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment