port(EterLib): Mutex/Thread/FileLoaderThread/GrpVertexBufferDynamic verbatim over Win32Crt kernel-object shims

Adds CreateSemaphore/ReleaseSemaphore/WaitForSingleObject/CloseHandle/
SetThreadPriority/_beginthreadex to Win32Crt; port.file_loader_thread covers
the shims, CThread and both the un-Created (40250 live) and Created loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-09-30 12:56:24 +09:00
co-authored by Claude Opus 5.5
parent e0cdcee2e6
commit 042bddf195
13 changed files with 758 additions and 75 deletions
+142 -33
View File
@@ -1,72 +1,181 @@
// Platform skeleton for EterLib/FileLoaderThread.h (40250 EterLib/FileLoaderThread.cpp), generated by platform_stub.py.
// Every MT_PLATFORM_STUB() body is unimplemented: replace it with the platform implementation.
// 40250 EterLib/FileLoaderThread.cpp, verbatim. The kernel32 calls resolve to the Win32Crt thread/semaphore/critical-section shims.
#include "EterLib/StdAfx.h"
#include "EterPack/EterPackManager.h"
#include "EterLib/FileLoaderThread.h"
#include "EterLib/ResourceManager.h"
#include "../PlatformStub.h"
CFileLoaderThread::CFileLoaderThread()
CFileLoaderThread::CFileLoaderThread() : m_bShutdowned(false), m_pArg(NULL), m_hThread(NULL), m_uThreadID(0)
{
MT_PLATFORM_STUB();
}
CFileLoaderThread::~CFileLoaderThread()
{
MT_PLATFORM_STUB();
Destroy();
}
auto CFileLoaderThread::Create(void *) -> int
int CFileLoaderThread::Create(void * arg)
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<int>();
Arg(arg);
m_hThread = (HANDLE) _beginthreadex(NULL, 0, EntryPoint, this, 0, &m_uThreadID);
if (!m_hThread)
return false;
SetThreadPriority(m_hThread, THREAD_PRIORITY_NORMAL);
return true;
}
auto CFileLoaderThread::Request(std::string &) -> void
UINT CFileLoaderThread::Run(void * arg)
{
MT_PLATFORM_STUB();
if (!Setup())
return 0;
return (Execute(arg));
}
auto CFileLoaderThread::Fetch(TData **) -> bool
/* Static */
UINT CALLBACK CFileLoaderThread::EntryPoint(void * pThis)
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<bool>();
CFileLoaderThread * pThread = (CFileLoaderThread *) pThis;
return pThread->Run(pThread->Arg());
}
auto CFileLoaderThread::Shutdown() -> void
//////////////////////////////////////////////////////////////////////////
void CFileLoaderThread::Destroy()
{
MT_PLATFORM_STUB();
if (m_hSemaphore)
{
CloseHandle(m_hSemaphore);
m_hSemaphore = NULL;
}
stl_wipe(m_pRequestDeque);
stl_wipe(m_pCompleteDeque);
}
auto CFileLoaderThread::EntryPoint(void *) -> UINT
UINT CFileLoaderThread::Setup()
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<UINT>();
m_hSemaphore = CreateSemaphore(NULL, // no security attributes
0, // initial count
65535, // maximum count
NULL); // unnamed semaphore
if (!m_hSemaphore)
return 0;
return 1;
}
auto CFileLoaderThread::Run(void *) -> UINT
void CFileLoaderThread::Shutdown()
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<UINT>();
if (!m_hSemaphore)
return;
BOOL bRet;
m_bShutdowned = true;
do
{
bRet = ReleaseSemaphore(m_hSemaphore, 1, NULL);
}
while (!bRet);
WaitForSingleObject(m_hThread, 10000); // 쓰레드가 종료 되기를 10초 기다림
}
auto CFileLoaderThread::Setup() -> UINT
UINT CFileLoaderThread::Execute(void * /*pvArg*/)
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<UINT>();
while (!m_bShutdowned)
{
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(m_hSemaphore, INFINITE);
if (m_bShutdowned)
break;
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
{
Process();
}
break;
case WAIT_TIMEOUT:
TraceError("CFileLoaderThread::Execute: Timeout occured while time-out interval is INIFITE");
break;
}
}
Destroy();
return 1;
}
auto CFileLoaderThread::Execute(void *) -> UINT
void CFileLoaderThread::Request(std::string & c_rstFileName) // called in main thread
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<UINT>();
TData * pData = new TData;
pData->dwSize = 0;
pData->pvBuf = NULL;
pData->stFileName = c_rstFileName;
m_RequestMutex.Lock();
m_pRequestDeque.push_back(pData);
m_RequestMutex.Unlock();
++m_iRestSemCount;
if (!ReleaseSemaphore(m_hSemaphore, m_iRestSemCount, NULL))
TraceError("CFileLoaderThread::Request: ReleaseSemaphore error");
--m_iRestSemCount;
}
auto CFileLoaderThread::Destroy() -> void
bool CFileLoaderThread::Fetch(TData ** ppData) // called in main thread
{
MT_PLATFORM_STUB();
m_CompleteMutex.Lock();
if (m_pCompleteDeque.empty())
{
m_CompleteMutex.Unlock();
return false;
}
*ppData = m_pCompleteDeque.front();
m_pCompleteDeque.pop_front();
m_CompleteMutex.Unlock();
return true;
}
auto CFileLoaderThread::Process() -> void
void CFileLoaderThread::Process() // called in loader thread
{
MT_PLATFORM_STUB();
m_RequestMutex.Lock();
if (m_pRequestDeque.empty())
{
m_RequestMutex.Unlock();
return;
}
TData * pData = m_pRequestDeque.front();
m_pRequestDeque.pop_front();
m_RequestMutex.Unlock();
LPCVOID pvBuf;
if (CEterPackManager::Instance().Get(pData->File, pData->stFileName.c_str(), &pvBuf))
{
pData->dwSize = pData->File.Size();
pData->pvBuf = new char [pData->dwSize];
memcpy(pData->pvBuf, pvBuf, pData->dwSize);
}
m_CompleteMutex.Lock();
m_pCompleteDeque.push_back(pData);
m_CompleteMutex.Unlock();
Sleep(g_iLoadingDelayTime);
}
+19 -11
View File
@@ -1,22 +1,30 @@
// Platform skeleton for EterLib/GrpVertexBufferDynamic.h (40250 EterLib/GrpVertexBufferDynamic.cpp), generated by platform_stub.py.
// Every MT_PLATFORM_STUB() body is unimplemented: replace it with the platform implementation.
// 40250 EterLib/GrpVertexBufferDynamic.cpp, verbatim.
#include "EterLib/StdAfx.h"
#include "EterLib/GrpVertexBufferDynamic.h"
#include "../PlatformStub.h"
bool CDynamicVertexBuffer::Create(int vtxCount, int fvf)
{
if (m_lpd3dVB)
{
if (m_fvf == fvf)
{
if (m_vtxCount >= vtxCount)
return true;
}
}
m_vtxCount = vtxCount;
m_fvf = fvf;
return CGraphicVertexBuffer::Create(m_vtxCount, m_fvf, D3DUSAGE_DYNAMIC, D3DPOOL_SYSTEMMEM);
}
CDynamicVertexBuffer::CDynamicVertexBuffer()
{
MT_PLATFORM_STUB();
m_vtxCount = 0;
m_fvf = 0;
}
CDynamicVertexBuffer::~CDynamicVertexBuffer()
{
MT_PLATFORM_STUB();
}
auto CDynamicVertexBuffer::Create(int, int) -> bool
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<bool>();
}
+7 -16
View File
@@ -1,32 +1,23 @@
// Platform skeleton for EterLib/Mutex.h (40250 EterLib/Mutex.cpp), generated by platform_stub.py.
// Every MT_PLATFORM_STUB() body is unimplemented: replace it with the platform implementation.
// 40250 EterLib/Mutex.cpp, verbatim. The kernel32 calls resolve to the Win32Crt thread/semaphore/critical-section shims.
#include "EterLib/StdAfx.h"
#include "EterLib/Mutex.h"
#include "../PlatformStub.h"
Mutex::Mutex()
{
MT_PLATFORM_STUB();
InitializeCriticalSection(&lock);
}
Mutex::~Mutex()
{
MT_PLATFORM_STUB();
DeleteCriticalSection(&lock);
}
auto Mutex::Lock() -> void
void Mutex::Lock()
{
MT_PLATFORM_STUB();
EnterCriticalSection(&lock);
}
auto Mutex::Unlock() -> void
void Mutex::Unlock()
{
MT_PLATFORM_STUB();
}
auto Mutex::Trylock() -> bool
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<bool>();
LeaveCriticalSection(&lock);
}
+20 -15
View File
@@ -1,29 +1,34 @@
// Platform skeleton for EterLib/Thread.h (40250 EterLib/Thread.cpp), generated by platform_stub.py.
// Every MT_PLATFORM_STUB() body is unimplemented: replace it with the platform implementation.
// 40250 EterLib/Thread.cpp, verbatim. The kernel32 calls resolve to the Win32Crt thread/semaphore/critical-section shims.
#include "EterLib/StdAfx.h"
#include "EterLib/Thread.h"
#include "../PlatformStub.h"
CThread::CThread()
CThread::CThread() : m_pArg(NULL), m_hThread(NULL), m_uThreadID(0)
{
MT_PLATFORM_STUB();
}
auto CThread::Create(void *) -> int
int CThread::Create(void * arg)
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<int>();
Arg(arg);
m_hThread = (HANDLE) _beginthreadex(NULL, 0, EntryPoint, this, 0, &m_uThreadID);
if (!m_hThread)
return false;
SetThreadPriority(m_hThread, THREAD_PRIORITY_NORMAL);
return true;
}
auto CThread::EntryPoint(void *) -> UINT
UINT CThread::Run(void * arg)
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<UINT>();
if (!Setup())
return 0;
return (Execute(arg));
}
auto CThread::Run(void *) -> UINT
/* Static */
UINT CALLBACK CThread::EntryPoint(void * pThis)
{
MT_PLATFORM_STUB();
return mt_platform_stub_return<UINT>();
CThread * pThread = (CThread *) pThis;
return pThread->Run(pThread->Arg());
}
+3
View File
@@ -213,6 +213,9 @@ if(BUILD_TESTING AND CMAKE_SYSTEM_NAME STREQUAL CMAKE_HOST_SYSTEM_NAME)
add_executable(port_screenshot_test ${PROJECT_SOURCE_DIR}/tests/port/port_screenshot_test.cpp)
target_link_libraries(port_screenshot_test PRIVATE port_platform)
add_test(NAME port.screenshot COMMAND $<TARGET_FILE:port_screenshot_test>)
add_executable(port_file_loader_thread_test ${PROJECT_SOURCE_DIR}/tests/port/port_file_loader_thread_test.cpp)
target_link_libraries(port_file_loader_thread_test PRIVATE port_platform)
add_test(NAME port.file_loader_thread COMMAND $<TARGET_FILE:port_file_loader_thread_test>)
# EterBase/Debug.cpp: syserr.txt / log.txt (redirects its own stderr).
add_executable(port_debug_log_test ${PROJECT_SOURCE_DIR}/tests/port/port_debug_log_test.cpp)
+136
View File
@@ -3,6 +3,10 @@
#if !defined(_WIN32)
#include <cctype>
#include <chrono>
#include <atomic>
#include <condition_variable>
#include <system_error>
#include <memory>
#include <cstdio>
#include <cstring>
#include <mutex>
@@ -170,6 +174,138 @@ void Sleep(DWORD milliseconds)
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}
// A HANDLE from CreateSemaphore or _beginthreadex points at one of these. The waitable state is shared
// with a running thread, so CloseHandle on a live thread only drops the handle, as on Win32.
namespace
{
struct KernelObject
{
virtual ~KernelObject() = default;
virtual DWORD Wait(DWORD milliseconds) = 0;
};
template <class Ready>
DWORD wait_until(std::unique_lock<std::mutex>& lock, std::condition_variable& cv, DWORD milliseconds, Ready ready)
{
if (milliseconds == INFINITE)
cv.wait(lock, ready);
else if (!cv.wait_for(lock, std::chrono::milliseconds(milliseconds), ready))
return WAIT_TIMEOUT;
return WAIT_OBJECT_0;
}
struct Semaphore final : KernelObject
{
std::mutex mutex;
std::condition_variable cv;
LONG count = 0;
LONG maximum = 0;
DWORD Wait(DWORD milliseconds) override
{
std::unique_lock<std::mutex> lock(mutex);
const DWORD result = wait_until(lock, cv, milliseconds, [this] { return count > 0; });
if (result == WAIT_OBJECT_0)
--count;
return result;
}
};
struct ThreadState
{
std::mutex mutex;
std::condition_variable cv;
bool done = false;
};
struct Thread final : KernelObject
{
std::shared_ptr<ThreadState> state = std::make_shared<ThreadState>();
DWORD Wait(DWORD milliseconds) override
{
std::unique_lock<std::mutex> lock(state->mutex);
return wait_until(lock, state->cv, milliseconds, [this] { return state->done; });
}
};
}
HANDLE CreateSemaphore(void*, LONG initial_count, LONG maximum_count, LPCSTR)
{
if (maximum_count <= 0 || initial_count < 0 || initial_count > maximum_count)
return NULL;
Semaphore* semaphore = new Semaphore;
semaphore->count = initial_count;
semaphore->maximum = maximum_count;
return static_cast<KernelObject*>(semaphore);
}
// Fails without changing the count when the release would pass the maximum (ERROR_TOO_MANY_POSTS).
BOOL ReleaseSemaphore(HANDLE handle, LONG release_count, LONG* previous_count)
{
Semaphore* semaphore = handle ? dynamic_cast<Semaphore*>(static_cast<KernelObject*>(handle)) : nullptr;
if (!semaphore || release_count <= 0)
return FALSE;
{
std::lock_guard<std::mutex> lock(semaphore->mutex);
if (release_count > semaphore->maximum - semaphore->count)
return FALSE;
if (previous_count)
*previous_count = semaphore->count;
semaphore->count += release_count;
}
semaphore->cv.notify_all();
return TRUE;
}
DWORD WaitForSingleObject(HANDLE handle, DWORD milliseconds)
{
if (!handle || handle == INVALID_HANDLE_VALUE)
return WAIT_FAILED;
return static_cast<KernelObject*>(handle)->Wait(milliseconds);
}
BOOL CloseHandle(HANDLE handle)
{
if (!handle || handle == INVALID_HANDLE_VALUE)
return FALSE;
delete static_cast<KernelObject*>(handle);
return TRUE;
}
BOOL SetThreadPriority(HANDLE handle, int)
{
return handle ? TRUE : FALSE;
}
uintptr_t _beginthreadex(void*, unsigned, unsigned (*start_address)(void*), void* arglist, unsigned, unsigned* thrdaddr)
{
Thread* thread = new Thread;
std::shared_ptr<ThreadState> state = thread->state;
try
{
std::thread([state, start_address, arglist] {
start_address(arglist);
{
std::lock_guard<std::mutex> lock(state->mutex);
state->done = true;
}
state->cv.notify_all();
}).detach();
}
catch (const std::system_error&)
{
delete thread;
return 0;
}
if (thrdaddr)
{
static std::atomic<unsigned> next_id{1};
*thrdaddr = next_id++;
}
return reinterpret_cast<uintptr_t>(static_cast<KernelObject*>(thread));
}
char* _strlwr(char* s)
{
for (char* p = s; *p; ++p)
+15
View File
@@ -73,6 +73,21 @@ BOOL FindNextFile(HANDLE handle, WIN32_FIND_DATA* data);
BOOL FindClose(HANDLE handle);
void Sleep(DWORD milliseconds);
// kernel32 synchronization objects used by EterLib (FileLoaderThread): a counting semaphore and a
// _beginthreadex thread, both waitable with WaitForSingleObject and freed with CloseHandle.
// Only the unnamed, default-security forms exist; thread priority is accepted and ignored.
#define WAIT_OBJECT_0 0x00000000L
#define WAIT_TIMEOUT 0x00000102L
#define WAIT_FAILED 0xFFFFFFFF
#define THREAD_PRIORITY_NORMAL 0
HANDLE CreateSemaphore(void* security_attributes, LONG initial_count, LONG maximum_count, LPCSTR name);
BOOL ReleaseSemaphore(HANDLE semaphore, LONG release_count, LONG* previous_count);
DWORD WaitForSingleObject(HANDLE handle, DWORD milliseconds);
BOOL CloseHandle(HANDLE handle);
BOOL SetThreadPriority(HANDLE thread, int priority);
uintptr_t _beginthreadex(void* security, unsigned stack_size, unsigned (*start_address)(void*), void* arglist,
unsigned initflag, unsigned* thrdaddr);
// MSVC <stdlib.h> __min / __max
#ifndef __min
#define __min(a, b) (((a) < (b)) ? (a) : (b))