diff options
Diffstat (limited to '')
| -rw-r--r-- | src/common/fifo_queue.h | 111 |
1 files changed, 0 insertions, 111 deletions
diff --git a/src/common/fifo_queue.h b/src/common/fifo_queue.h deleted file mode 100644 index b426e6596..000000000 --- a/src/common/fifo_queue.h +++ /dev/null | |||
| @@ -1,111 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // a simple lockless thread-safe, | ||
| 4 | // single reader, single writer queue | ||
| 5 | |||
| 6 | #include "common/atomic.h" | ||
| 7 | |||
| 8 | namespace Common | ||
| 9 | { | ||
| 10 | |||
| 11 | template <typename T> | ||
| 12 | class FifoQueue | ||
| 13 | { | ||
| 14 | public: | ||
| 15 | FifoQueue() : m_size(0) | ||
| 16 | { | ||
| 17 | m_write_ptr = m_read_ptr = new ElementPtr(); | ||
| 18 | } | ||
| 19 | |||
| 20 | ~FifoQueue() | ||
| 21 | { | ||
| 22 | // this will empty out the whole queue | ||
| 23 | delete m_read_ptr; | ||
| 24 | } | ||
| 25 | |||
| 26 | u32 Size() const | ||
| 27 | { | ||
| 28 | return m_size; | ||
| 29 | } | ||
| 30 | |||
| 31 | bool Empty() const | ||
| 32 | { | ||
| 33 | //return (m_read_ptr == m_write_ptr); | ||
| 34 | return (0 == m_size); | ||
| 35 | } | ||
| 36 | |||
| 37 | T& Front() const | ||
| 38 | { | ||
| 39 | return *m_read_ptr->current; | ||
| 40 | } | ||
| 41 | |||
| 42 | template <typename Arg> | ||
| 43 | void Push(Arg&& t) | ||
| 44 | { | ||
| 45 | // create the element, add it to the queue | ||
| 46 | m_write_ptr->current = new T(std::forward<Arg>(t)); | ||
| 47 | // set the next pointer to a new element ptr | ||
| 48 | // then advance the write pointer | ||
| 49 | m_write_ptr = m_write_ptr->next = new ElementPtr(); | ||
| 50 | Common::AtomicIncrement(m_size); | ||
| 51 | } | ||
| 52 | |||
| 53 | void Pop() | ||
| 54 | { | ||
| 55 | Common::AtomicDecrement(m_size); | ||
| 56 | ElementPtr *const tmpptr = m_read_ptr; | ||
| 57 | // advance the read pointer | ||
| 58 | m_read_ptr = m_read_ptr->next; | ||
| 59 | // set the next element to NULL to stop the recursive deletion | ||
| 60 | tmpptr->next = nullptr; | ||
| 61 | delete tmpptr; // this also deletes the element | ||
| 62 | } | ||
| 63 | |||
| 64 | bool Pop(T& t) | ||
| 65 | { | ||
| 66 | if (Empty()) | ||
| 67 | return false; | ||
| 68 | |||
| 69 | t = std::move(Front()); | ||
| 70 | Pop(); | ||
| 71 | |||
| 72 | return true; | ||
| 73 | } | ||
| 74 | |||
| 75 | // not thread-safe | ||
| 76 | void Clear() | ||
| 77 | { | ||
| 78 | m_size = 0; | ||
| 79 | delete m_read_ptr; | ||
| 80 | m_write_ptr = m_read_ptr = new ElementPtr(); | ||
| 81 | } | ||
| 82 | |||
| 83 | private: | ||
| 84 | // stores a pointer to element | ||
| 85 | // and a pointer to the next ElementPtr | ||
| 86 | class ElementPtr | ||
| 87 | { | ||
| 88 | public: | ||
| 89 | ElementPtr() : current(nullptr), next(nullptr) {} | ||
| 90 | |||
| 91 | ~ElementPtr() | ||
| 92 | { | ||
| 93 | if (current) | ||
| 94 | { | ||
| 95 | delete current; | ||
| 96 | // recusion ftw | ||
| 97 | if (next) | ||
| 98 | delete next; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | T *volatile current; | ||
| 103 | ElementPtr *volatile next; | ||
| 104 | }; | ||
| 105 | |||
| 106 | ElementPtr *volatile m_write_ptr; | ||
| 107 | ElementPtr *volatile m_read_ptr; | ||
| 108 | volatile u32 m_size; | ||
| 109 | }; | ||
| 110 | |||
| 111 | } | ||