From 3feff4ae44fd74c32158ed5f505e063b154c4d76 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期四, 16 七月 2020 11:43:34 +0800 Subject: [PATCH] udpate --- queue/include/lock_free_queue.h | 91 +++++++++++++++++++++++++++------------------ 1 files changed, 54 insertions(+), 37 deletions(-) diff --git a/queue/include/lock_free_queue.h b/queue/include/lock_free_queue.h index d33424e..ca8adb4 100644 --- a/queue/include/lock_free_queue.h +++ b/queue/include/lock_free_queue.h @@ -6,6 +6,7 @@ #include "mem_pool.h" #include "sem_util.h" #include "logger_factory.h" +#include "shm_allocator.h" // default Queue size #define LOCK_FREE_Q_DEFAULT_SIZE 16 @@ -18,11 +19,11 @@ // forward declarations for default template values // -template <typename ELEM_T> +template <typename ELEM_T, typename Allocator> class ArrayLockFreeQueue; -template <typename ELEM_T> -class LinkedLockFreeQueue; +// template <typename ELEM_T> +// class LinkedLockFreeQueue; /// @brief Lock-free queue based on a circular array @@ -66,7 +67,9 @@ /// by default) template < typename ELEM_T, - template <typename T> class Q_TYPE = ArrayLockFreeQueue > + typename Allocator = SHM_Allocator, + template <typename T, typename AT> class Q_TYPE = ArrayLockFreeQueue + > class LockFreeQueue { @@ -133,18 +136,19 @@ protected: /// @brief the actual queue. methods are forwarded into the real /// implementation - Q_TYPE<ELEM_T> m_qImpl; + Q_TYPE<ELEM_T, Allocator> m_qImpl; private: /// @brief disable copy constructor declaring it private - LockFreeQueue<ELEM_T, Q_TYPE>(const LockFreeQueue<ELEM_T, Q_TYPE> &a_src); + LockFreeQueue<ELEM_T, Allocator, Q_TYPE>(const LockFreeQueue<ELEM_T, Allocator, Q_TYPE> &a_src); }; template < typename ELEM_T, - template <typename T> class Q_TYPE> -LockFreeQueue<ELEM_T, Q_TYPE>::LockFreeQueue(size_t qsize): reference(0), m_qImpl(qsize) + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::LockFreeQueue(size_t qsize): reference(0), m_qImpl(qsize) { // std::cout << "LockFreeQueue init reference=" << reference << std::endl; slots = SemUtil::get(IPC_PRIVATE, qsize); @@ -153,8 +157,9 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -LockFreeQueue<ELEM_T, Q_TYPE>::~LockFreeQueue() + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::~LockFreeQueue() { LoggerFactory::getLogger().debug("LockFreeQueue desctroy"); SemUtil::remove(slots); @@ -163,24 +168,27 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -inline uint32_t LockFreeQueue<ELEM_T, Q_TYPE>::size() + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +inline uint32_t LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::size() { return m_qImpl.size(); } template < typename ELEM_T, - template <typename T> class Q_TYPE> -inline bool LockFreeQueue<ELEM_T, Q_TYPE>::full() + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +inline bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::full() { return m_qImpl.full(); } template < typename ELEM_T, - template <typename T> class Q_TYPE> -inline bool LockFreeQueue<ELEM_T, Q_TYPE>::empty() + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +inline bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::empty() { return m_qImpl.empty(); } @@ -188,8 +196,9 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -bool LockFreeQueue<ELEM_T, Q_TYPE>::push(const ELEM_T &a_data) + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push(const ELEM_T &a_data) { if (SemUtil::dec(slots) == -1) { err_exit(errno, "push"); @@ -205,8 +214,9 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -bool LockFreeQueue<ELEM_T, Q_TYPE>::push_nowait(const ELEM_T &a_data) + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push_nowait(const ELEM_T &a_data) { if (SemUtil::dec_nowait(slots) == -1) { if (errno == EAGAIN) @@ -225,8 +235,9 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -bool LockFreeQueue<ELEM_T, Q_TYPE>::push_timeout(const ELEM_T &a_data, struct timespec * timeout) + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push_timeout(const ELEM_T &a_data, struct timespec * timeout) { if (SemUtil::dec_timeout(slots, timeout) == -1) { @@ -249,8 +260,9 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -bool LockFreeQueue<ELEM_T, Q_TYPE>::pop(ELEM_T &a_data) + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop(ELEM_T &a_data) { if (SemUtil::dec(items) == -1) { err_msg(errno, "LockFreeQueue pop"); @@ -267,8 +279,9 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -bool LockFreeQueue<ELEM_T, Q_TYPE>::pop_nowait(ELEM_T &a_data) + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop_nowait(ELEM_T &a_data) { if (SemUtil::dec_nowait(items) == -1) { if (errno == EAGAIN) @@ -290,8 +303,9 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -bool LockFreeQueue<ELEM_T, Q_TYPE>::pop_timeout(ELEM_T &a_data, struct timespec * timeout) + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop_timeout(ELEM_T &a_data, struct timespec * timeout) { if (SemUtil::dec_timeout(items, timeout) == -1) { if (errno == EAGAIN) @@ -312,27 +326,30 @@ template < typename ELEM_T, - template <typename T> class Q_TYPE> -ELEM_T& LockFreeQueue<ELEM_T, Q_TYPE>::operator[](unsigned i) { + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +ELEM_T& LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::operator[](unsigned i) { return m_qImpl.operator[](i); } template < typename ELEM_T, - template <typename T> class Q_TYPE> -void * LockFreeQueue<ELEM_T, Q_TYPE>::operator new(size_t size){ - return mem_pool_malloc(size); + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +void * LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::operator new(size_t size){ + return Allocator::malloc(size); } template < typename ELEM_T, - template <typename T> class Q_TYPE> -void LockFreeQueue<ELEM_T, Q_TYPE>::operator delete(void *p) { - return mem_pool_free(p); + typename Allocator, + template <typename T, typename AT> class Q_TYPE> +void LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::operator delete(void *p) { + return Allocator::free(p); } // include implementation files -#include "linked_lock_free_queue.h" +//#include "linked_lock_free_queue.h" #include "array_lock_free_queue.h" #endif // _LOCK_FREE_QUEUE_H__ -- Gitblit v1.8.0