| | |
| | | #include "sem_util.h" |
| | | #include "logger_factory.h" |
| | | #include "shm_allocator.h" |
| | | #include "px_sem_util.h" |
| | | |
| | | // default Queue size |
| | | #define LOCK_FREE_Q_DEFAULT_SIZE 16 |
| | |
| | | { |
| | | |
| | | 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. |
| | |
| | | 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 < |
| | |
| | | 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 < |
| | |
| | | template <typename T, typename AT> class Q_TYPE> |
| | | bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push(const ELEM_T &a_data) |
| | | { |
| | | LoggerFactory::getLogger()->debug("==================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; |
| | | } |
| | | |
| | | |
| | | if ( m_qImpl.push(a_data) ) { |
| | | |
| | | SemUtil::inc(items); |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n"); |
| | | sem_post(&items); |
| | | // LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n"); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | 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 (sem_trywait(&slots) == -1) { |
| | | if (errno == EAGAIN) |
| | | return false; |
| | | else { |
| | |
| | | } |
| | | |
| | | if ( m_qImpl.push(a_data)) { |
| | | SemUtil::inc(items); |
| | | sem_post(&items); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | 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) |
| | | bool 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; |
| | | |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout before\n"); |
| | | if (SemUtil::dec_timeout(slots, timeout) == -1) { |
| | | if (errno == EAGAIN) |
| | | 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 { |
| | | err_msg(errno, "LockFreeQueue push_timeout"); |
| | | LoggerFactory::getLogger()->error(errno, "LockFreeQueue push_timeout"); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | if (m_qImpl.push(a_data)){ |
| | | SemUtil::inc(items); |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout after\n"); |
| | | sem_post(&items); |
| | | // LoggerFactory::getLogger()->debug("==================LockFreeQueue push_timeout after\n"); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop(ELEM_T &a_data) |
| | | { |
| | | |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before\n"); |
| | | if (SemUtil::dec(items) == -1) { |
| | | err_msg(errno, "LockFreeQueue pop"); |
| | | // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before\n"); |
| | | if (sem_wait(&items) == -1) { |
| | | LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop"); |
| | | return false; |
| | | } |
| | | |
| | | if (m_qImpl.pop(a_data)) { |
| | | SemUtil::inc(slots); |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n"); |
| | | sem_post(&slots); |
| | | // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n"); |
| | | return true; |
| | | } |
| | | return false; |
| | | |
| | | } |
| | | |
| | | template < |
| | |
| | | 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 (sem_trywait(&items) == -1) { |
| | | if (errno == EAGAIN) |
| | | return false; |
| | | else { |
| | | err_msg(errno, "LockFreeQueue pop_nowait"); |
| | | LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop_nowait"); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | if (m_qImpl.pop(a_data)) { |
| | | SemUtil::inc(slots); |
| | | sem_post(&slots); |
| | | return true; |
| | | } |
| | | return false; |
| | | |
| | | } |
| | | |
| | | |
| | |
| | | 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) |
| | | bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop_timeout(ELEM_T &a_data, struct timespec * ts) |
| | | { |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop_timeout before\n"); |
| | | if (SemUtil::dec_timeout(items, timeout) == -1) { |
| | | // 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 { |
| | | // err_msg(errno, "LockFreeQueue pop_timeout"); |
| | | LoggerFactory::getLogger()->error(errno, "LockFreeQueue pop_timeout"); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | if (m_qImpl.pop(a_data)) { |
| | | SemUtil::inc(slots); |
| | | LoggerFactory::getLogger()->debug("==================LockFreeQueue pop_timeout after\n"); |
| | | sem_post(&slots); |
| | | // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop_timeout after\n"); |
| | | return true; |
| | | } |
| | | return false; |