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/logging/backend.cpp | |
| 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/logging/backend.cpp')
| -rw-r--r-- | src/common/logging/backend.cpp | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index a5e031189..276e49f86 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp | |||
| @@ -40,9 +40,7 @@ public: | |||
| 40 | const Impl& operator=(Impl const&) = delete; | 40 | const Impl& operator=(Impl const&) = delete; |
| 41 | 41 | ||
| 42 | void PushEntry(Entry e) { | 42 | void PushEntry(Entry e) { |
| 43 | std::lock_guard<std::mutex> lock(message_mutex); | ||
| 44 | message_queue.Push(std::move(e)); | 43 | message_queue.Push(std::move(e)); |
| 45 | message_cv.notify_one(); | ||
| 46 | } | 44 | } |
| 47 | 45 | ||
| 48 | void AddBackend(std::unique_ptr<Backend> backend) { | 46 | void AddBackend(std::unique_ptr<Backend> backend) { |
| @@ -85,16 +83,13 @@ private: | |||
| 85 | backend->Write(e); | 83 | backend->Write(e); |
| 86 | } | 84 | } |
| 87 | }; | 85 | }; |
| 88 | while (true) { | 86 | while (message_queue.PopWait(entry)) { |
| 89 | { | 87 | if (entry.final_entry) { |
| 90 | std::unique_lock<std::mutex> lock(message_mutex); | ||
| 91 | message_cv.wait(lock, [&] { return !running || message_queue.Pop(entry); }); | ||
| 92 | } | ||
| 93 | if (!running) { | ||
| 94 | break; | 88 | break; |
| 95 | } | 89 | } |
| 96 | write_logs(entry); | 90 | write_logs(entry); |
| 97 | } | 91 | } |
| 92 | |||
| 98 | // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case | 93 | // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case |
| 99 | // where a system is repeatedly spamming logs even on close. | 94 | // where a system is repeatedly spamming logs even on close. |
| 100 | const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100; | 95 | const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100; |
| @@ -106,14 +101,13 @@ private: | |||
| 106 | } | 101 | } |
| 107 | 102 | ||
| 108 | ~Impl() { | 103 | ~Impl() { |
| 109 | running = false; | 104 | Entry entry; |
| 110 | message_cv.notify_one(); | 105 | entry.final_entry = true; |
| 106 | message_queue.Push(entry); | ||
| 111 | backend_thread.join(); | 107 | backend_thread.join(); |
| 112 | } | 108 | } |
| 113 | 109 | ||
| 114 | std::atomic_bool running{true}; | 110 | std::mutex writing_mutex; |
| 115 | std::mutex message_mutex, writing_mutex; | ||
| 116 | std::condition_variable message_cv; | ||
| 117 | std::thread backend_thread; | 111 | std::thread backend_thread; |
| 118 | std::vector<std::unique_ptr<Backend>> backends; | 112 | std::vector<std::unique_ptr<Backend>> backends; |
| 119 | Common::MPSCQueue<Log::Entry> message_queue; | 113 | Common::MPSCQueue<Log::Entry> message_queue; |