Files
core_zero/tjp/core/threads/LockedBy.hpp
Timothy Prepscius 996151a56a flatten 20260225
2026-02-25 12:43:06 -05:00

57 lines
896 B
C++
Executable File

// 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