mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
Add Thread Guard for debugging simple threading issues (#4254)
* Add ThreadGuard class * Use ThreadGuard when accessing a ChannelView's messageSnapshot
This commit is contained in:
parent
2ba4da02ae
commit
b9308d7325
|
@ -364,6 +364,7 @@ set(SOURCE_FILES
|
|||
util/StreamLink.hpp
|
||||
util/StreamerMode.cpp
|
||||
util/StreamerMode.hpp
|
||||
util/ThreadGuard.hpp
|
||||
util/Twitch.cpp
|
||||
util/Twitch.hpp
|
||||
util/TypeName.hpp
|
||||
|
|
35
src/util/ThreadGuard.hpp
Normal file
35
src/util/ThreadGuard.hpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
// Debug-class which asserts if guard of the same object has been called from different threads
|
||||
struct ThreadGuard {
|
||||
#ifndef NDEBUG
|
||||
std::mutex mutex;
|
||||
std::optional<std::thread::id> threadID;
|
||||
#endif
|
||||
|
||||
inline void guard()
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::unique_lock lock(this->mutex);
|
||||
|
||||
auto currentThreadID = std::this_thread::get_id();
|
||||
|
||||
if (!this->threadID.has_value())
|
||||
{
|
||||
this->threadID = currentThreadID;
|
||||
return;
|
||||
}
|
||||
|
||||
assert(this->threadID == currentThreadID);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
|
@ -628,6 +628,7 @@ const boost::optional<MessageElementFlags> &ChannelView::getOverrideFlags()
|
|||
|
||||
LimitedQueueSnapshot<MessageLayoutPtr> &ChannelView::getMessagesSnapshot()
|
||||
{
|
||||
this->snapshotGuard_.guard();
|
||||
if (!this->paused() /*|| this->scrollBar_->isVisible()*/)
|
||||
{
|
||||
this->snapshot_ = this->messages_.getSnapshot();
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "messages/LimitedQueue.hpp"
|
||||
#include "messages/LimitedQueueSnapshot.hpp"
|
||||
#include "messages/Selection.hpp"
|
||||
#include "util/ThreadGuard.hpp"
|
||||
#include "widgets/BaseWidget.hpp"
|
||||
|
||||
#include <pajlada/signals/signal.hpp>
|
||||
|
@ -270,6 +271,7 @@ private:
|
|||
boost::optional<MessageElementFlags> overrideFlags_;
|
||||
MessageLayoutPtr lastReadMessage_;
|
||||
|
||||
ThreadGuard snapshotGuard_;
|
||||
LimitedQueueSnapshot<MessageLayoutPtr> snapshot_;
|
||||
|
||||
ChannelPtr channel_ = nullptr;
|
||||
|
|
Loading…
Reference in a new issue