| | |
| | | #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 |
| | |
| | | // 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 |
| | |
| | | /// by default) |
| | | template < |
| | | typename ELEM_T, |
| | | template <typename T> class Q_TYPE = ArrayLockFreeQueue > |
| | | typename Allocator = SHM_Allocator, |
| | | template <typename T, typename AT> class Q_TYPE = ArrayLockFreeQueue |
| | | > |
| | | class LockFreeQueue |
| | | { |
| | | |
| | |
| | | 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); |
| | |
| | | |
| | | 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); |
| | |
| | | |
| | | 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(); |
| | | } |
| | |
| | | |
| | | 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"); |
| | |
| | | |
| | | 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) |
| | |
| | | |
| | | 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) { |
| | |
| | | |
| | | 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_msg(errno, "LockFreeQueue pop"); |
| | |
| | | |
| | | 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) |
| | |
| | | |
| | | 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) |
| | |
| | | |
| | | 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 mem_pool_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::malloc(size); |
| | | } |
| | | |
| | | template < |
| | | typename ELEM_T, |
| | | template <typename T> class Q_TYPE> |
| | | void LockFreeQueue<ELEM_T, Q_TYPE>::operator delete(void *p) { |
| | | return mem_pool_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::free(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__ |