Commit bc8d07e3 by Tobias Fuchs

containers_cpp: implementing LLX/SCX, using FixedSizeList for list parameters

parent 16ccb48e
......@@ -30,28 +30,43 @@
namespace embb {
namespace containers {
namespace internal {
template< typename ElementT >
FixedSizeList<ElementT>::FixedSizeList(size_t capacity) :
max_size_(capacity),
size_(0) {
elementsArray = static_cast<ElementT*>(
embb::base::Allocation::Allocate(sizeof(ElementT) *
max_size_));
}
template< typename ElementT >
FixedSizeList<ElementT>::FixedSizeList(size_t max_size) :
max_size(max_size),
size(0) {
FixedSizeList<ElementT>::FixedSizeList(const FixedSizeList & other) :
max_size_(other.max_size_),
size_(0) {
elementsArray = static_cast<ElementT*>(
embb::base::Allocation::Allocate(sizeof(ElementT) *
max_size));
max_size_));
const_iterator it;
const_iterator end = other.end();
for (it = other.begin(); it != end; ++it) {
PushBack(*it);
}
}
template< typename ElementT >
inline size_t FixedSizeList<ElementT>::GetSize() const {
return size;
return size_;
}
template< typename ElementT >
inline size_t FixedSizeList<ElementT>::GetMaxSize() const {
return max_size;
return max_size_;
}
template< typename ElementT >
inline void FixedSizeList<ElementT>::clear() {
size = 0;
size_ = 0;
}
template< typename ElementT >
......@@ -63,31 +78,31 @@ FixedSizeList<ElementT>::begin() const {
template< typename ElementT >
typename FixedSizeList<ElementT>::iterator
FixedSizeList<ElementT>::end() const {
return &elementsArray[size];
return &elementsArray[size_];
}
template< typename ElementT >
FixedSizeList< ElementT > &
FixedSizeList<ElementT>::operator= (const FixedSizeList & other) {
size = 0;
if (max_size < other.size) {
size_ = 0;
if (max_size_ < other.size_) {
EMBB_THROW(embb::base::ErrorException, "Copy target to small");
}
for (const_iterator it = other.begin(); it != other.end(); ++it) {
const_iterator it;
const_iterator end = other.end();
for (it = other.begin(); it != end; ++it) {
PushBack(*it);
}
return *this;
}
template< typename ElementT >
bool FixedSizeList<ElementT>::PushBack(ElementT const el) {
if (size + 1 > max_size) {
bool FixedSizeList<ElementT>::PushBack(const ElementT & el) {
if (size_ + 1 > max_size_) {
return false;
}
elementsArray[size] = el;
size++;
elementsArray[size_] = el;
size_++;
return true;
}
......@@ -100,4 +115,4 @@ FixedSizeList<ElementT>::~FixedSizeList() {
} // namespace containers
} // namespace embb
#endif // EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_INL_H_
#endif // EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_INL_H_
\ No newline at end of file
......@@ -30,6 +30,7 @@
namespace embb {
namespace containers {
namespace internal {
/**
* A list with fixed size, implemented as an array. Replaces std::vector that
* was used in previous hazard pointer implementation.
......@@ -41,28 +42,21 @@ namespace internal {
template< typename ElementT >
class FixedSizeList {
private:
/**
* Capacity of the list
*/
size_t max_size;
/**
* Capacity of the list
*/
size_t max_size_;
/**
* Size of the list
*/
size_t size;
size_t size_;
/**
* Pointer to the array containing the list
*/
ElementT* elementsArray;
/**
* Copy constructor not implemented. Would require dynamic memory allocation.
*/
FixedSizeList(
const FixedSizeList &
/**< [IN] Other list */);
public:
/**
* Definition of an iterator
......@@ -78,20 +72,38 @@ class FixedSizeList {
* Constructor, initializes list with given capacity
*/
FixedSizeList(
size_t max_size
/**< [IN] Capacity of the list */);
size_t capacity
/**< [IN] Capacity of the list */
);
/**
* Copy constructor.
*/
FixedSizeList(
const FixedSizeList &
/**< [IN] Other list */
);
/**
* Copies the elements of another list to this list. The capacity of
* this list has to be greater than or equal to the size of the other list.
*/
FixedSizeList & operator=(
const FixedSizeList & other
/**< [IN] Other list */
);
/**
* Gets the current size of the list
* Gets the current number of element in the list
*
* \return Size of the list
*/
inline size_t GetSize() const;
/**
* Gets the capacity of the list
* Gets the maximum number of elements that the list can hold
*
* \return The capacity of the list
* \return The element capacity of the list
*/
inline size_t GetMaxSize() const;
......@@ -115,22 +127,15 @@ class FixedSizeList {
iterator end() const;
/**
* Copies the elements of another list to this list. The capacity of
* this list has to be greater than or equal to the size of the other list.
*/
FixedSizeList & operator=(
const FixedSizeList & other
/**< [IN] Other list */);
/**
* Appends an element to the end of the list
*
* \return \c false if the operation was not successful because the list is
* full, otherwise \c true.
*/
bool PushBack(
ElementT const el
/**< [IN] Element to append to the list */);
const ElementT & el
/**< [IN] Element to append to the list */
);
/**
* Destructs the list.
......@@ -142,6 +147,6 @@ class FixedSizeList {
} // namespace containers
} // namespace embb
#include "./fixed_size_list-inl.h"
#include <embb/containers/internal/fixed_size_list-inl.h>
#endif // EMBB_CONTAINERS_INTERNAL_FIXED_SIZE_LIST_H_
/*
* Copyright (c) 2014-2015, Siemens AG. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <llx_scx_test.h>
#include <embb/containers/internal/fixed_size_list.h>
#include <embb/containers/primitives/llx_scx.h>
namespace embb {
namespace containers {
namespace test {
using embb::containers::internal::FixedSizeList;
using embb::containers::primitives::LlxScxRecord;
using embb::containers::primitives::LlxScx;
LlxScxTest::LlxScxTest() :
num_threads_(static_cast<int>
(partest::TestSuite::GetDefaultNumThreads())) {
CreateUnit("SerialTest").Add(&LlxScxTest::SerialTest, this);
}
void LlxScxTest::SerialTest() {
typedef LlxScxTest::Node Node;
// Global:
LlxScx<Node> llxscx(3);
// Multiset { a, b, b, c }
Node n1(1, 'a');
Node n2(2, 'b');
Node n3(1, 'c');
// V = { dr1, dr2, dr3 }
// R = { dr1, dr2 }
LlxScxRecord<Node> dr1(n1);
LlxScxRecord<Node> dr2(n2);
LlxScxRecord<Node> dr3(n3);
dr1->next_.Store(&dr2);
dr2->next_.Store(&dr3);
// Thread-local:
LlxScxRecord<Node> l1, l2, l3;
bool finalized;
PT_ASSERT(llxscx.TryLoadLinked(&dr1, l1, finalized));
PT_ASSERT(!finalized);
PT_ASSERT(llxscx.TryLoadLinked(&dr2, l2, finalized));
PT_ASSERT(!finalized);
PT_ASSERT(llxscx.TryLoadLinked(&dr3, l3, finalized));
PT_ASSERT(!finalized);
FixedSizeList< LlxScxRecord<Node> * >
linked_deps(3);
linked_deps.PushBack(&dr1);
linked_deps.PushBack(&dr2);
linked_deps.PushBack(&dr3);
FixedSizeList< LlxScxRecord<Node> * >
finalize_deps(2);
finalize_deps.PushBack(&dr2);
finalize_deps.PushBack(&dr3);
typedef LlxScxRecord<Node> * field_t;
LlxScxRecord<Node> new_node(n3);
PT_ASSERT(
llxscx.TryStoreConditional<LlxScxRecord<Node> *>(
&n2.next_, // fld: field to update
&new_node, // new value
linked_deps, // V: dependencies, must be LL'd before
finalize_deps // R: Subsequence of V to be finalized
));
}
} // namespace test
} // namespace containers
} // namespace embb
/*
* Copyright (c) 2014-2015, Siemens AG. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CONTAINERS_CPP_TEST_LLX_SCX_TEST_H_
#define CONTAINERS_CPP_TEST_LLX_SCX_TEST_H_
#include <partest/partest.h>
#include <embb/containers/primitives/llx_scx.h>
namespace embb {
namespace containers {
namespace test {
class LlxScxTest : public partest::TestCase {
private:
class Node {
public:
typedef primitives::LlxScxRecord<Node> * node_ptr_t;
private:
char value_;
public:
embb::base::Atomic<primitives::LlxScxRecord<Node> *> next_;
embb::base::Atomic<int> count_;
public:
Node()
: value_(-1) {
next_.Store(reinterpret_cast<node_ptr_t>(0));
count_.Store(0);
}
Node(int count, char value)
: value_(value) {
count_.Store(count);
next_.Store(reinterpret_cast<node_ptr_t>(0));
}
Node(int count, char value, node_ptr_t const next_node)
: value_(value) {
count_.Store(count);
next_.Store(next_node);
}
Node & operator=(const Node & rhs) {
count_.Store(rhs.count_.Load());
next_.Store(rhs.next_.Load());
value_ = rhs.value_;
}
};
public:
/**
* Adds test methods.
*/
LlxScxTest();
private:
void SerialTest();
int num_threads_;
};
} // namespace test
} // namespace containers
} // namespace embb
#endif // CONTAINERS_CPP_TEST_LLX_SCX_TEST_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment