From 1af139a4d42f6d5aa8f0f575029aebdd7d68da0d Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 12 Mar 2018 18:01:22 -0700 Subject: [PATCH] FIrst step introducing the Transaction model to access the Proxy of the space --- libraries/workload/src/workload/Proxy.h | 5 - libraries/workload/src/workload/Transaction.h | 120 ++++++++++-------- 2 files changed, 65 insertions(+), 60 deletions(-) diff --git a/libraries/workload/src/workload/Proxy.h b/libraries/workload/src/workload/Proxy.h index bf3de6e4c9..b7f8094b00 100644 --- a/libraries/workload/src/workload/Proxy.h +++ b/libraries/workload/src/workload/Proxy.h @@ -15,11 +15,6 @@ namespace workload { -using Index = int32_t; -using ProxyID = Index; - -using IndexVector = std::vector; - class Proxy { public: Proxy() : sphere(0.0f) {} diff --git a/libraries/workload/src/workload/Transaction.h b/libraries/workload/src/workload/Transaction.h index 87bf0e4209..098e953f36 100644 --- a/libraries/workload/src/workload/Transaction.h +++ b/libraries/workload/src/workload/Transaction.h @@ -12,6 +12,8 @@ #ifndef hifi_workload_Transaction_h #define hifi_workload_Transaction_h +#include +#include #include #include #include @@ -21,6 +23,64 @@ namespace workload { + namespace indexed_container { + + using Index = int32_t; + const Index MAXIMUM_INDEX{ 1 << 30 }; + const Index INVALID_INDEX{ -1 }; + using Indices = std::vector< Index >; + + template + class Allocator { + public: + Allocator() {} + Indices _freeIndices; + Index _nextNewIndex{ 0 }; + + bool checkIndex(Index index) const { return ((index >= 0) && (index < _nextNewIndex)); } + Index getNumIndices() const { return _nextNewIndex - (Index)_freeIndices.size(); } + Index getNumFreeIndices() const { return (Index)_freeIndices.size(); } + Index getNumAllocatedIndices() const { return _nextNewIndex; } + + Index allocateIndex() { + if (_freeIndices.empty()) { + Index index = _nextNewIndex; + if (index >= MaxNumElements) { + // abort! we are trying to go overboard with the total number of allocated elements + assert(false); + // This should never happen because Bricks are allocated along with the cells and there + // is already a cap on the cells allocation + return INVALID_INDEX; + } + _nextNewIndex++; + return index; + } else { + Index index = _freeIndices.back(); + _freeIndices.pop_back(); + return index; + } + } + + void freeIndex(Index index) { + if (checkIndex(index)) { + _freeIndices.push_back(index); + } + } + + void clear() { + _freeIndices.clear(); + _nextNewIndex = 0; + } + }; + } + + + using Index = indexed_container::Index; + using IndexVector = indexed_container::Indices; + + using ProxyID = Index; + + // Transaction is the mechanism to make any change to the Space. // Whenever a new proxy need to be reset, // or when an proxy changes its position or its size @@ -67,58 +127,6 @@ protected: }; typedef std::vector TransactionQueue; -namespace indexed_container { - - using Index = int32_t; - const Index MAXIMUM_INDEX{ 1 << 30 }; - const Index INVALID_INDEX{ -1 }; - using Indices = std::vector< Index >; - - template - class Allocator { - public: - Allocator() {} - Indices _freeIndices; - std::atomic _nextNewIndex{ 0 }; - std::atomic _numFreeIndices{ 0 }; - - bool checkIndex(Index index) const { return ((index >= 0) && (index < _nextNewIndex.load())); } - Index getNumIndices() const { return _nextNewIndex - (Index)_freeIndices.size(); } - Index getNumFreeIndices() const { return (Index)_freeIndices.size(); } - Index getNumAllocatedIndices() const { return _nextNewIndex.load(); } - - Index allocateIndex() { - if (_freeIndices.empty()) { - Index index = _nextNewIndex; - if (index >= MaxNumElements) { - // abort! we are trying to go overboard with the total number of allocated elements - assert(false); - // This should never happen because Bricks are allocated along with the cells and there - // is already a cap on the cells allocation - return INVALID_INDEX; - } - _nextNewIndex++; - return index; - } else { - Index index = _freeIndices.back(); - _freeIndices.pop_back(); - return index; - } - } - - void freeIndex(Index index) { - if (checkIndex(index)) { - _freeIndices.push_back(index); - } - } - - void clear() { - _freeIndices.clear(); - _nextNewIndex = 0; - } - }; -} - class Collection { public: @@ -129,7 +137,7 @@ public: bool isAllocatedID(const ProxyID& id) const; // THis is the total number of allocated proxies, this a threadsafe call - Index getNumAllocatedProxies() const { return _numAllocatedProxies.load(); } + Index getNumAllocatedProxies() const { return _IDAllocator.getNumAllocatedIndices(); } // Enqueue transaction to the space void enqueueTransaction(const Transaction& transaction); @@ -146,8 +154,10 @@ public: protected: // Thread safe elements that can be accessed from anywhere - std::atomic _IDAllocator{ 1 }; // first valid itemID will be One - std::atomic _numAllocatedItems{ 1 }; // num of allocated items, matching the _items.size() + indexed_container::Allocator<> _IDAllocator; + + //std::atomic _IDAllocator{ 1 }; // first valid itemID will be One + //std::atomic _numAllocatedItems{ 1 }; // num of allocated items, matching the _items.size() std::mutex _transactionQueueMutex; TransactionQueue _transactionQueue;