diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/logging/backend.cpp | 19 | ||||
| -rw-r--r-- | src/common/logging/backend.h | 1 | ||||
| -rw-r--r-- | src/common/threadsafe_queue.h | 18 |
3 files changed, 26 insertions, 12 deletions
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index a5e031189..b369f199f 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) { |
| @@ -86,15 +84,13 @@ private: | |||
| 86 | } | 84 | } |
| 87 | }; | 85 | }; |
| 88 | while (true) { | 86 | while (true) { |
| 89 | { | 87 | entry = message_queue.PopWait(); |
| 90 | std::unique_lock<std::mutex> lock(message_mutex); | 88 | if (entry.final_entry) { |
| 91 | message_cv.wait(lock, [&] { return !running || message_queue.Pop(entry); }); | ||
| 92 | } | ||
| 93 | if (!running) { | ||
| 94 | break; | 89 | break; |
| 95 | } | 90 | } |
| 96 | write_logs(entry); | 91 | write_logs(entry); |
| 97 | } | 92 | } |
| 93 | |||
| 98 | // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case | 94 | // 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. | 95 | // where a system is repeatedly spamming logs even on close. |
| 100 | const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100; | 96 | const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100; |
| @@ -106,14 +102,13 @@ private: | |||
| 106 | } | 102 | } |
| 107 | 103 | ||
| 108 | ~Impl() { | 104 | ~Impl() { |
| 109 | running = false; | 105 | Entry entry; |
| 110 | message_cv.notify_one(); | 106 | entry.final_entry = true; |
| 107 | message_queue.Push(entry); | ||
| 111 | backend_thread.join(); | 108 | backend_thread.join(); |
| 112 | } | 109 | } |
| 113 | 110 | ||
| 114 | std::atomic_bool running{true}; | 111 | std::mutex writing_mutex; |
| 115 | std::mutex message_mutex, writing_mutex; | ||
| 116 | std::condition_variable message_cv; | ||
| 117 | std::thread backend_thread; | 112 | std::thread backend_thread; |
| 118 | std::vector<std::unique_ptr<Backend>> backends; | 113 | std::vector<std::unique_ptr<Backend>> backends; |
| 119 | Common::MPSCQueue<Log::Entry> message_queue; | 114 | Common::MPSCQueue<Log::Entry> message_queue; |
diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index 91bb0c309..a31ee6968 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h | |||
| @@ -27,6 +27,7 @@ struct Entry { | |||
| 27 | unsigned int line_num; | 27 | unsigned int line_num; |
| 28 | std::string function; | 28 | std::string function; |
| 29 | std::string message; | 29 | std::string message; |
| 30 | bool final_entry = false; | ||
| 30 | 31 | ||
| 31 | Entry() = default; | 32 | Entry() = default; |
| 32 | Entry(Entry&& o) = default; | 33 | Entry(Entry&& o) = default; |
diff --git a/src/common/threadsafe_queue.h b/src/common/threadsafe_queue.h index f553efdc9..821e8536a 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> |
| @@ -45,6 +46,7 @@ public: | |||
| 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 | } |
| @@ -74,6 +76,16 @@ public: | |||
| 74 | return true; | 76 | return true; |
| 75 | } | 77 | } |
| 76 | 78 | ||
| 79 | T PopWait() { | ||
| 80 | if (Empty()) { | ||
| 81 | std::unique_lock<std::mutex> lock(cv_mutex); | ||
| 82 | cv.wait(lock, [this]() { return !Empty(); }); | ||
| 83 | } | ||
| 84 | T t; | ||
| 85 | Pop(t); | ||
| 86 | return t; | ||
| 87 | } | ||
| 88 | |||
| 77 | // not thread-safe | 89 | // not thread-safe |
| 78 | void Clear() { | 90 | void Clear() { |
| 79 | size.store(0); | 91 | size.store(0); |
| @@ -101,6 +113,8 @@ private: | |||
| 101 | ElementPtr* write_ptr; | 113 | ElementPtr* write_ptr; |
| 102 | ElementPtr* read_ptr; | 114 | ElementPtr* read_ptr; |
| 103 | std::atomic_size_t size{0}; | 115 | std::atomic_size_t size{0}; |
| 116 | std::mutex cv_mutex; | ||
| 117 | std::condition_variable cv; | ||
| 104 | }; | 118 | }; |
| 105 | 119 | ||
| 106 | // a simple thread-safe, | 120 | // a simple thread-safe, |
| @@ -135,6 +149,10 @@ public: | |||
| 135 | return spsc_queue.Pop(t); | 149 | return spsc_queue.Pop(t); |
| 136 | } | 150 | } |
| 137 | 151 | ||
| 152 | T PopWait() { | ||
| 153 | return spsc_queue.PopWait(); | ||
| 154 | } | ||
| 155 | |||
| 138 | // not thread-safe | 156 | // not thread-safe |
| 139 | void Clear() { | 157 | void Clear() { |
| 140 | spsc_queue.Clear(); | 158 | spsc_queue.Clear(); |