| | |
| | | #ifndef SLinkedLockFreeQueue_H_ |
| | | #define SLinkedLockFreeQueue_H_ |
| | | #include "mm.h" |
| | | #include "pcsem.h" |
| | | #include "sem_util.h" |
| | | #include "SAbstractQueue.h" |
| | | |
| | | |
| | |
| | | Head.store(pointer, std::memory_order_relaxed); |
| | | Tail.store(pointer, std::memory_order_relaxed); |
| | | |
| | | slots = pcsem::init(IPC_PRIVATE, qsize); |
| | | items = pcsem::init(IPC_PRIVATE, 0); |
| | | slots = SemUtil::get(IPC_PRIVATE, qsize); |
| | | items = SemUtil::get(IPC_PRIVATE, 0); |
| | | |
| | | } |
| | | |
| | |
| | | SLinkedLockFreeQueue<T>::~SLinkedLockFreeQueue() |
| | | { |
| | | std::cerr << "SLinkedLockFreeQueue destory" << std::endl; |
| | | pcsem::remove(slots); |
| | | pcsem::remove(items); |
| | | SemUtil::remove(slots); |
| | | SemUtil::remove(items); |
| | | |
| | | |
| | | Node<T> * nodeptr; |
| | |
| | | template <typename T> |
| | | bool SLinkedLockFreeQueue<T>::add(const T & item) |
| | | { |
| | | if (pcsem::dec(slots) == -1) { |
| | | if (SemUtil::dec(slots) == -1) { |
| | | err_exit(errno, "add"); |
| | | } |
| | | |
| | | if (SLinkedLockFreeQueue<T>::_add(item)) { |
| | | pcsem::inc(items); |
| | | SemUtil::inc(items); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | template <typename T> |
| | | bool SLinkedLockFreeQueue<T>::add_nowait(const T & item) |
| | | { |
| | | if (pcsem::dec_nowait(slots) == -1) { |
| | | if (SemUtil::dec_nowait(slots) == -1) { |
| | | if (errno == EAGAIN) |
| | | return false; |
| | | else |
| | |
| | | } |
| | | |
| | | if (SLinkedLockFreeQueue<T>::_add(item)) { |
| | | pcsem::inc(items); |
| | | SemUtil::inc(items); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | template <typename T> |
| | | bool SLinkedLockFreeQueue<T>::add_timeout(const T & item, struct timespec * timeout) |
| | | { |
| | | if (pcsem::dec_timeout(slots, timeout) == -1) { |
| | | if (SemUtil::dec_timeout(slots, timeout) == -1) { |
| | | if (errno == EAGAIN) |
| | | return false; |
| | | else |
| | |
| | | } |
| | | |
| | | if (SLinkedLockFreeQueue<T>::_add(item)){ |
| | | pcsem::inc(items); |
| | | SemUtil::inc(items); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | template <typename T> |
| | | bool SLinkedLockFreeQueue<T>::remove(T & item) |
| | | { |
| | | if (pcsem::dec(items) == -1) { |
| | | if (SemUtil::dec(items) == -1) { |
| | | err_exit(errno, "remove"); |
| | | } |
| | | |
| | | if (SLinkedLockFreeQueue<T>::_remove(item)) { |
| | | pcsem::inc(slots); |
| | | SemUtil::inc(slots); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | template <typename T> |
| | | bool SLinkedLockFreeQueue<T>::remove_nowait(T & item) |
| | | { |
| | | if (pcsem::dec_nowait(items) == -1) { |
| | | if (SemUtil::dec_nowait(items) == -1) { |
| | | if (errno == EAGAIN) |
| | | return false; |
| | | else |
| | |
| | | } |
| | | |
| | | if (SLinkedLockFreeQueue<T>::_remove(item)) { |
| | | pcsem::inc(slots); |
| | | SemUtil::inc(slots); |
| | | return true; |
| | | } |
| | | return false; |
| | |
| | | template <typename T> |
| | | bool SLinkedLockFreeQueue<T>::remove_timeout(T & item, struct timespec * timeout) |
| | | { |
| | | if (pcsem::dec_timeout(items, timeout) == -1) { |
| | | if (SemUtil::dec_timeout(items, timeout) == -1) { |
| | | if (errno == EAGAIN) |
| | | return false; |
| | | else |
| | |
| | | } |
| | | |
| | | if (SLinkedLockFreeQueue<T>::_remove(item)) { |
| | | pcsem::inc(slots); |
| | | SemUtil::inc(slots); |
| | | return true; |
| | | } |
| | | return false; |