Merge pull request #2128 from FearlessTobi/port-4197
Port citra-emu/citra#4197: "threadsafe_queue: Add PopWait and use it where possible "
This commit is contained in:
commit
cd7e1183e2
|
@ -40,9 +40,7 @@ public:
|
||||||
const Impl& operator=(Impl const&) = delete;
|
const Impl& operator=(Impl const&) = delete;
|
||||||
|
|
||||||
void PushEntry(Entry e) {
|
void PushEntry(Entry e) {
|
||||||
std::lock_guard<std::mutex> lock(message_mutex);
|
|
||||||
message_queue.Push(std::move(e));
|
message_queue.Push(std::move(e));
|
||||||
message_cv.notify_one();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddBackend(std::unique_ptr<Backend> backend) {
|
void AddBackend(std::unique_ptr<Backend> backend) {
|
||||||
|
@ -86,15 +84,13 @@ private:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
while (true) {
|
while (true) {
|
||||||
{
|
entry = message_queue.PopWait();
|
||||||
std::unique_lock<std::mutex> lock(message_mutex);
|
if (entry.final_entry) {
|
||||||
message_cv.wait(lock, [&] { return !running || message_queue.Pop(entry); });
|
|
||||||
}
|
|
||||||
if (!running) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
write_logs(entry);
|
write_logs(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case
|
// Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case
|
||||||
// where a system is repeatedly spamming logs even on close.
|
// where a system is repeatedly spamming logs even on close.
|
||||||
const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100;
|
const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100;
|
||||||
|
@ -106,14 +102,13 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
~Impl() {
|
~Impl() {
|
||||||
running = false;
|
Entry entry;
|
||||||
message_cv.notify_one();
|
entry.final_entry = true;
|
||||||
|
message_queue.Push(entry);
|
||||||
backend_thread.join();
|
backend_thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::atomic_bool running{true};
|
std::mutex writing_mutex;
|
||||||
std::mutex message_mutex, writing_mutex;
|
|
||||||
std::condition_variable message_cv;
|
|
||||||
std::thread backend_thread;
|
std::thread backend_thread;
|
||||||
std::vector<std::unique_ptr<Backend>> backends;
|
std::vector<std::unique_ptr<Backend>> backends;
|
||||||
Common::MPSCQueue<Log::Entry> message_queue;
|
Common::MPSCQueue<Log::Entry> message_queue;
|
||||||
|
|
|
@ -27,6 +27,7 @@ struct Entry {
|
||||||
unsigned int line_num;
|
unsigned int line_num;
|
||||||
std::string function;
|
std::string function;
|
||||||
std::string message;
|
std::string message;
|
||||||
|
bool final_entry = false;
|
||||||
|
|
||||||
Entry() = default;
|
Entry() = default;
|
||||||
Entry(Entry&& o) = default;
|
Entry(Entry&& o) = default;
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
// single reader, single writer queue
|
// single reader, single writer queue
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <condition_variable>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
@ -45,6 +46,7 @@ public:
|
||||||
ElementPtr* new_ptr = new ElementPtr();
|
ElementPtr* new_ptr = new ElementPtr();
|
||||||
write_ptr->next.store(new_ptr, std::memory_order_release);
|
write_ptr->next.store(new_ptr, std::memory_order_release);
|
||||||
write_ptr = new_ptr;
|
write_ptr = new_ptr;
|
||||||
|
cv.notify_one();
|
||||||
|
|
||||||
++size;
|
++size;
|
||||||
}
|
}
|
||||||
|
@ -74,6 +76,16 @@ public:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T PopWait() {
|
||||||
|
if (Empty()) {
|
||||||
|
std::unique_lock<std::mutex> lock(cv_mutex);
|
||||||
|
cv.wait(lock, [this]() { return !Empty(); });
|
||||||
|
}
|
||||||
|
T t;
|
||||||
|
Pop(t);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
// not thread-safe
|
// not thread-safe
|
||||||
void Clear() {
|
void Clear() {
|
||||||
size.store(0);
|
size.store(0);
|
||||||
|
@ -101,6 +113,8 @@ private:
|
||||||
ElementPtr* write_ptr;
|
ElementPtr* write_ptr;
|
||||||
ElementPtr* read_ptr;
|
ElementPtr* read_ptr;
|
||||||
std::atomic_size_t size{0};
|
std::atomic_size_t size{0};
|
||||||
|
std::mutex cv_mutex;
|
||||||
|
std::condition_variable cv;
|
||||||
};
|
};
|
||||||
|
|
||||||
// a simple thread-safe,
|
// a simple thread-safe,
|
||||||
|
@ -135,6 +149,10 @@ public:
|
||||||
return spsc_queue.Pop(t);
|
return spsc_queue.Pop(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T PopWait() {
|
||||||
|
return spsc_queue.PopWait();
|
||||||
|
}
|
||||||
|
|
||||||
// not thread-safe
|
// not thread-safe
|
||||||
void Clear() {
|
void Clear() {
|
||||||
spsc_queue.Clear();
|
spsc_queue.Clear();
|
||||||
|
|
Loading…
Reference in New Issue