parallel/stack: add view accessor for underlying data

This commit is contained in:
Danny Robson 2019-05-23 16:42:57 +10:00
parent ef56c29b22
commit 422a12258c

View File

@ -11,6 +11,7 @@
#include "../thread/primitive.hpp" #include "../thread/primitive.hpp"
#include "../debug/assert.hpp" #include "../debug/assert.hpp"
#include "../thread/spinlock.hpp" #include "../thread/spinlock.hpp"
#include "../view.hpp"
#include <atomic> #include <atomic>
#include <vector> #include <vector>
@ -84,6 +85,24 @@ namespace cruft::parallel {
return true; return true;
} }
// DO NOT make this enum easier to use. It's supposed to be annoying
// so that people don't use it.
enum class contract { I_HAVE_LOCKED_THIS_STRUCTURE };
/// Returns the currently available data.
///
/// Data is laid out bottom-to-top of the stack.
///
/// DO NOT call this if there are any other potential clients
/// accessing the structure as it will not be protected by locks.
cruft::view<ValueT*> store (contract)
{
auto begin = reinterpret_cast<ValueT*> (m_store.data ());
auto end = begin + m_cursor.load ();
return cruft::view<ValueT*> { begin, end };
}
private: private:
using index_type = std::size_t; using index_type = std::size_t;
using raw_type = std::aligned_storage_t<sizeof(ValueT), alignof(ValueT)>; using raw_type = std::aligned_storage_t<sizeof(ValueT), alignof(ValueT)>;