From f85c9b875b060681b51f57b15074ba1c7c9f5636 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期一, 20 七月 2020 11:10:02 +0800
Subject: [PATCH] update

---
 queue/include/lock_free_queue.h |  135 +++++++++++++++++++++++++++------------------
 1 files changed, 81 insertions(+), 54 deletions(-)

diff --git a/queue/include/lock_free_queue.h b/queue/include/lock_free_queue.h
index db1ecfb..f34079f 100644
--- a/queue/include/lock_free_queue.h
+++ b/queue/include/lock_free_queue.h
@@ -3,9 +3,10 @@
 
 #include <usg_common.h>
 #include <assert.h> // assert()
-#include "mm.h" 
+#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,24 +67,24 @@
 ///        by default)
 template <
     typename ELEM_T, 
-    template <typename T> class Q_TYPE = LinkedLockFreeQueue >
+    typename Allocator = SHM_Allocator,
+    template <typename T, typename AT> class Q_TYPE = ArrayLockFreeQueue
+     >
 class LockFreeQueue
 {
-
-    template < typename ELEM_T_ >
-    friend class SHMQueue;
 
 private:
     int slots;
     int items;
-protected:
-     LockFreeQueue(size_t qsize = LOCK_FREE_Q_DEFAULT_SIZE);
+   
+public:
+    // int mutex;
+    LockFreeQueue(size_t qsize = LOCK_FREE_Q_DEFAULT_SIZE);
     
     /// @brief destructor of the class. 
     /// Note it is not virtual since it is not expected to inherit from this
     /// template
     ~LockFreeQueue();
-public:
     std::atomic_uint reference;    
     /// @brief constructor of the class
    
@@ -133,28 +134,31 @@
 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);
     items = SemUtil::get(IPC_PRIVATE, 0);
+    // mutex = SemUtil::get(IPC_PRIVATE, 1);
 }
 
 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 +167,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,11 +195,13 @@
 
 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");
+        err_msg(errno, "LockFreeQueue push");
+        return false;
     }
 
     if ( m_qImpl.push(a_data) ) {
@@ -205,14 +214,18 @@
 
 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)
             return false;
-        else
-            err_exit(errno, "push_nowait");
+        else {
+            err_msg(errno, "LockFreeQueue push_nowait");
+            return false;
+        }
+
     }
 
     if ( m_qImpl.push(a_data)) {
@@ -225,15 +238,18 @@
 
 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) {
         if (errno == EAGAIN)
             return false;
-        else 
-            err_exit(errno, "push_timeout");
+        else {
+            err_msg(errno, "LockFreeQueue push_timeout");
+            return false;
+        }
     }
 
     if (m_qImpl.push(a_data)){
@@ -249,11 +265,13 @@
 
 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_exit(errno, "remove");
+        err_msg(errno, "LockFreeQueue pop");
+        return false;
     }
 
     if (m_qImpl.pop(a_data)) {
@@ -266,14 +284,17 @@
 
 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)
             return false;
-        else
-            err_exit(errno, "remove_nowait");
+        else {
+            err_msg(errno, "LockFreeQueue pop_nowait");
+            return false;
+        }
     }
 
     if (m_qImpl.pop(a_data)) {
@@ -287,14 +308,17 @@
  
 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)
             return false;
-        else 
-            err_exit(errno, "remove_timeout");
+        else {
+            err_msg(errno, "LockFreeQueue pop_timeout");
+            return false;
+        }
     }
 
     if (m_qImpl.pop(a_data)) {
@@ -307,27 +331,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 mm_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::allocate(size);
 }
 
 template <
     typename ELEM_T, 
-    template <typename T> class Q_TYPE>
-void LockFreeQueue<ELEM_T, Q_TYPE>::operator delete(void *p) {
-    return mm_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::deallocate(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