-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Lockable/Locked to wrap data we want to move around threads
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#pragma once | ||
|
||
#include "Stl.hpp" | ||
|
||
namespace TiltedPhoques | ||
{ | ||
template<class T, class U> | ||
struct Locked; | ||
|
||
template<class T, class TLock = std::mutex> | ||
struct Lockable | ||
{ | ||
struct Data | ||
{ | ||
template<class... Args> | ||
Data(Args&& ... aArgs) | ||
: Value(std::forward<Args>(aArgs)...) | ||
{} | ||
|
||
T Value; | ||
TLock Lock; | ||
}; | ||
|
||
struct Ref | ||
{ | ||
Ref(typename SharedPtr<Data>::weak_type apData) | ||
: m_pData(std::move(apData)) | ||
{} | ||
|
||
[[nodiscard]] Locked<T, TLock> Lock() const | ||
{ | ||
if (auto locked = m_pData.lock()) | ||
{ | ||
return { locked }; | ||
} | ||
|
||
return { {} }; | ||
} | ||
|
||
private: | ||
typename SharedPtr<Data>::weak_type m_pData; | ||
}; | ||
|
||
|
||
template<class... Args> | ||
Lockable(Args&& ... aArgs) | ||
: m_pData(MakeShared<Data>(std::forward<Args>(aArgs)...)) | ||
{} | ||
|
||
Ref AsRef() const noexcept | ||
{ | ||
return Ref(m_pData); | ||
} | ||
|
||
[[nodiscard]] Locked<T, TLock> Lock() const | ||
{ | ||
return AsRef().Lock(); | ||
} | ||
|
||
private: | ||
|
||
friend struct Locked<T, TLock>; | ||
|
||
SharedPtr<Data> m_pData; | ||
}; | ||
} | ||
|
||
#include "Locked.hpp" |
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,52 @@ | ||
#pragma once | ||
|
||
#include "Lockable.hpp" | ||
|
||
namespace TiltedPhoques | ||
{ | ||
template<class T, class TLock> | ||
struct Locked | ||
{ | ||
Locked(SharedPtr<typename Lockable<T, TLock>::Data> apData) | ||
: m_pData(std::move(apData)) | ||
, m_handle(m_pData->Lock) | ||
{ | ||
} | ||
|
||
operator typename Lockable<T, TLock>::Ref() const | ||
{ | ||
return { m_pData }; | ||
} | ||
|
||
bool IsValid() const | ||
{ | ||
return (bool)m_pData; | ||
} | ||
|
||
operator T& () | ||
{ | ||
return Get(); | ||
} | ||
|
||
operator T&() const | ||
{ | ||
return Get(); | ||
} | ||
|
||
const T& Get() const noexcept | ||
{ | ||
return m_pData->Value; | ||
} | ||
|
||
T& Get() noexcept | ||
{ | ||
return m_pData->Value; | ||
} | ||
|
||
private: | ||
friend struct Lockable<T, TLock>; | ||
|
||
SharedPtr<typename Lockable<T, TLock>::Data> m_pData; | ||
std::scoped_lock<TLock> m_handle; | ||
}; | ||
} |
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