Commit b8e71625 by lucapegolotti

container_cpp: refactor BlockingContainer -> BlockingPushAndPopContainer, moved to internal folder

parent ecd1ecdf
/*
* 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 EMBB_CONTAINERS_BLOCKING_CONTAINER_H_
#define EMBB_CONTAINERS_BLOCKING_CONTAINER_H_
#include <embb/base/base.h>
namespace embb {
namespace containers {
/*
* Abstract class to use for the implementation of blocking datastructure.
* Child classes need to be wrappers for another container.
*
* \tparam T Element type of the container's data.
*/
template < typename Type >
class BlockingContainer {
typedef embb::base::Mutex Mutex;
protected:
/*
* Mutex for thread synchronization.
*/
Mutex mutex;
/**
* Condition variable for notifying threads that are waiting
* for popping elements that new elements are available.
*/
embb::base::ConditionVariable condition;
/**
* Pure virtual method to be implemented in derived classes.
* The method must wrap the pushing method of the
* underlying container.
*/
virtual void SpecializedPush(
const Type& element
/**< [IN] Constant reference
to element that shall be pushed. */) = 0;
/**
* Pure virtual method to be implemented in derived classes.
* The method must wrap the popping method of the
* underlying container.
*/
virtual void SpecializedPop(
Type& element
/**< [IN, OUT] Reference to popped element*/) = 0;
/**
* Pure virtual method to check if the underlying container
* is empty.
*
* \return \c true if container is empty, \c false otherwise.
*/
virtual bool IsEmpty() = 0;
/**
* Pushes an element and notifies threads that are waiting to
* pop that new elements are available
*
*\note The calling threads acquire the mutex at the beginning
* and release it at the end of the method to avoid concurrent
* access to the structure.
*/
void PushAndWakeUp(
const Type& element
/**< [IN] Constant reference
to element that shall be pushed */);
/**
* Pops an element from the container. If no elements are
* available, the calling thread wait until it is notified
* by the condition variable in PushAndWakeUp.
*
*\note The calling threads acquire the mutex at the beginning
* and release it at the end of the method to avoid concurrent
* access to the structure.
*/
void BlockingPop(
Type & element
/**< [IN,OUT] Reference to popped element*/);
};
}
}
#include <embb/containers/internal/blocking_container-inl.h>
#endif // EMBB_CONTAINERS_BLOCKING_CONTAINER_
\ No newline at end of file
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include <queue> #include <queue>
#include <embb/containers/blocking_container.h> #include <embb/containers/internal/blocking_push_and_pop_container.h>
namespace embb { namespace embb {
namespace containers { namespace containers {
...@@ -42,7 +42,7 @@ namespace containers { ...@@ -42,7 +42,7 @@ namespace containers {
template< typename Type, template< typename Type,
class Container = std::vector<Type>, class Container = std::vector<Type>,
class Compare = std::less<typename Container::value_type>> class Compare = std::less<typename Container::value_type>>
class BlockingPriorityQueue : public BlockingContainer<Type> { class BlockingPriorityQueue : public BlockingPushAndPopContainer<Type> {
private: private:
/** /**
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include <queue> #include <queue>
#include <embb/containers/blocking_container.h> #include <embb/containers/internal/blocking_push_and_pop_container.h>
namespace embb { namespace embb {
namespace containers { namespace containers {
...@@ -40,7 +40,7 @@ namespace containers { ...@@ -40,7 +40,7 @@ namespace containers {
* \tparam T Element type * \tparam T Element type
*/ */
template< typename Type> template< typename Type>
class BlockingQueue : public BlockingContainer<Type> { class BlockingQueue : public BlockingPushAndPopContainer<Type> {
private: private:
/** /**
* Internal queue from the standard library. * Internal queue from the standard library.
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
#include <stack> #include <stack>
#include <embb/containers/blocking_container.h> #include <embb/containers/internal/blocking_push_and_pop_container.h>
namespace embb { namespace embb {
namespace containers { namespace containers {
...@@ -40,7 +40,7 @@ namespace containers { ...@@ -40,7 +40,7 @@ namespace containers {
* \tparam T Element type * \tparam T Element type
*/ */
template< typename Type> template< typename Type>
class BlockingStack : public BlockingContainer<Type> { class BlockingStack : public BlockingPushAndPopContainer<Type> {
private: private:
/** /**
* Internal stack from the standard library. * Internal stack from the standard library.
......
#include "..\blocking_container.h"
/* /*
* Copyright (c) 2014-2015, Siemens AG. All rights reserved. * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
* *
...@@ -25,25 +24,42 @@ ...@@ -25,25 +24,42 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef EMBB_CONTAINERS_BLOCKING_CONTAINER_INL_H_ #ifndef EMBB_CONTAINERS_BLOCKING_PUSH_AND_POP_CONTAINER_H_
#define EMBB_CONTAINERS_BLOCKING_CONTAINER_INL_H_ #define EMBB_CONTAINERS_BLOCKING_PUSH_AND_POP_CONTAINER_H_
#include <embb/base/base.h>
namespace embb { namespace embb {
namespace containers { namespace containers {
template<typename T> template < typename Type >
void BlockingContainer<T>::PushAndWakeUp(const T& element) { class BlockingPushAndPopContainer {
typedef embb::base::Mutex Mutex;
protected:
Mutex mutex;
embb::base::ConditionVariable condition;
virtual void SpecializedPush(const Type& element) = 0;
virtual void SpecializedPop(Type& element) = 0;
virtual bool IsEmpty() = 0;
void PushAndWakeUp(const Type& element) {
embb::base::LockGuard<Mutex> lock(mutex); embb::base::LockGuard<Mutex> lock(mutex);
SpecializedPush(element); SpecializedPush(element);
condition.NotifyOne(); condition.NotifyOne();
} }
template<typename T> void BlockingPop( Type & element) {
void BlockingContainer<T>::BlockingPop(T& element) {
embb::base::UniqueLock<Mutex> lock(mutex); embb::base::UniqueLock<Mutex> lock(mutex);
while (IsEmpty()) { while (IsEmpty()) {
...@@ -51,9 +67,12 @@ void BlockingContainer<T>::BlockingPop(T& element) { ...@@ -51,9 +67,12 @@ void BlockingContainer<T>::BlockingPop(T& element) {
} }
SpecializedPop(element); SpecializedPop(element);
} }
};
} }
} }
#endif // EMBB_CONTAINERS_BLOCKING_CONTAINER_INL_H_ #endif // EMBB_CONTAINERS_BLOCKING_PUSH_AND_POP_CONTAINER_
\ No newline at end of file \ No newline at end of file
#include "..\blocking_set.h"
/* /*
* Copyright (c) 2014-2015, Siemens AG. All rights reserved. * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
* *
...@@ -25,8 +24,8 @@ ...@@ -25,8 +24,8 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef EMBB_CONTAINERS_BLOCKING_SET_INL_H_ #ifndef EMBB_CONTAINERS_INTERNAL_BLOCKING_SET_INL_H_
#define EMBB_CONTAINERS_BLOCKING_SET_INL_H_ #define EMBB_CONTAINERS_INTERNAL_BLOCKING_SET_INL_H_
namespace embb { namespace embb {
...@@ -57,4 +56,4 @@ namespace containers { ...@@ -57,4 +56,4 @@ namespace containers {
} }
} }
#endif // EMBB_CONTAINERS_BLOCKING_SET_INL_H_ #endif // EMBB_CONTAINERS_INTERNAL_BLOCKING_SET_INL_H_
\ No newline at end of file \ No newline at end of file
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