mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-10-01 22:08:37 -05:00
Refactor locking of macro segments
This should avoid crashes when actions or conditions are performed in parallel to the main macro loop and will improve the UI responsiveness in some scenarios
This commit is contained in:
@@ -20,4 +20,36 @@ std::unique_lock<std::mutex> *GetLoopLock()
|
||||
return GetSwitcherLoopLock();
|
||||
}
|
||||
|
||||
PerInstanceMutex::PerInstanceMutex() {}
|
||||
|
||||
PerInstanceMutex::~PerInstanceMutex(){};
|
||||
|
||||
PerInstanceMutex::PerInstanceMutex(const PerInstanceMutex &) {}
|
||||
|
||||
PerInstanceMutex &PerInstanceMutex::operator=(const PerInstanceMutex &)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
PerInstanceMutex::operator std::mutex &()
|
||||
{
|
||||
return _mtx;
|
||||
}
|
||||
|
||||
PerInstanceMutex::operator const std::mutex &() const
|
||||
{
|
||||
return _mtx;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> Lockable::Lock()
|
||||
{
|
||||
return std::lock_guard<std::mutex>(_mtx);
|
||||
}
|
||||
|
||||
void Lockable::WithLock(const std::function<void()> &func)
|
||||
{
|
||||
const auto lock = Lock();
|
||||
func();
|
||||
}
|
||||
|
||||
} // namespace advss
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
#include "export-symbol-helper.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
namespace advss {
|
||||
@@ -10,10 +12,46 @@ namespace advss {
|
||||
if (_loading || !_entryData) { \
|
||||
return; \
|
||||
} \
|
||||
auto lock = LockContext();
|
||||
auto lock = _entryData->Lock();
|
||||
|
||||
[[nodiscard]] EXPORT std::mutex *GetMutex();
|
||||
[[nodiscard]] EXPORT std::lock_guard<std::mutex> LockContext();
|
||||
[[nodiscard]] EXPORT std::unique_lock<std::mutex> *GetLoopLock();
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4251)
|
||||
#endif
|
||||
|
||||
// std::mutex wrapper with no-op copy constructor and assignment operator
|
||||
class EXPORT PerInstanceMutex {
|
||||
public:
|
||||
PerInstanceMutex();
|
||||
~PerInstanceMutex();
|
||||
PerInstanceMutex(const PerInstanceMutex &);
|
||||
PerInstanceMutex &operator=(const PerInstanceMutex &);
|
||||
|
||||
operator std::mutex &();
|
||||
operator const std::mutex &() const;
|
||||
|
||||
private:
|
||||
std::mutex _mtx;
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
class EXPORT Lockable {
|
||||
public:
|
||||
Lockable() = default;
|
||||
virtual ~Lockable() = default;
|
||||
|
||||
[[nodiscard]] std::lock_guard<std::mutex> Lock();
|
||||
void WithLock(const std::function<void()> &func);
|
||||
|
||||
private:
|
||||
PerInstanceMutex _mtx;
|
||||
};
|
||||
|
||||
} // namespace advss
|
||||
|
||||
Reference in New Issue
Block a user