From 87f014b618c130b9f06d344ec50622d66da5d041 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 14 一月 2021 15:03:08 +0800
Subject: [PATCH] update
---
src/queue/lock_free_queue.h | 174 ++++++++++++++++++++++++++++++++++-----------------------
1 files changed, 103 insertions(+), 71 deletions(-)
diff --git a/src/queue/lock_free_queue.h b/src/queue/lock_free_queue.h
index ee11da6..338e868 100644
--- a/src/queue/lock_free_queue.h
+++ b/src/queue/lock_free_queue.h
@@ -7,10 +7,13 @@
#include "sem_util.h"
#include "logger_factory.h"
#include "shm_allocator.h"
+#include "px_sem_util.h"
+#include "bus_error.h"
// default Queue size
#define LOCK_FREE_Q_DEFAULT_SIZE 16
+// static Logger *logger = LoggerFactory::getLogger();
// define this macro if calls to "size" must return the real size of the
// queue. If it is undefined that function will try to take a snapshot of
// the queue, but returned value might be bogus
@@ -74,11 +77,13 @@
{
private:
- int slots;
- int items;
+ sem_t slots;
+ sem_t items;
+
+
public:
- int mutex;
+ sem_t mutex;
LockFreeQueue(size_t qsize = LOCK_FREE_Q_DEFAULT_SIZE);
/// @brief destructor of the class.
@@ -115,17 +120,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);
@@ -149,9 +154,14 @@
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);
+ 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");
+
+
}
template <
@@ -161,9 +171,15 @@
LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::~LockFreeQueue()
{
// LoggerFactory::getLogger()->debug("LockFreeQueue desctroy");
- SemUtil::remove(slots);
- SemUtil::remove(items);
- SemUtil::remove(mutex);
+ 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 <
@@ -198,21 +214,20 @@
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)
{
- // printf("==================LockFreeQueue push before\n");
- if (SemUtil::dec(slots) == -1) {
+// LoggerFactory::getLogger()->debug("==================LockFreeQueue push before\n");
+ if (sem_wait(&slots) == -1) {
err_msg(errno, "LockFreeQueue push");
- return false;
+ return errno;
}
-
+
if ( m_qImpl.push(a_data) ) {
-
- SemUtil::inc(items);
- // printf("==================LockFreeQueue push after\n");
- return true;
+ sem_post(&items);
+// LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n");
+ return 0;
}
- return false;
+ return -1;
}
@@ -220,23 +235,23 @@
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 (SemUtil::dec_nowait(slots) == -1) {
+ if (sem_trywait(&slots) == -1) {
if (errno == EAGAIN)
- return false;
+ return EAGAIN;
else {
err_msg(errno, "LockFreeQueue push_nowait");
- return false;
+ return errno;
}
}
if ( m_qImpl.push(a_data)) {
- SemUtil::inc(items);
- return true;
+ sem_post(&items);
+ return 0;
}
- return false;
+ return -1;
}
@@ -244,24 +259,34 @@
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 * timeout)
+int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push_timeout(const ELEM_T &a_data, const struct timespec * ts)
{
+
+ int rv;
+ 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 (SemUtil::dec_timeout(slots, timeout) == -1) {
- if (errno == EAGAIN)
- return false;
+ if(errno == ETIMEDOUT)
+ return EBUS_TIMEOUT;
+ else if(errno == EINTR)
+ continue;
else {
- // err_msg(errno, "LockFreeQueue push_timeout");
- return false;
+ LoggerFactory::getLogger()->error(errno, "LockFreeQueue push_timeout");
+ return errno;
}
}
if (m_qImpl.push(a_data)){
- SemUtil::inc(items);
- return true;
+ sem_post(&items);
+// LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout after\n");
+ return 0;
}
- return false;
+ return -1;
}
@@ -272,44 +297,43 @@
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)
{
- // printf("==================LockFreeQueue pop before\n");
- if (SemUtil::dec(items) == -1) {
- err_msg(errno, "LockFreeQueue pop");
- return false;
+
+ // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before\n");
+ if (sem_wait(&items) == -1) {
+ LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop");
+ return errno;
}
if (m_qImpl.pop(a_data)) {
- SemUtil::inc(slots);
- // printf("==================LockFreeQueue pop after\n");
- return true;
+ sem_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 (SemUtil::dec_nowait(items) == -1) {
+ if (sem_trywait(&items) == -1) {
if (errno == EAGAIN)
- return false;
+ return errno;
else {
- err_msg(errno, "LockFreeQueue pop_nowait");
- return false;
+ LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop_nowait");
+ return errno;
}
}
if (m_qImpl.pop(a_data)) {
- SemUtil::inc(slots);
- return true;
+ sem_post(&slots);
+ return 0;
}
- return false;
-
+ return -1;
}
@@ -317,24 +341,31 @@
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 * timeout)
+int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop_timeout(ELEM_T &a_data, struct timespec * ts)
{
-// printf("==================LockFreeQueue pop_timeout before\n");
- if (SemUtil::dec_timeout(items, timeout) == -1) {
- if (errno == EAGAIN)
- return false;
+// LoggerFactory::getLogger()->debug("=================ts sec = %d, nsec = %ld \n", ts->tv_sec, ts->tv_nsec );
+
+ LoggerFactory::getLogger()->debug("==================LockFreeQueue pop_timeout before\n");
+ struct timespec timeout = PXSemUtil::calc_sem_timeout(ts);
+// LoggerFactory::getLogger()->debug("================== timeout before sec = %d, nsec = %ld \n", timeout.tv_sec, timeout.tv_nsec );
+
+ while (sem_timedwait(&items, &timeout) == -1) {
+ if (errno == ETIMEDOUT)
+ return EBUS_TIMEOUT;
+ else if(errno == EINTR)
+ continue;
else {
- // err_msg(errno, "LockFreeQueue pop_timeout");
- return false;
+ // LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop_timeout %d", errno);
+ return errno;
}
}
if (m_qImpl.pop(a_data)) {
- SemUtil::inc(slots);
-// printf("==================LockFreeQueue pop_timeout after\n");
- return true;
+ sem_post(&slots);
+// LoggerFactory::getLogger()->debug("==================LockFreeQueue pop_timeout after\n");
+ return 0;
}
- return false;
+ return -1;
}
@@ -346,6 +377,7 @@
return m_qImpl.operator[](i);
}
+
template <
typename ELEM_T,
typename Allocator,
--
Gitblit v1.8.0