|
| 1 | +#ifndef _ARRAYLOCKFREEQUEUEIMP_H___ |
| 2 | +#define _ARRAYLOCKFREEQUEUEIMP_H___ |
| 3 | +#include "ArrayLockFreeQueue.h" |
| 4 | + |
| 5 | +#include <assert.h> |
| 6 | +#include "atom_opt.h" |
| 7 | + |
| 8 | +template <typename ELEM_T, uint32_t Q_SIZE> |
| 9 | +ArrayLockFreeQueue<ELEM_T, Q_SIZE>::ArrayLockFreeQueue() : |
| 10 | + m_writeIndex(0), |
| 11 | + m_readIndex(0), |
| 12 | + m_maximumReadIndex(0) |
| 13 | +{ |
| 14 | + |
| 15 | + m_count = 0; |
| 16 | + |
| 17 | +} |
| 18 | + |
| 19 | +template <typename ELEM_T, uint32_t Q_SIZE> |
| 20 | +ArrayLockFreeQueue<ELEM_T, Q_SIZE>::~ArrayLockFreeQueue() |
| 21 | +{ |
| 22 | + |
| 23 | +} |
| 24 | + |
| 25 | +template <typename ELEM_T, uint32_t Q_SIZE> |
| 26 | +inline uint32_t ArrayLockFreeQueue<ELEM_T, Q_SIZE>::countToIndex(uint32_t a_count) |
| 27 | +{ |
| 28 | + return (a_count % Q_SIZE); |
| 29 | +} |
| 30 | + |
| 31 | +template <typename ELEM_T, uint32_t Q_SIZE> |
| 32 | +uint32_t ArrayLockFreeQueue<ELEM_T, Q_SIZE>::size() |
| 33 | +{ |
| 34 | + uint32_t currentWriteIndex = m_writeIndex; |
| 35 | + uint32_t currentReadIndex = m_readIndex; |
| 36 | + |
| 37 | + if(currentWriteIndex>=currentReadIndex) |
| 38 | + return currentWriteIndex - currentReadIndex; |
| 39 | + else |
| 40 | + return Q_SIZE + currentWriteIndex - currentReadIndex; |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +template <typename ELEM_T, uint32_t Q_SIZE> |
| 45 | +bool ArrayLockFreeQueue<ELEM_T, Q_SIZE>::enqueue(const ELEM_T &a_data) |
| 46 | +{ |
| 47 | + uint32_t currentWriteIndex; |
| 48 | + uint32_t currentReadIndex; |
| 49 | + do |
| 50 | + { |
| 51 | + currentWriteIndex = m_writeIndex; |
| 52 | + currentReadIndex = m_readIndex; |
| 53 | + if(countToIndex(currentWriteIndex + 1) == |
| 54 | + countToIndex(currentReadIndex)) |
| 55 | + { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } while(!CAS(&m_writeIndex, currentWriteIndex, (currentWriteIndex+1))); |
| 59 | + |
| 60 | + m_thequeue[countToIndex(currentWriteIndex)] = a_data; |
| 61 | + |
| 62 | + while(!CAS(&m_maximumReadIndex, currentWriteIndex, (currentWriteIndex + 1))) |
| 63 | + { |
| 64 | + sched_yield(); |
| 65 | + } |
| 66 | + |
| 67 | + AtomicAdd(&m_count, 1); |
| 68 | + |
| 69 | + return true; |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +template <typename ELEM_T, uint32_t Q_SIZE> |
| 74 | +bool ArrayLockFreeQueue<ELEM_T, Q_SIZE>::try_dequeue(ELEM_T &a_data) |
| 75 | +{ |
| 76 | + return dequeue(a_data); |
| 77 | +} |
| 78 | + |
| 79 | +template <typename ELEM_T, uint32_t Q_SIZE> |
| 80 | +bool ArrayLockFreeQueue<ELEM_T, Q_SIZE>::dequeue(ELEM_T &a_data) |
| 81 | +{ |
| 82 | + uint32_t currentMaximumReadIndex; |
| 83 | + uint32_t currentReadIndex; |
| 84 | + |
| 85 | + do |
| 86 | + { |
| 87 | + currentReadIndex = m_readIndex; |
| 88 | + currentMaximumReadIndex = m_maximumReadIndex; |
| 89 | + |
| 90 | + if(countToIndex(currentReadIndex) == |
| 91 | + countToIndex(currentMaximumReadIndex)) |
| 92 | + { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + a_data = m_thequeue[countToIndex(currentReadIndex)]; |
| 97 | + |
| 98 | + if(CAS(&m_readIndex, currentReadIndex, (currentReadIndex + 1))) |
| 99 | + { |
| 100 | + AtomicSub(&m_count, 1); |
| 101 | + return true; |
| 102 | + } |
| 103 | + } while(true); |
| 104 | + |
| 105 | + assert(0); |
| 106 | + |
| 107 | + return false; |
| 108 | + |
| 109 | +} |
| 110 | + |
| 111 | +#endif |
0 commit comments