flatten 20260225
This commit is contained in:
56
tjp/core/threads/LockedBy.hpp
Executable file
56
tjp/core/threads/LockedBy.hpp
Executable file
@@ -0,0 +1,56 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "LockedBy.h"
|
||||
#include <thread>
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
template<typename T>
|
||||
class LockedBy
|
||||
{
|
||||
public:
|
||||
T m;
|
||||
|
||||
void lock()
|
||||
{
|
||||
m.lock();
|
||||
if (++rentry == 1)
|
||||
m_holder = std::this_thread::get_id();
|
||||
}
|
||||
|
||||
bool try_lock()
|
||||
{
|
||||
auto locked = m.try_lock();
|
||||
if (locked)
|
||||
{
|
||||
if (++rentry == 1)
|
||||
m_holder = std::this_thread::get_id();
|
||||
}
|
||||
|
||||
return locked;
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
if (--rentry == 0)
|
||||
m_holder = {};
|
||||
m.unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true iff the mutex is locked by the caller of this method. */
|
||||
bool locked_by_caller() const
|
||||
{
|
||||
return m_holder == std::this_thread::get_id();
|
||||
}
|
||||
|
||||
private:
|
||||
size_t rentry = 0;
|
||||
std::thread::id m_holder;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user