Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Engine/src/delta/platform/os_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ namespace delta::platform
void* CommitMemory(void* mem, size_t commitSize);
void DecommitMemory(void* mem, size_t decommitSize);
void ReleaseMemory(void* mem);

struct OSThreadHandle;
struct OSSemaphoreHandle;

struct ThreadCreationInfo
{
void (*entryPoint)(void*);
void* userData;
uint32_t coreAffinityMask;
const char* debugName;
};

OSThreadHandle* CreateEngineThread(const ThreadCreationInfo& info);
void DestroyEngineThread(OSThreadHandle* thread);
void SetThreadAffinity(OSThreadHandle* thread, uint32_t mask);

OSSemaphoreHandle* CreateEngineSemaphore(uint32_t initialCount);
void WaitOnSemaphore(OSSemaphoreHandle* sem);
void SignalSemaphore(OSSemaphoreHandle* sem, uint32_t count);
}
78 changes: 78 additions & 0 deletions Engine/src/delta/platform/os_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ namespace delta::platform
}
}

struct OSThreadHandle
{
HANDLE handle;
DWORD osThreadId;
};

struct OSSemaphoreHandle
{
HANDLE handle;
};

static DWORD WINAPI ThreadWindowsEntry(LPVOID param)
{
auto* info = static_cast<ThreadCreationInfo*>(param);
info->entryPoint(info->userData);
return 0;
}

inline static void fetchCpuidValues()
{
int cpuinfo[4];
Expand Down Expand Up @@ -136,6 +154,66 @@ namespace delta::platform

return status;
}

OSThreadHandle* CreateEngineThread(const ThreadCreationInfo& info)
{
OSThreadHandle* thread = new OSThreadHandle(); // TODO: custom global allocator, or living on the main thread
thread->handle = CreateThread(
nullptr, 0,
ThreadWindowsEntry,
(void*)&info,
0,
&thread->osThreadId
);

assert(thread->handle != 0);

if (info.debugName) {
// TODO: Convert to wide string
}

if (info.coreAffinityMask > 0) {
SetThreadAffinityMask(thread->handle, info.coreAffinityMask);
}

return thread;
}

void DestroyEngineThread(OSThreadHandle* thread)
{
if (!thread)
return;

WaitForSingleObject(thread->handle, INFINITE);
CloseHandle(thread->handle);

delete thread;
}

void SetThreadAffinity(OSThreadHandle* thread, uint32_t mask)
{
if (!thread)
return;

SetThreadAffinityMask(thread->handle, mask);
}

OSSemaphoreHandle* CreateEngineSemaphore(uint32_t initialCount)
{
OSSemaphoreHandle* sem = new OSSemaphoreHandle();
sem->handle = CreateSemaphoreA(nullptr, initialCount, LONG_MAX, nullptr);
return sem;
}

void WaitOnSemaphore(OSSemaphoreHandle* sem)
{
WaitForSingleObject(sem->handle, INFINITE);
}

void SignalSemaphore(OSSemaphoreHandle* sem, uint32_t count)
{
ReleaseSemaphore(sem->handle, count, nullptr);
}
}

#endif