| | |
| | | int items; |
| | | |
| | | public: |
| | | // int mutex; |
| | | int mutex; |
| | | LockFreeQueue(size_t qsize = LOCK_FREE_Q_DEFAULT_SIZE); |
| | | |
| | | /// @brief destructor of the class. |
| | |
| | | /// @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, struct timespec * timeout); |
| | | bool 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 |
| | |
| | | // 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); |
| | | mutex = SemUtil::get(IPC_PRIVATE, 1); |
| | | } |
| | | |
| | | template < |
| | |
| | | LoggerFactory::getLogger().debug("LockFreeQueue desctroy"); |
| | | SemUtil::remove(slots); |
| | | SemUtil::remove(items); |
| | | SemUtil::remove(mutex); |
| | | } |
| | | |
| | | template < |
| | |
| | | template <typename T, typename AT> class Q_TYPE> |
| | | bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push(const ELEM_T &a_data) |
| | | { |
| | | // printf("==================LockFreeQueue push before\n"); |
| | | if (SemUtil::dec(slots) == -1) { |
| | | err_msg(errno, "LockFreeQueue push"); |
| | | return false; |
| | | } |
| | | |
| | | if ( m_qImpl.push(a_data) ) { |
| | | SemUtil::inc(items); |
| | | |
| | | SemUtil::inc(items); |
| | | // printf("==================LockFreeQueue push after\n"); |
| | | 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, struct timespec * timeout) |
| | | bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push_timeout(const ELEM_T &a_data, const struct timespec * timeout) |
| | | { |
| | | |
| | | |
| | | if (SemUtil::dec_timeout(slots, timeout) == -1) { |
| | | if (errno == EAGAIN) |
| | |
| | | template <typename T, typename AT> class Q_TYPE> |
| | | bool 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; |
| | | } |
| | | |
| | | if (m_qImpl.pop(a_data)) { |
| | | SemUtil::inc(slots); |
| | | SemUtil::inc(slots); |
| | | // printf("==================LockFreeQueue pop after\n"); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | template <typename T, typename AT> class Q_TYPE> |
| | | bool LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop_timeout(ELEM_T &a_data, struct timespec * timeout) |
| | | { |
| | | // printf("==================LockFreeQueue pop_timeout before\n"); |
| | | if (SemUtil::dec_timeout(items, timeout) == -1) { |
| | | if (errno == EAGAIN) |
| | | return false; |
| | |
| | | } |
| | | |
| | | if (m_qImpl.pop(a_data)) { |
| | | SemUtil::inc(slots); |
| | | SemUtil::inc(slots); |
| | | // printf("==================LockFreeQueue pop_timeout after\n"); |
| | | return true; |
| | | } |
| | | return false; |