From 2e1afff475181677a3dd38ab6e6d5632f8a70590 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 27 七月 2020 10:55:10 +0800 Subject: [PATCH] udpate --- src/queue/include/shm_queue.h | 265 ++++++++++++++++++++++++----------------------------- 1 files changed, 120 insertions(+), 145 deletions(-) diff --git a/src/queue/include/shm_queue.h b/src/queue/include/shm_queue.h index 0d8bbaf..81913d0 100644 --- a/src/queue/include/shm_queue.h +++ b/src/queue/include/shm_queue.h @@ -1,191 +1,166 @@ #ifndef __SHM_QUEUE_H__ #define __SHM_QUEUE_H__ -#include "usg_common.h" #include "hashtable.h" #include "lock_free_queue.h" #include "logger_factory.h" -#include "shm_allocator.h" #include "sem_util.h" +#include "shm_allocator.h" +#include "usg_common.h" // default Queue size // #define LOCK_FREE_Q_DEFAULT_SIZE 16 - -template < typename ELEM_T> -class SHMQueue -{ + +template <typename ELEM_T> class SHMQueue { private: - const int KEY; - + const int KEY; + public: - /// @brief constructor of the class - SHMQueue(int key=0, size_t qsize = LOCK_FREE_Q_DEFAULT_SIZE); - - - ~SHMQueue(); + /// @brief constructor of the class + SHMQueue(int key = 0, size_t qsize = LOCK_FREE_Q_DEFAULT_SIZE); - - inline uint32_t size(); - - inline bool full(); - inline bool empty(); - - inline bool push(const ELEM_T &a_data); - inline bool push_nowait(const ELEM_T &a_data); - inline bool push_timeout(const ELEM_T &a_data, const struct timespec * timeout); - inline bool pop(ELEM_T &a_data); - inline bool pop_nowait(ELEM_T &a_data); - inline bool pop_timeout(ELEM_T &a_data, struct timespec * timeout); + ~SHMQueue(); - inline ELEM_T& operator[](unsigned i); + inline uint32_t size(); - static void remove_queues_exclude(int *keys, size_t length); + inline bool full(); + inline bool empty(); + + inline bool push(const ELEM_T &a_data); + inline bool push_nowait(const ELEM_T &a_data); + inline bool push_timeout(const ELEM_T &a_data, + const struct timespec *timeout); + inline bool pop(ELEM_T &a_data); + inline bool pop_nowait(ELEM_T &a_data); + inline bool pop_timeout(ELEM_T &a_data, struct timespec *timeout); + + inline ELEM_T &operator[](unsigned i); + + static void remove_queues_exclude(int *keys, size_t length); + private: - - protected: - /// @brief the actual queue-> methods are forwarded into the real - /// implementation - LockFreeQueue<ELEM_T, SHM_Allocator>* queue; + /// @brief the actual queue-> methods are forwarded into the real + /// implementation + LockFreeQueue<ELEM_T, SHM_Allocator> *queue; private: - /// @brief disable copy constructor declaring it private - SHMQueue<ELEM_T>(const SHMQueue<ELEM_T> &a_src); + /// @brief disable copy constructor declaring it private + SHMQueue<ELEM_T>(const SHMQueue<ELEM_T> &a_src); }; +template <typename ELEM_T> +void SHMQueue<ELEM_T>::remove_queues_exclude(int *keys, size_t length) { + hashtable_t *hashtable = mm_get_hashtable(); + std::set<int> *keyset = hashtable_keyset(hashtable); + std::set<int>::iterator keyItr; + LockFreeQueue<ELEM_T, SHM_Allocator> *mqueue; + bool found; + for (keyItr = keyset->begin(); keyItr != keyset->end(); keyItr++) { + found = false; + for (size_t i = 0; i < length; i++) { + if (*keyItr == keys[i]) { + found = true; + break; + } + } + if (!found) { + mqueue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, + *keyItr); + delete mqueue; + } + } + delete keyset; +} -template < typename ELEM_T > -void SHMQueue<ELEM_T>::remove_queues_exclude(int *keys, size_t length) -{ +template <typename ELEM_T> +SHMQueue<ELEM_T>::SHMQueue(int key, size_t qsize) : KEY(key) { + + hashtable_t *hashtable = mm_get_hashtable(); + queue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, key); + // LockFreeQueue<int, 10000> q; + if (queue == NULL || (void *)queue == (void *)1) { + queue = new LockFreeQueue<ELEM_T, SHM_Allocator>(qsize); + hashtable_put(hashtable, key, (void *)queue); + } + queue->reference++; + LoggerFactory::getLogger().debug("SHMQueue constructor reference===%d", + queue->reference.load()); +} + +template <typename ELEM_T> SHMQueue<ELEM_T>::~SHMQueue() { + SemUtil::dec(queue->mutex); + queue->reference--; + // LoggerFactory::getLogger().debug("SHMQueue destructor reference===%d", + // queue->reference.load()); + if (queue->reference.load() == 0) { + delete queue; hashtable_t *hashtable = mm_get_hashtable(); - std::set<int>* keyset = hashtable_keyset(hashtable); - std::set<int>::iterator keyItr; - LockFreeQueue<ELEM_T, SHM_Allocator>* mqueue; - bool found; - for (keyItr = keyset->begin(); keyItr != keyset->end(); keyItr++) { - found = false; - for(size_t i = 0; i < length; i++) { - if(*keyItr == keys[i]) { - found = true; - break; - } - } - if(!found) { - mqueue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, *keyItr); - delete mqueue; - } - } - delete keyset; - -} - -template < typename ELEM_T > -SHMQueue<ELEM_T>::SHMQueue(int key, size_t qsize): KEY(key) -{ - - hashtable_t *hashtable = mm_get_hashtable(); - queue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, key); - //LockFreeQueue<int, 10000> q; - if (queue == NULL || (void *)queue == (void *)1) { - queue = new LockFreeQueue<ELEM_T, SHM_Allocator>(qsize); - hashtable_put(hashtable, key, (void *)queue); - } - queue->reference++; - LoggerFactory::getLogger().debug("SHMQueue constructor reference===%d", queue->reference.load()); + hashtable_remove(hashtable, KEY); + // LoggerFactory::getLogger().debug("SHMQueue destructor delete queue\n"); + } else { + SemUtil::inc(queue->mutex); + } } -template < typename ELEM_T > -SHMQueue<ELEM_T>::~SHMQueue() -{ - SemUtil::dec( queue->mutex); - queue->reference--; -//LoggerFactory::getLogger().debug("SHMQueue destructor reference===%d", queue->reference.load()); - if(queue->reference.load() == 0) { - delete queue; - hashtable_t *hashtable = mm_get_hashtable(); - hashtable_remove(hashtable, KEY); -// LoggerFactory::getLogger().debug("SHMQueue destructor delete queue\n"); - } else { - SemUtil::inc(queue->mutex); - } - +template <typename ELEM_T> inline uint32_t SHMQueue<ELEM_T>::force_destroy() { + SemUtil::dec(queue->mutex); + delete queue; + hashtable_t *hashtable = mm_get_hashtable(); + hashtable_remove(hashtable, KEY); + SemUtil::inc(queue->mutex); } -template < typename ELEM_T > -inline uint32_t SHMQueue<ELEM_T>::size() -{ - return queue->size(); -} - -template < typename ELEM_T > -inline bool SHMQueue<ELEM_T>::full() -{ - return queue->full(); +template <typename ELEM_T> inline uint32_t SHMQueue<ELEM_T>::size() { + return queue->size(); } -template < typename ELEM_T > -inline bool SHMQueue<ELEM_T>::empty() -{ - return queue->empty(); -} - - -template < typename ELEM_T > -inline bool SHMQueue<ELEM_T>::push(const ELEM_T &a_data) -{ - return queue->push(a_data); - +template <typename ELEM_T> inline bool SHMQueue<ELEM_T>::full() { + return queue->full(); } -template < - typename ELEM_T > -inline bool SHMQueue<ELEM_T>::push_nowait(const ELEM_T &a_data) -{ - return queue->push_nowait(a_data); - +template <typename ELEM_T> inline bool SHMQueue<ELEM_T>::empty() { + return queue->empty(); } -template < typename ELEM_T > -inline bool SHMQueue<ELEM_T>::push_timeout(const ELEM_T &a_data, const struct timespec * timeout) -{ - - return queue->push_timeout(a_data, timeout); - +template <typename ELEM_T> +inline bool SHMQueue<ELEM_T>::push(const ELEM_T &a_data) { + return queue->push(a_data); } - - - -template < typename ELEM_T > -inline bool SHMQueue<ELEM_T>::pop(ELEM_T &a_data) -{ -// printf("SHMQueue pop before\n"); - int rv = queue->pop(a_data); -// printf("SHMQueue after before\n"); - return rv; - +template <typename ELEM_T> +inline bool SHMQueue<ELEM_T>::push_nowait(const ELEM_T &a_data) { + return queue->push_nowait(a_data); } -template < typename ELEM_T > -inline bool SHMQueue<ELEM_T>::pop_nowait(ELEM_T &a_data) -{ - return queue->pop_nowait(a_data); - +template <typename ELEM_T> +inline bool SHMQueue<ELEM_T>::push_timeout(const ELEM_T &a_data, + const struct timespec *timeout) { + + return queue->push_timeout(a_data, timeout); } - -template < typename ELEM_T > -inline bool SHMQueue<ELEM_T>::pop_timeout(ELEM_T &a_data, struct timespec * timeout) -{ - return queue->pop_timeout(a_data, timeout); - +template <typename ELEM_T> inline bool SHMQueue<ELEM_T>::pop(ELEM_T &a_data) { + // printf("SHMQueue pop before\n"); + int rv = queue->pop(a_data); + // printf("SHMQueue after before\n"); + return rv; } -template < typename ELEM_T > -inline ELEM_T& SHMQueue<ELEM_T>::operator[](unsigned i) { - return queue->operator[](i); +template <typename ELEM_T> +inline bool SHMQueue<ELEM_T>::pop_nowait(ELEM_T &a_data) { + return queue->pop_nowait(a_data); } +template <typename ELEM_T> +inline bool SHMQueue<ELEM_T>::pop_timeout(ELEM_T &a_data, + struct timespec *timeout) { + return queue->pop_timeout(a_data, timeout); +} +template <typename ELEM_T> +inline ELEM_T &SHMQueue<ELEM_T>::operator[](unsigned i) { + return queue->operator[](i); +} #endif -- Gitblit v1.8.0