From 27d8bc7cad4a8f68c2da3efbb77f45ec70ae40e6 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期三, 05 一月 2022 14:11:39 +0800
Subject: [PATCH] pb.go MsgQueryProcReply add subLocalTopics and subNetTopics

---
 src/queue/lock_free_queue.h |   98 +++++++++++++++++++++++++++++++++---------------
 1 files changed, 67 insertions(+), 31 deletions(-)

diff --git a/src/queue/lock_free_queue.h b/src/queue/lock_free_queue.h
index ef6a893..6363ccb 100644
--- a/src/queue/lock_free_queue.h
+++ b/src/queue/lock_free_queue.h
@@ -6,15 +6,21 @@
 
 #include <usg_common.h>
 #include <assert.h> // assert()
-#include "mem_pool.h"
+#include "shm_mm.h"
 #include "sem_util.h"
 #include "logger_factory.h"
 #include "shm_allocator.h"
 #include "psem.h"
 #include "bus_error.h"
 #include "bus_def.h"
+
 // default Queue size
-#define LOCK_FREE_Q_DEFAULT_SIZE 16
+#define LOCK_FREE_Q_DEFAULT_SIZE 320
+
+
+#define LOCK_FREE_Q_ST_OPENED 0
+
+#define LOCK_FREE_Q_ST_CLOSED 1
 
 // static Logger *logger = LoggerFactory::getLogger();
 // define this macro if calls to "size" must return the real size of the
@@ -82,9 +88,11 @@
   sem_t slots;
   sem_t items;
 
+  time_t createTime;
+  time_t closeTime;
+  int status;
 
 public:
-  sem_t mutex;
 
   LockFreeQueue(size_t qsize = LOCK_FREE_Q_DEFAULT_SIZE);
 
@@ -93,7 +101,10 @@
   /// template
   ~LockFreeQueue();
 
-  std::atomic_uint reference;
+  inline void  close();
+  inline bool isClosed();
+
+  // std::atomic_uint reference;
   /// @brief constructor of the class
 
 
@@ -116,6 +127,20 @@
   inline bool empty();
 
   inline ELEM_T &operator[](unsigned i);
+
+  
+
+  time_t getCreateTime() {
+    return createTime;
+  }
+
+  time_t getCloseTime() {
+    return closeTime;
+  }
+
+  int getStatus() {
+    return status;
+  }
 
   /// @brief push an element at the tail of the queue
   /// @param the element to insert in the queue
@@ -151,15 +176,15 @@
   typename ELEM_T,
   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;
+LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::LockFreeQueue(size_t qsize): m_qImpl(qsize) {
+  //std::cout << "LockFreeQueue init reference=" << reference << std::endl;
   if (sem_init(&slots, 1, qsize) == -1)
     err_exit(errno, "LockFreeQueue sem_init");
   if (sem_init(&items, 1, 0) == -1)
     err_exit(errno, "LockFreeQueue sem_init");
-  if (sem_init(&mutex, 1, 1) == -1)
-    err_exit(errno, "LockFreeQueue sem_init");
-
+  
+  createTime = time(NULL);
+  status = LOCK_FREE_Q_ST_OPENED;
 
 }
 
@@ -168,17 +193,32 @@
   typename ELEM_T,
   typename Allocator,
   template<typename T, typename AT> class Q_TYPE>
+inline void LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::close() {
+  status = LOCK_FREE_Q_ST_CLOSED;
+  closeTime = time(NULL); 
+}
+
+template<
+  typename ELEM_T,
+  typename Allocator,
+  template<typename T, typename AT> class Q_TYPE>
+inline bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::isClosed() {
+  return status == LOCK_FREE_Q_ST_CLOSED;
+}
+
+
+template<
+  typename ELEM_T,
+  typename Allocator,
+  template<typename T, typename AT> class Q_TYPE>
 LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::~LockFreeQueue() {
-  // LoggerFactory::getLogger()->debug("LockFreeQueue desctroy");
   if (sem_destroy(&slots) == -1) {
     err_exit(errno, "LockFreeQueue sem_destroy");
   }
   if (sem_destroy(&items) == -1) {
     err_exit(errno, "LockFreeQueue sem_destroy");
   }
-  if (sem_destroy(&mutex) == -1) {
-    err_exit(errno, "LockFreeQueue sem_destroy");
-  }
+  
 }
 
 template<
@@ -210,11 +250,10 @@
   typename Allocator,
   template<typename T, typename AT> class Q_TYPE>
 int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push(const ELEM_T &a_data, const struct timespec *timeout, int flag) {
-  // LoggerFactory::getLogger()->debug("==================LockFreeQueue push before\n");
-  // sigset_t mask_all, pre;
-  // sigfillset(&mask_all);
+  sigset_t mask_all, pre;
+  sigfillset(&mask_all);
 
-  // sigprocmask(SIG_BLOCK, &mask_all, &pre);
+  sigprocmask(SIG_BLOCK, &mask_all, &pre);
 
   if ((flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) {
     if (psem_trywait(&slots) == -1) {
@@ -233,13 +272,12 @@
 
   if (m_qImpl.push(a_data)) {
     psem_post(&items);
-    // sigprocmask(SIG_SETMASK, &pre, NULL);
-    LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n");
+    sigprocmask(SIG_SETMASK, &pre, NULL);
     return 0;
   }
 
   LABEL_FAILTURE:
-  // sigprocmask(SIG_SETMASK, &pre, NULL);
+  sigprocmask(SIG_SETMASK, &pre, NULL);
   return errno;
 }
 
@@ -247,20 +285,18 @@
   typename Allocator,
   template<typename T, typename AT> class Q_TYPE>
 int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop(ELEM_T &a_data, const struct timespec *timeout, int flag) {
-  // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before....");
 
-  // sigset_t mask_all, pre;
-  // sigfillset(&mask_all);
+  sigset_t mask_all, pre;
+  sigfillset(&mask_all);
 
-  // sigprocmask(SIG_BLOCK, &mask_all, &pre);
+  sigprocmask(SIG_BLOCK, &mask_all, &pre);
 
   if ((flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) {
     if (psem_trywait(&items) == -1) {
       goto LABEL_FAILTURE;
     }
   } else if ((flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) {
-    // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before. flag=%d ,  %d\n", flag, timeout->tv_sec);
-    if (psem_timedwait(&items, timeout) == -1) {
+   if (psem_timedwait(&items, timeout) == -1) {
       goto LABEL_FAILTURE;
     }
   } else {
@@ -269,16 +305,15 @@
     }
   }
 
-
   if (m_qImpl.pop(a_data)) {
     psem_post(&slots);
-    // sigprocmask(SIG_SETMASK, &pre, NULL);
-    // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n");
+    sigprocmask(SIG_SETMASK, &pre, NULL);
     return 0;
   }
 
+
   LABEL_FAILTURE:
-  // sigprocmask(SIG_SETMASK, &pre, NULL);
+  sigprocmask(SIG_SETMASK, &pre, NULL);
   return errno;
 }
 
@@ -301,7 +336,8 @@
   typename Allocator,
   template<typename T, typename AT> class Q_TYPE>
 void LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::operator delete(void *p) {
-  return Allocator::deallocate(p);
+  LockFreeQueue<ELEM_T, Allocator, Q_TYPE> * _que =  (LockFreeQueue<ELEM_T, Allocator, Q_TYPE> * )p;
+  Allocator::deallocate(p);
 }
 
 // include implementation files

--
Gitblit v1.8.0