From 09a82c2ece4caadad0baa0d1f3b84f1506363fdd Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期五, 22 一月 2021 11:58:33 +0800
Subject: [PATCH] update

---
 src/queue/lock_free_queue.h |  148 +++++++++++++++++-------------------------------
 1 files changed, 53 insertions(+), 95 deletions(-)

diff --git a/src/queue/lock_free_queue.h b/src/queue/lock_free_queue.h
index 6a610dc..01e597c 100644
--- a/src/queue/lock_free_queue.h
+++ b/src/queue/lock_free_queue.h
@@ -1,3 +1,6 @@
+/**
+ * encapsulate array_lock_free_queue, add semphore. populate in kernal space.
+ */
 #ifndef __LOCK_FREE_QUEUE_H__
 #define __LOCK_FREE_QUEUE_H__
 
@@ -7,7 +10,8 @@
 #include "sem_util.h"
 #include "logger_factory.h"
 #include "shm_allocator.h"
-#include "px_sem_util.h"
+#include "psem.h"
+#include "bus_error.h"
 
 // default Queue size
 #define LOCK_FREE_Q_DEFAULT_SIZE 16
@@ -119,17 +123,17 @@
     /// structures to be inserted in the queue you should think of instantiate the template
     /// of the queue as a pointer to that large structure
     /// @return true if the element was inserted in the queue. False if the queue was full
-    bool push(const ELEM_T &a_data);
-    bool push_nowait(const ELEM_T &a_data);
-    bool push_timeout(const ELEM_T &a_data, const struct timespec * timeout);
+    int push(const ELEM_T &a_data);
+    int push_nowait(const ELEM_T &a_data);
+    int push_timeout(const ELEM_T &a_data, const struct timespec * timeout);
 
     /// @brief pop the element at the head of the queue
     /// @param a reference where the element in the head of the queue will be saved to
     /// Note that the a_data parameter might contain rubbish if the function returns false
     /// @return true if the element was successfully extracted from the queue. False if the queue was empty
-    bool pop(ELEM_T &a_data);
-    bool pop_nowait(ELEM_T &a_data);
-    bool pop_timeout(ELEM_T &a_data, struct timespec * timeout);
+    int pop(ELEM_T &a_data);
+    int pop_nowait(ELEM_T &a_data);
+    int pop_timeout(ELEM_T &a_data, struct timespec * timeout);
 
 
     void *operator new(size_t size);
@@ -213,20 +217,19 @@
     typename ELEM_T, 
     typename Allocator,
     template <typename T, typename AT> class Q_TYPE>
-bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push(const ELEM_T &a_data)
+int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push(const ELEM_T &a_data)
 {
-// LoggerFactory::getLogger()->debug("==================LockFreeQueue push before\n");   
-    if (sem_wait(&slots) == -1) {
-        err_msg(errno, "LockFreeQueue push");
-        return false;
+ LoggerFactory::getLogger()->debug("==================LockFreeQueue push before\n");   
+    if (psem_wait(&slots) == -1) {
+        return -1;
     }
     
     if ( m_qImpl.push(a_data) ) {
-        sem_post(&items);   
-// LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n");   
-        return true;
+        psem_post(&items);   
+LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n");   
+        return 0;
     }
-    return false;
+    return -1;
     
 }
 
@@ -234,23 +237,17 @@
     typename ELEM_T, 
     typename Allocator,
     template <typename T, typename AT> class Q_TYPE>
-bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push_nowait(const ELEM_T &a_data)
+int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push_nowait(const ELEM_T &a_data)
 {
-    if (sem_trywait(&slots) == -1) {
-        if (errno == EAGAIN)
-            return false;
-        else {
-            err_msg(errno, "LockFreeQueue push_nowait");
-            return false;
-        }
-
+    if (psem_trywait(&slots) == -1) {
+        return -1;
     }
 
     if ( m_qImpl.push(a_data)) {
-        sem_post(&items);     
-        return true;
+        psem_post(&items);     
+        return 0;
     }
-    return false;
+    return -1;
     
 }
 
@@ -258,41 +255,19 @@
     typename ELEM_T, 
     typename Allocator,
     template <typename T, typename AT> class Q_TYPE>
-bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push_timeout(const ELEM_T &a_data, const struct timespec * ts)
+int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push_timeout(const ELEM_T &a_data, const struct timespec * ts)
 {
-    // int tmp_sec;
-    // struct timespec timeout;
-    // if (clock_gettime(CLOCK_REALTIME, &timeout) == -1)
-    //     err_exit(errno, "clock_gettime");
-    // timeout.tv_nsec += ts->tv_nsec;
-    // tmp_sec =  timeout.tv_nsec / 10e9;
-    // timeout.tv_nsec =  timeout.tv_nsec - tmp_sec * 10e9;
-    // timeout.tv_sec += ts->tv_sec + tmp_sec;
-
-    struct timespec timeout = PXSemUtil::calc_sem_timeout(ts);
-  // LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout before tv_sec=%d, tv_nsec=%ld", 
-  //   timeout.tv_sec, timeout.tv_nsec);
-
-    while (sem_timedwait(&slots, &timeout) == -1) {
-    //     LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout before tv_sec=%d, tv_nsec=%ld, ETIMEDOUT=%d, errno=%d\n", 
-    // timeout.tv_sec, timeout.tv_nsec, ETIMEDOUT, errno);
-
-        if(errno == ETIMEDOUT)
-            return false;
-        else if(errno == EINTR)
-            continue;
-        else {
-            LoggerFactory::getLogger()->error(errno, "LockFreeQueue push_timeout");
-            return false;
-        }
+LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout before\n");       
+    if ( psem_timedwait(&slots, ts) == -1) {
+        return -1;
     }
 
     if (m_qImpl.push(a_data)){
-        sem_post(&items);   
-// LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout after\n");    
-        return true;
+        psem_post(&items);   
+LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout after\n");    
+        return 0;
     }
-    return false;
+    return -1;
     
 }
 
@@ -303,43 +278,37 @@
     typename ELEM_T, 
     typename Allocator,
     template <typename T, typename AT> class Q_TYPE>
-bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop(ELEM_T &a_data)
+int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop(ELEM_T &a_data)
 {
 
-// LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before\n");
-    if (sem_wait(&items) == -1) {
-        LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop");
-        return false;
+  LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before\n");
+    if (psem_wait(&items) == -1) {
+        return -1;
     }
 
     if (m_qImpl.pop(a_data)) {
-        sem_post(&slots);
-// LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n");      
-        return true;
+        psem_post(&slots);
+ LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n");      
+        return 0;
     }
-    return false;
+    return -1;
 }
 
 template <
     typename ELEM_T, 
     typename Allocator,
     template <typename T, typename AT> class Q_TYPE>
-bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop_nowait(ELEM_T &a_data)
+int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop_nowait(ELEM_T &a_data)
 {
-    if (sem_trywait(&items) == -1) {
-        if (errno == EAGAIN)
-            return false;
-        else {
-            LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop_nowait");
-            return false;
-        }
+    if (psem_trywait(&items) == -1) {
+        return -1;
     }
 
     if (m_qImpl.pop(a_data)) {
-        sem_post(&slots);     
-        return true;
+        psem_post(&slots);     
+        return 0;
     }
-    return false;
+    return -1;
 }
 
  
@@ -347,29 +316,18 @@
     typename ELEM_T, 
     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 * ts)
+int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop_timeout(ELEM_T &a_data, struct timespec * ts)
 {
-// LoggerFactory::getLogger()->debug("==================LockFreeQueue pop_timeout before\n");
-
-     struct timespec timeout = PXSemUtil::calc_sem_timeout(ts);
-
-    while (sem_timedwait(&items, &timeout) == -1) {
-        if (errno == EAGAIN)
-            return false;
-        else if(errno == EINTR)
-            continue;
-        else {
-            LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop_timeout");
-            return false;
-        }
+    if (psem_timedwait(&items, ts) == -1) {
+       return -1;
     }
 
     if (m_qImpl.pop(a_data)) {
-        sem_post(&slots);  
+        psem_post(&slots);  
 // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop_timeout after\n");     
-        return true;
+        return 0;
     }
-    return false;
+    return -1;
     
 }
 

--
Gitblit v1.8.0