vn/code/core/core_thread_context.cpp

56 lines
1.3 KiB
C++
Raw Normal View History

2023-06-21 16:59:36 +00:00
per_thread thread_context *ThreadLocal_ThreadContext = 0;
2023-07-19 15:09:41 +00:00
static thread_context AllocateThreadContext(void)
{
thread_context Context = {};
for(s64 ArenaIndex = 0; ArenaIndex < ArrayCount(Context.Arenas); ArenaIndex += 1)
{
Context.Arenas[ArenaIndex] = ArenaAlloc(Megabytes(2), true);
2023-07-19 15:09:41 +00:00
}
return(Context);
}
2023-06-21 16:59:36 +00:00
static void SetThreadContext(thread_context *Context)
{
ThreadLocal_ThreadContext = Context;
}
static thread_context *GetThreadContext(void)
{
return(ThreadLocal_ThreadContext);
}
static temporary_memory GetScratch(arena **Conflicts, u64 ConflictCount)
2023-06-21 16:59:36 +00:00
{
temporary_memory Scratch = {};
thread_context *Context = GetThreadContext();
for(u64 ArenaIndex = 0;
ArenaIndex < ArrayCount(Context->Arenas);
++ArenaIndex)
{
b32 FoundConflict = false;
for(u64 ConflictIndex = 0;
ConflictIndex < ConflictCount;
++ConflictIndex)
{
arena *Conflict = Conflicts[ConflictIndex];
2023-07-19 15:09:41 +00:00
if(Conflict == Context->Arenas[ArenaIndex])
2023-06-21 16:59:36 +00:00
{
FoundConflict = true;
break;
}
}
if(!FoundConflict)
{
2023-07-19 15:09:41 +00:00
Scratch = BeginTemporaryMemory(Context->Arenas[ArenaIndex]);
2023-06-21 16:59:36 +00:00
break;
}
}
Assert(Scratch.Arena);
return(Scratch);
}