From 21f03d8d05e5c04953e31d7e4360346031603511 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 04 二月 2021 11:47:50 +0800
Subject: [PATCH] 修改注解
---
src/queue/shm_queue.h | 220 ++++++++++++++++++++++++------------------------------
1 files changed, 98 insertions(+), 122 deletions(-)
diff --git a/src/queue/shm_queue.h b/src/queue/shm_queue.h
index 5d2d9b6..3a7750e 100644
--- a/src/queue/shm_queue.h
+++ b/src/queue/shm_queue.h
@@ -12,46 +12,48 @@
#include "shm_allocator.h"
#include "usg_common.h"
#include "array_lock_free_sem_queue.h"
+#include "lock_free_queue.h"
#include "bus_error.h"
template <typename ELEM_T> class SHMQueue {
private:
- const int KEY;
+ const int mkey;
+ hashtable_t * hashtable;
+ // 鏄惁鏄痥ey瀵瑰簲鐨勫叡浜槦鍒楃殑鐪熸鎷ユ湁鑰咃紝涔熷氨鏄鏄痓ind鍒発ey涓婄殑锛屼笉鏄痑ttach鍒発ey涓婄殑瀵硅薄
+ bool owner;
+ size_t mqsize;
public:
/// @brief constructor of the class
- SHMQueue(int key = 0, size_t qsize = 16);
+ SHMQueue(size_t qsize = 16);
~SHMQueue();
- void force_destroy();
+ bool bind(int key, bool force) ;
+ bool attach(int key);
- inline uint32_t size();
+ int get_key();
- inline bool full();
- inline bool empty();
+ uint32_t size();
- inline int push(const ELEM_T &a_data);
- inline int push_nowait(const ELEM_T &a_data);
- inline int push_timeout(const ELEM_T &a_data, const struct timespec *timeout);
- inline int pop(ELEM_T &a_data);
- inline int pop_nowait(ELEM_T &a_data);
- inline int pop_timeout(ELEM_T &a_data, struct timespec *timeout);
+ bool full();
+ bool empty();
- inline ELEM_T &operator[](unsigned i);
+ int push(const ELEM_T &a_data, const struct timespec *timeout=NULL, int flag=0);
+ int pop(ELEM_T &a_data, const struct timespec *timeout=NULL, int flag=0);
+
+ ELEM_T &operator[](unsigned i);
// @deprecate
static size_t remove_queues_exclude(int keys[], size_t length);
- static size_t remove_queues(int keys[], size_t length);
- static size_t remove_queue(int key);
private:
protected:
/// @brief the actual queue-> methods are forwarded into the real
/// implementation
- ArrayLockFreeSemQueue<ELEM_T, SHM_Allocator> *queue;
+ LockFreeQueue<ELEM_T, SHM_Allocator> *queue;
private:
/// @brief disable copy constructor declaring it private
@@ -64,7 +66,7 @@
hashtable_t *hashtable = mm_get_hashtable();
std::set<int> *keyset = hashtable_keyset(hashtable);
std::set<int>::iterator keyItr;
- ArrayLockFreeSemQueue<ELEM_T, SHM_Allocator> *mqueue;
+ LockFreeQueue<ELEM_T, SHM_Allocator> *mqueue;
bool found;
size_t count = 0;
for (keyItr = keyset->begin(); keyItr != keyset->end(); keyItr++) {
@@ -77,7 +79,7 @@
}
if (!found) {
// 閿�姣佸叡浜唴瀛樼殑queue
- mqueue = (ArrayLockFreeSemQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, *keyItr);
+ mqueue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, *keyItr);
delete mqueue;
hashtable_remove(hashtable, *keyItr);
count++;
@@ -88,146 +90,120 @@
}
-template <typename ELEM_T>
-size_t SHMQueue<ELEM_T>::remove_queues(int keys[], size_t length) {
- hashtable_t *hashtable = mm_get_hashtable();
- ArrayLockFreeSemQueue<ELEM_T, SHM_Allocator> *mqueue;
- size_t count = 0;
- for(int i = 0; i< length; i++) {
- // 閿�姣佸叡浜唴瀛樼殑queue
- mqueue = (ArrayLockFreeSemQueue<ELEM_T, SHM_Allocator> *)mm_get_by_key(keys[i]);
- delete mqueue;
- hashtable_remove(hashtable, keys[i]);
- count++;
- }
- return count;
-}
template <typename ELEM_T>
-size_t SHMQueue<ELEM_T>::remove_queue(int key) {
- int keys[] = {key};
- return remove_queues(keys, 1);
-}
+SHMQueue<ELEM_T>::SHMQueue(size_t qsize): mqsize(qsize) {
-template <typename ELEM_T>
-SHMQueue<ELEM_T>::SHMQueue(int key, size_t qsize) : KEY(key) {
-
- hashtable_t *hashtable = mm_get_hashtable();
- queue = (ArrayLockFreeSemQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, key);
- if (queue == NULL || (void *)queue == (void *)1) {
- queue = new ArrayLockFreeSemQueue<ELEM_T, SHM_Allocator>(qsize);
- hashtable_put(hashtable, key, (void *)queue);
- }
+ hashtable = mm_get_hashtable();
+ owner = false;
+ mkey = 0;
+ // queue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, key);
+ // 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() {
LoggerFactory::getLogger()->debug("SHMQueue destroy");
- delete queue;
- queue = NULL;
- hashtable_t *hashtable = mm_get_hashtable();
- hashtable_remove(hashtable, KEY);
+ if(owner) {
+ delete queue;
+ hashtable_remove(hashtable, mkey);
+ }
+
}
-template <typename ELEM_T> inline uint32_t SHMQueue<ELEM_T>::size() {
+template <typename ELEM_T>
+bool SHMQueue<ELEM_T>::bind(int key, bool force) {
+
+
+ hashtable_lock(hashtable);
+ void *tmp_ptr = hashtable_get(hashtable, key);
+ if (tmp_ptr == NULL || tmp_ptr == (void *)1 || force) {
+ queue = new LockFreeQueue<ELEM_T, SHM_Allocator>(mqsize);
+ hashtable_put(hashtable, key, (void *)queue);
+ mkey = key;
+ owner = true;
+ hashtable_unlock(hashtable);
+ return true;
+ }
+
+ hashtable_unlock(hashtable);
+ return false;
+}
+
+template <typename ELEM_T>
+bool SHMQueue<ELEM_T>::attach(int key) {
+ void *tmp_ptr = hashtable_get(hashtable, key);
+ if (tmp_ptr == NULL || tmp_ptr == (void *)1) {
+ return false;
+ }
+ mkey = key;
+ queue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)tmp_ptr;
+ return true;
+}
+
+template <typename ELEM_T> int SHMQueue<ELEM_T>::get_key() {
+ return mkey;
+}
+
+
+template <typename ELEM_T> uint32_t SHMQueue<ELEM_T>::size() {
return queue->size();
}
-template <typename ELEM_T> inline bool SHMQueue<ELEM_T>::full() {
+template <typename ELEM_T> bool SHMQueue<ELEM_T>::full() {
return queue->full();
}
-template <typename ELEM_T> inline bool SHMQueue<ELEM_T>::empty() {
+template <typename ELEM_T> bool SHMQueue<ELEM_T>::empty() {
return queue->empty();
}
+
+
+
template <typename ELEM_T>
-inline int SHMQueue<ELEM_T>::push(const ELEM_T &a_data) {
- int rv = queue->push(a_data);
- if(rv == -1) {
- return errno;
- } else {
+int SHMQueue<ELEM_T>::push(const ELEM_T &a_data, const struct timespec *timeout, int flag) {
+
+
+ int rv = queue->push(a_data, timeout, flag);
+ if(rv == 0) {
return 0;
}
+ if(rv == ETIMEDOUT)
+ return EBUS_TIMEOUT;
+ else {
+ LoggerFactory::getLogger()->error("LockFreeQueue push_timeout: %s", bus_strerror(rv));
+ return rv;
+ }
}
+
template <typename ELEM_T>
-inline int SHMQueue<ELEM_T>::push_nowait(const ELEM_T &a_data) {
- int rv = queue->push(a_data, NULL, LOCK_FREE_QUEUE_NOWAIT);
- if(rv == -1) {
- if (errno == EAGAIN)
- return EAGAIN;
- else {
- err_msg(errno, "LockFreeQueue push_nowait");
- return errno;
- }
- }
- return 0;
-}
+int SHMQueue<ELEM_T>::pop(ELEM_T &a_data, const struct timespec *timeout, int flag) {
-template <typename ELEM_T>
-inline int SHMQueue<ELEM_T>::push_timeout(const ELEM_T &a_data, const struct timespec *timeout) {
-
- int rv = queue->push(a_data, timeout, LOCK_FREE_QUEUE_TIMEOUT);
- if(rv == -1) {
- if(errno == ETIMEDOUT)
- return EBUS_TIMEOUT;
- else {
- LoggerFactory::getLogger()->error(errno, "LockFreeQueue push_timeout");
- return errno;
- }
- }
- return 0;
-}
-
-template <typename ELEM_T> inline int SHMQueue<ELEM_T>::pop(ELEM_T &a_data) {
- // printf("SHMQueue pop before\n");
- int rv = queue->pop(a_data);
- // printf("SHMQueue after before\n");
- if(rv == -1) {
- return errno;
- } else {
+ int rv = queue->pop(a_data, timeout, flag);
+ if(rv == 0) {
return 0;
}
-}
-template <typename ELEM_T>
-inline int SHMQueue<ELEM_T>::pop_nowait(ELEM_T &a_data) {
- int rv = queue->pop(a_data, NULL, LOCK_FREE_QUEUE_NOWAIT);
-
- if(rv == -1) {
- if (errno == EAGAIN)
- return errno;
- else {
- LoggerFactory::getLogger()->error(errno, " SHMQueue pop_nowait");
- return errno;
- }
+
+ if(rv == ETIMEDOUT)
+ return EBUS_TIMEOUT;
+ else {
+ LoggerFactory::getLogger()->error("LockFreeQueue pop_timeout: %s", bus_strerror(rv));
+ return rv;
}
- return 0;
+ return rv;
}
template <typename ELEM_T>
-inline int SHMQueue<ELEM_T>::pop_timeout(ELEM_T &a_data, struct timespec *timeout) {
-
- int rv;
- rv = queue->pop(a_data, timeout, LOCK_FREE_QUEUE_TIMEOUT);
- if(rv == -1) {
- if (errno == ETIMEDOUT) {
- return EBUS_TIMEOUT;
- } else {
- LoggerFactory::getLogger()->error(errno, " SHMQueue pop_timeout");
- return errno;
- }
- }
- return 0;
-
-}
-
-template <typename ELEM_T>
-inline ELEM_T &SHMQueue<ELEM_T>::operator[](unsigned i) {
+ELEM_T &SHMQueue<ELEM_T>::operator[](unsigned i) {
return queue->operator[](i);
}
--
Gitblit v1.8.0