Musa-Cpp-Lib-V2/lib/Base/Base_Thread_Context.h

56 lines
1.6 KiB
C

#pragma once
#include "Allocator.h"
#include "Base.h"
// #include "Arena.h"
struct Arena; // #TEMP - Base_Thread_Context and Arena rely on each other, so I have to figure out how to make this less messy (maybe with meta-generated forward declares?)
// See Context_Base in jai, and TCTX in raddebugger:
struct Thread_Context {
Arena* temp; // Used for temporary allocations, scratch space.
Arena* arena; // general purpose local arena
Allocator allocator;
s32 thread_idx;
u16 _padding0;
u16 GPAllocator_alignment = 16;
// Logger logger;
// Stack_Trace* stack_trace;
// #TODO: other debug information
// #TODO:
// Array<Thread*> threads_created; // maybe should be linked-list?
// Thread* thread_that_created_me = nullptr; // so we can remove from above array
// Mutex thread_context_mutex;
string thread_name;
};
Thread_Context* get_thread_context();
// maybe should append line number to guard?
// This is stupid, and maybe should be something generated by a metaprogram?
#define push_allocator(x) Push_Allocator Concat(_push_alloc_guard_, __LINE__)(x)
struct Push_Allocator {
Thread_Context* context;
Allocator old_allocator;
Push_Allocator (Allocator new_allocator) {
context = get_thread_context();
old_allocator = context->allocator;
context->allocator = new_allocator;
}
~Push_Allocator () {
context->allocator = old_allocator;
}
};
// Thread-local allocators:
PROTOTYPING_API Allocator get_temp_allocator();
PROTOTYPING_API Allocator get_context_allocator();
// PROTOTYPING_API void reset_temp_allocator();
// PROTOTYPING_API void free_temp_allocator();