Add threadlock and universal fs functions

This commit is contained in:
murdle
2026-03-01 23:19:08 +02:00
parent 53bd1b7e5b
commit 41ad19cd15
92 changed files with 618 additions and 281 deletions

View File

@@ -1,7 +1,8 @@
#include "C4JThread.h"
std::vector<C4JThread*> C4JThread::ms_threadList;
std::mutex C4JThread::ms_threadListMutex;
ThreadLock C4JThread::ms_threadListLock;
thread_local C4JThread* C4JThread::tls_currentThread = nullptr;
C4JThread C4JThread::m_mainThread("MainThread");
@@ -12,20 +13,22 @@ C4JThread::Event::Event(EMode mode)
void C4JThread::Event::Set()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_lock.lock();
m_signaled = true;
m_cv.notify_all();
m_lock.unlock();
}
void C4JThread::Event::Clear()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_lock.lock();
m_signaled = false;
m_lock.unlock();
}
unsigned long C4JThread::Event::WaitForSignal(int timeoutMs)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_lock.lock();
if (timeoutMs < 0)
{
@@ -40,6 +43,7 @@ unsigned long C4JThread::Event::WaitForSignal(int timeoutMs)
if (m_mode == e_modeAutoClear)
m_signaled = false;
m_lock.unlock();
return 0;
}
@@ -47,31 +51,40 @@ C4JThread::EventArray::EventArray(int size, Event::EMode mode)
{
m_events.reserve(size);
for (int i = 0; i < size; ++i)
m_events.emplace_back(mode);
{
m_events.push_back(std::unique_ptr<Event>(new Event(mode)));
}
}
void C4JThread::EventArray::Set(int i) { m_events[i].Set(); }
void C4JThread::EventArray::Clear(int i) { m_events[i].Clear(); }
void C4JThread::EventArray::Set(int i)
{
m_events[i]->Set();
}
void C4JThread::EventArray::Clear(int i)
{
m_events[i]->Clear();
}
void C4JThread::EventArray::SetAll()
{
for (auto& e : m_events) e.Set();
for (auto& e : m_events) e->Set();
}
void C4JThread::EventArray::ClearAll()
{
for (auto& e : m_events) e.Clear();
for (auto& e : m_events) e->Clear();
}
unsigned long C4JThread::EventArray::WaitForSingle(int index, int timeoutMs)
{
return m_events[index].WaitForSignal(timeoutMs);
return m_events[index]->WaitForSignal(timeoutMs);
}
unsigned long C4JThread::EventArray::WaitForAll(int timeoutMs)
{
for (auto& e : m_events)
e.WaitForSignal(timeoutMs);
e->WaitForSignal(timeoutMs);
return 0;
}
@@ -81,7 +94,7 @@ unsigned long C4JThread::EventArray::WaitForAny(int timeoutMs)
{
for (auto& e : m_events)
{
if (e.WaitForSignal(0) == 0)
if (e->WaitForSignal(0) == 0)
return 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
@@ -92,9 +105,9 @@ C4JThread::C4JThread(C4JThreadStartFunc* startFunc, void* param, const char* nam
: m_threadParam(param), m_startFunc(startFunc)
{
std::strncpy(m_threadName, name, sizeof(m_threadName));
std::lock_guard<std::mutex> lock(ms_threadListMutex);
ms_threadListLock.lock();
ms_threadList.push_back(this);
ms_threadListLock.unlock();
}
C4JThread::C4JThread(const char* mainThreadName)