chore: use condition variable to shutdown websocket pools (#5721)
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#include "util/OnceFlag.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
OnceFlag::OnceFlag() = default;
|
||||
OnceFlag::~OnceFlag() = default;
|
||||
|
||||
void OnceFlag::set()
|
||||
{
|
||||
{
|
||||
std::unique_lock guard(this->mutex);
|
||||
this->flag = true;
|
||||
}
|
||||
this->condvar.notify_all();
|
||||
}
|
||||
|
||||
bool OnceFlag::waitFor(std::chrono::milliseconds ms)
|
||||
{
|
||||
std::unique_lock lock(this->mutex);
|
||||
return this->condvar.wait_for(lock, ms, [this] {
|
||||
return this->flag;
|
||||
});
|
||||
}
|
||||
|
||||
void OnceFlag::wait()
|
||||
{
|
||||
std::unique_lock lock(this->mutex);
|
||||
this->condvar.wait(lock, [this] {
|
||||
return this->flag;
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
/// @brief A flag that can only be set once which notifies waiters.
|
||||
///
|
||||
/// This can be used to synchronize with other threads. Note that waiting
|
||||
/// threads will be suspended.
|
||||
class OnceFlag
|
||||
{
|
||||
public:
|
||||
OnceFlag();
|
||||
~OnceFlag();
|
||||
|
||||
/// Set this flag and notify waiters
|
||||
void set();
|
||||
|
||||
/// @brief Wait for at most `ms` until this flag is set.
|
||||
///
|
||||
/// The calling thread will be suspended during the wait.
|
||||
///
|
||||
/// @param ms The maximum time to wait for this flag
|
||||
/// @returns `true` if this flag was set during the wait or before
|
||||
bool waitFor(std::chrono::milliseconds ms);
|
||||
|
||||
/// @brief Wait until this flag is set by another thread
|
||||
///
|
||||
/// The calling thread will be suspended during the wait.
|
||||
void wait();
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
std::condition_variable condvar;
|
||||
bool flag = false;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
Reference in New Issue
Block a user