From af100db4697a85522893ebbffbf2de2741988265 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期一, 20 七月 2020 11:13:41 +0800
Subject: [PATCH] update
---
queue/include/shm_queue.h | 57 ++++++++++++++++++++++++++++++++++++++-------------------
1 files changed, 38 insertions(+), 19 deletions(-)
diff --git a/queue/include/shm_queue.h b/queue/include/shm_queue.h
index 70cd9cc..97eaa9e 100644
--- a/queue/include/shm_queue.h
+++ b/queue/include/shm_queue.h
@@ -6,6 +6,7 @@
#include "hashtable.h"
#include "lock_free_queue.h"
#include "logger_factory.h"
+#include "shm_allocator.h"
// default Queue size
// #define LOCK_FREE_Q_DEFAULT_SIZE 16
@@ -13,19 +14,6 @@
template < typename ELEM_T>
class SHMQueue
{
-
-private:
- static hashtable_t * getHashTable() {
- static hashtable_t *hashtable = NULL;
-
- if(hashtable == NULL) {
- int first = mm_init(sizeof(hashtable_t), (void **)&hashtable);
- if (first)
- hashtable_init(hashtable);
- }
- return hashtable;
-
- }
private:
const int KEY;
@@ -52,10 +40,14 @@
inline ELEM_T& operator[](unsigned i);
+ static void remove_queues_exclue(int *keys, size_t length);
+private:
+
+
protected:
/// @brief the actual queue-> methods are forwarded into the real
/// implementation
- LockFreeQueue<ELEM_T>* queue;
+ LockFreeQueue<ELEM_T, SHM_Allocator>* queue;
private:
/// @brief disable copy constructor declaring it private
@@ -64,13 +56,39 @@
template < typename ELEM_T >
+void SHMQueue<ELEM_T>::remove_queues_exclue(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 >
SHMQueue<ELEM_T>::SHMQueue(int key, size_t qsize): KEY(key)
{
- hashtable_t *hashtable = getHashTable();
+ hashtable_t *hashtable = mm_get_hashtable();
+ queue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, key);
//LockFreeQueue<int, 10000> q;
- if ((queue = (LockFreeQueue<ELEM_T> *)hashtable_get(hashtable, key)) == NULL ) {
- queue = new LockFreeQueue<ELEM_T>(qsize);
+ if (queue == NULL || (void *)queue == (void *)1) {
+ queue = new LockFreeQueue<ELEM_T, SHM_Allocator>(qsize);
hashtable_put(hashtable, key, (void *)queue);
}
queue->reference++;
@@ -82,10 +100,11 @@
{
queue->reference--;
LoggerFactory::getLogger().debug("SHMQueue destructor reference===%d", queue->reference.load());
- if(queue->reference == 0) {
+ if(queue->reference.load() == 0) {
delete queue;
- hashtable_t *hashtable = getHashTable();
+ hashtable_t *hashtable = mm_get_hashtable();
hashtable_remove(hashtable, KEY);
+ LoggerFactory::getLogger().debug("SHMQueue destructor delete queue");
}
}
--
Gitblit v1.8.0