diff options
| author | 2018-09-09 13:08:57 +0200 | |
|---|---|---|
| committer | 2019-02-15 22:12:54 +0100 | |
| commit | 41549365680f0aae3f82e5812f3470305e939f7d (patch) | |
| tree | 027ec302d0119e4ded6cc4627c6fa3a94c8699ec /src/common/threadsafe_queue.h | |
| parent | Merge pull request #2112 from lioncash/shadowing (diff) | |
| download | yuzu-41549365680f0aae3f82e5812f3470305e939f7d.tar.gz yuzu-41549365680f0aae3f82e5812f3470305e939f7d.tar.xz yuzu-41549365680f0aae3f82e5812f3470305e939f7d.zip | |
threadsafe_queue: Add WaitIfEmpty and use it in logging
Diffstat (limited to 'src/common/threadsafe_queue.h')
| -rw-r--r-- | src/common/threadsafe_queue.h | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index f553efdc9..4fdcecca0 100644 --- a/src/common/threadsafe_queue.h +++ b/src/common/threadsafe_queue.h | |||
| @@ -8,6 +8,7 @@ | |||
| 8 | // single reader, single writer queue | 8 | // single reader, single writer queue |
| 9 | 9 | ||
| 10 | #include <atomic> | 10 | #include <atomic> |
| 11 | #include <condition_variable> | ||
| 11 | #include <cstddef> | 12 | #include <cstddef> |
| 12 | #include <mutex> | 13 | #include <mutex> |
| 13 | #include <utility> | 14 | #include <utility> |
| @@ -39,12 +40,13 @@ public: | |||
| 39 | template <typename Arg> | 40 | template <typename Arg> |
| 40 | void Push(Arg&& t) { | 41 | void Push(Arg&& t) { |
| 41 | // create the element, add it to the queue | 42 | // create the element, add it to the queue |
| 42 | write_ptr->current = std::forward<Arg>(t); | 43 | write_ptr->current = std::move(t); |
| 43 | // set the next pointer to a new element ptr | 44 | // set the next pointer to a new element ptr |
| 44 | // then advance the write pointer | 45 | // then advance the write pointer |
| 45 | ElementPtr* new_ptr = new ElementPtr(); | 46 | ElementPtr* new_ptr = new ElementPtr(); |
| 46 | write_ptr->next.store(new_ptr, std::memory_order_release); | 47 | write_ptr->next.store(new_ptr, std::memory_order_release); |
| 47 | write_ptr = new_ptr; | 48 | write_ptr = new_ptr; |
| 49 | cv.notify_one(); | ||
| 48 | 50 | ||
| 49 | ++size; | 51 | ++size; |
| 50 | } | 52 | } |
| @@ -67,6 +69,7 @@ public: | |||
| 67 | --size; | 69 | --size; |
| 68 | 70 | ||
| 69 | ElementPtr* tmpptr = read_ptr; | 71 | ElementPtr* tmpptr = read_ptr; |
| 72 | |||
| 70 | read_ptr = tmpptr->next.load(std::memory_order_acquire); | 73 | read_ptr = tmpptr->next.load(std::memory_order_acquire); |
| 71 | t = std::move(tmpptr->current); | 74 | t = std::move(tmpptr->current); |
| 72 | tmpptr->next.store(nullptr); | 75 | tmpptr->next.store(nullptr); |
| @@ -74,6 +77,14 @@ public: | |||
| 74 | return true; | 77 | return true; |
| 75 | } | 78 | } |
| 76 | 79 | ||
| 80 | bool PopWait(T& t) { | ||
| 81 | if (Empty()) { | ||
| 82 | std::unique_lock<std::mutex> lock(cv_mutex); | ||
| 83 | cv.wait(lock, [this]() { return !Empty(); }); | ||
| 84 | } | ||
| 85 | return Pop(t); | ||
| 86 | } | ||
| 87 | |||
| 77 | // not thread-safe | 88 | // not thread-safe |
| 78 | void Clear() { | 89 | void Clear() { |
| 79 | size.store(0); | 90 | size.store(0); |
| @@ -101,6 +112,8 @@ private: | |||
| 101 | ElementPtr* write_ptr; | 112 | ElementPtr* write_ptr; |
| 102 | ElementPtr* read_ptr; | 113 | ElementPtr* read_ptr; |
| 103 | std::atomic_size_t size{0}; | 114 | std::atomic_size_t size{0}; |
| 115 | std::mutex cv_mutex; | ||
| 116 | std::condition_variable cv; | ||
| 104 | }; | 117 | }; |
| 105 | 118 | ||
| 106 | // a simple thread-safe, | 119 | // a simple thread-safe, |
| @@ -135,6 +148,10 @@ public: | |||
| 135 | return spsc_queue.Pop(t); | 148 | return spsc_queue.Pop(t); |
| 136 | } | 149 | } |
| 137 | 150 | ||
| 151 | bool PopWait(T& t) { | ||
| 152 | return spsc_queue.PopWait(t); | ||
| 153 | } | ||
| 154 | |||
| 138 | // not thread-safe | 155 | // not thread-safe |
| 139 | void Clear() { | 156 | void Clear() { |
| 140 | spsc_queue.Clear(); | 157 | spsc_queue.Clear(); |