vn/code/vn_thread_context.h

62 lines
1.2 KiB
C
Raw Normal View History

2023-06-17 17:00:55 +00:00
/* date = April 30th 2023 11:35 am */
#ifndef VN_THREAD_CONTEXT_H
#define VN_THREAD_CONTEXT_H
#define per_thread __declspec(thread)
struct thread_context
{
2023-12-23 07:27:22 +00:00
memory_arena Arenas[2];
2023-06-17 17:00:55 +00:00
};
per_thread thread_context *ThreadLocal_ThreadContext = 0;
inline void SetThreadContext(thread_context *Context)
{
2023-12-23 07:27:22 +00:00
ThreadLocal_ThreadContext = Context;
2023-06-17 17:00:55 +00:00
}
inline thread_context *GetThreadContext(void)
{
2023-12-23 07:27:22 +00:00
return(ThreadLocal_ThreadContext);
2023-06-17 17:00:55 +00:00
}
2023-06-19 17:12:26 +00:00
static temporary_memory GetScratch(memory_arena **Conflicts = 0, u64 ConflictCount = 0)
2023-06-17 17:00:55 +00:00
{
2023-12-23 07:27:22 +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)
{
memory_arena *Conflict = Conflicts[ConflictIndex];
if(Conflict == Context->Arenas + ArenaIndex)
{
FoundConflict = true;
break;
}
}
if(!FoundConflict)
{
Scratch = BeginTemporaryMemory(Context->Arenas + ArenaIndex);
break;
}
}
Assert(Scratch.Arena);
return(Scratch);
2023-06-17 17:00:55 +00:00
}
2023-12-23 07:27:22 +00:00
#define ReleaseScratch(Scratch) EndTemp(Scratch)
2023-06-17 17:00:55 +00:00
#endif //VN_THREAD_CONTEXT_H