From b8e716253a94f9a5f14552b57384da987c35701d Mon Sep 17 00:00:00 2001 From: lucapegolotti Date: Fri, 29 Apr 2016 13:40:22 +0200 Subject: [PATCH] container_cpp: refactor BlockingContainer -> BlockingPushAndPopContainer, moved to internal folder --- containers_cpp/include/embb/containers/blocking_container.h | 119 ----------------------------------------------------------------------------------------------------------------------- containers_cpp/include/embb/containers/blocking_priority_queue.h | 4 ++-- containers_cpp/include/embb/containers/blocking_queue.h | 4 ++-- containers_cpp/include/embb/containers/blocking_stack.h | 4 ++-- containers_cpp/include/embb/containers/internal/blocking_container-inl.h | 59 ----------------------------------------------------------- containers_cpp/include/embb/containers/internal/blocking_push_and_pop_container.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ containers_cpp/include/embb/containers/internal/blocking_set-inl.h | 7 +++---- 7 files changed, 87 insertions(+), 188 deletions(-) delete mode 100755 containers_cpp/include/embb/containers/blocking_container.h delete mode 100755 containers_cpp/include/embb/containers/internal/blocking_container-inl.h create mode 100755 containers_cpp/include/embb/containers/internal/blocking_push_and_pop_container.h diff --git a/containers_cpp/include/embb/containers/blocking_container.h b/containers_cpp/include/embb/containers/blocking_container.h deleted file mode 100755 index 3e9a949..0000000 --- a/containers_cpp/include/embb/containers/blocking_container.h +++ /dev/null @@ -1,119 +0,0 @@ -/* -* 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 - - -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 - -#endif // EMBB_CONTAINERS_BLOCKING_CONTAINER_ \ No newline at end of file diff --git a/containers_cpp/include/embb/containers/blocking_priority_queue.h b/containers_cpp/include/embb/containers/blocking_priority_queue.h index 50b4b53..0a94122 100755 --- a/containers_cpp/include/embb/containers/blocking_priority_queue.h +++ b/containers_cpp/include/embb/containers/blocking_priority_queue.h @@ -29,7 +29,7 @@ #include -#include +#include namespace embb { namespace containers { @@ -42,7 +42,7 @@ namespace containers { template< typename Type, class Container = std::vector, class Compare = std::less> -class BlockingPriorityQueue : public BlockingContainer { +class BlockingPriorityQueue : public BlockingPushAndPopContainer { private: /** diff --git a/containers_cpp/include/embb/containers/blocking_queue.h b/containers_cpp/include/embb/containers/blocking_queue.h index 2fce38f..060ec0f 100755 --- a/containers_cpp/include/embb/containers/blocking_queue.h +++ b/containers_cpp/include/embb/containers/blocking_queue.h @@ -29,7 +29,7 @@ #include -#include +#include namespace embb { namespace containers { @@ -40,7 +40,7 @@ namespace containers { * \tparam T Element type */ template< typename Type> -class BlockingQueue : public BlockingContainer { +class BlockingQueue : public BlockingPushAndPopContainer { private: /** * Internal queue from the standard library. diff --git a/containers_cpp/include/embb/containers/blocking_stack.h b/containers_cpp/include/embb/containers/blocking_stack.h index c37ef34..aa0c44b 100755 --- a/containers_cpp/include/embb/containers/blocking_stack.h +++ b/containers_cpp/include/embb/containers/blocking_stack.h @@ -29,7 +29,7 @@ #include -#include +#include namespace embb { namespace containers { @@ -40,7 +40,7 @@ namespace containers { * \tparam T Element type */ template< typename Type> -class BlockingStack : public BlockingContainer { +class BlockingStack : public BlockingPushAndPopContainer { private: /** * Internal stack from the standard library. diff --git a/containers_cpp/include/embb/containers/internal/blocking_container-inl.h b/containers_cpp/include/embb/containers/internal/blocking_container-inl.h deleted file mode 100755 index 4753cc2..0000000 --- a/containers_cpp/include/embb/containers/internal/blocking_container-inl.h +++ /dev/null @@ -1,59 +0,0 @@ -#include "..\blocking_container.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. -*/ - -#ifndef EMBB_CONTAINERS_BLOCKING_CONTAINER_INL_H_ -#define EMBB_CONTAINERS_BLOCKING_CONTAINER_INL_H_ - - - -namespace embb { -namespace containers { - -template -void BlockingContainer::PushAndWakeUp(const T& element) { - embb::base::LockGuard lock(mutex); - - SpecializedPush(element); - - condition.NotifyOne(); -} - -template -void BlockingContainer::BlockingPop(T& element) { - embb::base::UniqueLock lock(mutex); - - while (IsEmpty()) { - condition.Wait(lock); - } - - SpecializedPop(element); -} - -} -} - -#endif // EMBB_CONTAINERS_BLOCKING_CONTAINER_INL_H_ \ No newline at end of file diff --git a/containers_cpp/include/embb/containers/internal/blocking_push_and_pop_container.h b/containers_cpp/include/embb/containers/internal/blocking_push_and_pop_container.h new file mode 100755 index 0000000..b90b07e --- /dev/null +++ b/containers_cpp/include/embb/containers/internal/blocking_push_and_pop_container.h @@ -0,0 +1,78 @@ +/* +* 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_PUSH_AND_POP_CONTAINER_H_ +#define EMBB_CONTAINERS_BLOCKING_PUSH_AND_POP_CONTAINER_H_ + + +#include + + +namespace embb { +namespace containers { + +template < typename Type > +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 lock(mutex); + + SpecializedPush(element); + + condition.NotifyOne(); + } + + void BlockingPop( Type & element) { + embb::base::UniqueLock lock(mutex); + + while (IsEmpty()) { + condition.Wait(lock); + } + + SpecializedPop(element); + } + + +}; + +} +} + +#endif // EMBB_CONTAINERS_BLOCKING_PUSH_AND_POP_CONTAINER_ \ No newline at end of file diff --git a/containers_cpp/include/embb/containers/internal/blocking_set-inl.h b/containers_cpp/include/embb/containers/internal/blocking_set-inl.h index 271211f..eae2296 100755 --- a/containers_cpp/include/embb/containers/internal/blocking_set-inl.h +++ b/containers_cpp/include/embb/containers/internal/blocking_set-inl.h @@ -1,4 +1,3 @@ -#include "..\blocking_set.h" /* * Copyright (c) 2014-2015, Siemens AG. All rights reserved. * @@ -25,8 +24,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#ifndef EMBB_CONTAINERS_BLOCKING_SET_INL_H_ -#define EMBB_CONTAINERS_BLOCKING_SET_INL_H_ +#ifndef EMBB_CONTAINERS_INTERNAL_BLOCKING_SET_INL_H_ +#define EMBB_CONTAINERS_INTERNAL_BLOCKING_SET_INL_H_ namespace embb { @@ -57,4 +56,4 @@ namespace containers { } } -#endif // EMBB_CONTAINERS_BLOCKING_SET_INL_H_ \ No newline at end of file +#endif // EMBB_CONTAINERS_INTERNAL_BLOCKING_SET_INL_H_ \ No newline at end of file -- libgit2 0.26.0