flatten 20260225
This commit is contained in:
65
tjp/core/threads/ThreadSafe.hpp
Executable file
65
tjp/core/threads/ThreadSafe.hpp
Executable file
@@ -0,0 +1,65 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Lock.h"
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
template<typename T, typename MutexType = Mutex>
|
||||
struct Accessor;
|
||||
|
||||
template<typename T, typename MutexType = Mutex>
|
||||
struct ThreadSafe
|
||||
{
|
||||
using A = Accessor<T, MutexType>;
|
||||
using A_const = Accessor<const T, MutexType>;
|
||||
|
||||
T v;
|
||||
mutable MutexType mutex;
|
||||
|
||||
A access ()
|
||||
{
|
||||
return A { &v, &mutex };
|
||||
}
|
||||
|
||||
A_const access () const
|
||||
{
|
||||
return A_const { &v, &mutex };
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
auto with(F &&f)
|
||||
{
|
||||
auto lk = lock_of(mutex);
|
||||
return f(v);
|
||||
}
|
||||
} ;
|
||||
|
||||
template<typename T, typename MutexType>
|
||||
struct Accessor
|
||||
{
|
||||
using V = ThreadSafe<T, MutexType>;
|
||||
T *v;
|
||||
typename MutexType::Lock lock;
|
||||
|
||||
Accessor(T *v_, MutexType *m) :
|
||||
v(v_),
|
||||
lock(*m)
|
||||
{
|
||||
}
|
||||
|
||||
T *operator ->()
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
T &operator *()
|
||||
{
|
||||
return *v;
|
||||
}
|
||||
} ;
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user