-
-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use condition variable to shutdown
- Loading branch information
Showing
8 changed files
with
233 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include "util/OnceFlag.hpp" | ||
|
||
#include "Test.hpp" | ||
|
||
#include <thread> | ||
|
||
using namespace chatterino; | ||
|
||
TEST(OnceFlag, basic) | ||
{ | ||
OnceFlag startedFlag; | ||
OnceFlag startedAckFlag; | ||
OnceFlag stoppedFlag; | ||
|
||
std::thread t([&] { | ||
startedFlag.set(); | ||
startedAckFlag.wait(); | ||
std::this_thread::sleep_for(std::chrono::milliseconds{50}); | ||
stoppedFlag.set(); | ||
}); | ||
|
||
startedFlag.wait(); | ||
startedAckFlag.set(); | ||
stoppedFlag.wait(); | ||
|
||
t.join(); | ||
} | ||
|
||
TEST(OnceFlag, waitFor) | ||
{ | ||
OnceFlag startedFlag; | ||
OnceFlag startedAckFlag; | ||
OnceFlag stoppedFlag; | ||
|
||
std::thread t([&] { | ||
startedFlag.set(); | ||
startedAckFlag.wait(); | ||
|
||
std::this_thread::sleep_for(std::chrono::milliseconds{100}); | ||
stoppedFlag.set(); | ||
}); | ||
|
||
startedFlag.wait(); | ||
startedAckFlag.set(); | ||
|
||
auto start = std::chrono::system_clock::now(); | ||
ASSERT_TRUE(stoppedFlag.waitFor(std::chrono::milliseconds{200})); | ||
auto stop = std::chrono::system_clock::now(); | ||
|
||
ASSERT_LT(stop - start, std::chrono::milliseconds{150}); | ||
|
||
start = std::chrono::system_clock::now(); | ||
ASSERT_TRUE(stoppedFlag.waitFor(std::chrono::milliseconds{1000})); | ||
stop = std::chrono::system_clock::now(); | ||
|
||
ASSERT_LT(stop - start, std::chrono::milliseconds{10}); | ||
|
||
start = std::chrono::system_clock::now(); | ||
stoppedFlag.wait(); | ||
stop = std::chrono::system_clock::now(); | ||
|
||
ASSERT_LT(stop - start, std::chrono::milliseconds{10}); | ||
|
||
t.join(); | ||
} | ||
|
||
TEST(OnceFlag, waitForTimeout) | ||
{ | ||
OnceFlag startedFlag; | ||
OnceFlag startedAckFlag; | ||
OnceFlag stoppedFlag; | ||
|
||
std::thread t([&] { | ||
startedFlag.set(); | ||
startedAckFlag.wait(); | ||
std::this_thread::sleep_for(std::chrono::milliseconds{100}); | ||
stoppedFlag.set(); | ||
}); | ||
|
||
startedFlag.wait(); | ||
startedAckFlag.set(); | ||
|
||
ASSERT_FALSE(stoppedFlag.waitFor(std::chrono::milliseconds{25})); | ||
stoppedFlag.wait(); | ||
|
||
t.join(); | ||
} |