track total thread count

This commit is contained in:
jacob 2025-12-07 09:32:26 -06:00
parent 52d1f6da72
commit 702d33613f
4 changed files with 104 additions and 97 deletions

View File

@ -17,7 +17,6 @@ Arena *AcquireArena(u64 reserve)
u8 *base = ReserveMemory(reserve); u8 *base = ReserveMemory(reserve);
if (!base) if (!base)
{ {
/* Hard fail on memory reserve failure for now */
Panic(Lit("Failed to reserve memory")); Panic(Lit("Failed to reserve memory"));
} }
u64 reserved = reserve; u64 reserved = reserve;
@ -27,7 +26,6 @@ Arena *AcquireArena(u64 reserve)
base = CommitMemory(base, ArenaBlockSize); base = CommitMemory(base, ArenaBlockSize);
if (!base) if (!base)
{ {
/* Hard fail on commit failure */
Panic(Lit("Failed to commit initial memory block: System may be out of memory")); Panic(Lit("Failed to commit initial memory block: System may be out of memory"));
} }
@ -96,7 +94,7 @@ void *PushBytesNoZero(Arena *arena, u64 size, u64 align)
{ {
Assert(align > 0); Assert(align > 0);
void *ptr = 0; void *result = 0;
u8 *base = ArenaFirst(arena, u8); u8 *base = ArenaFirst(arena, u8);
/* Check to avoid aligning when size = 0 */ /* Check to avoid aligning when size = 0 */
@ -128,16 +126,16 @@ void *PushBytesNoZero(Arena *arena, u64 size, u64 align)
AsanPoison(commit_address, commit_bytes); AsanPoison(commit_address, commit_bytes);
} }
ptr = base + aligned_start_pos; result = base + aligned_start_pos;
AsanUnpoison(ptr, new_pos - aligned_start_pos); AsanUnpoison(result, new_pos - aligned_start_pos);
arena->pos = new_pos; arena->pos = new_pos;
} }
else else
{ {
ptr = base + arena->pos; result = base + arena->pos;
} }
return ptr; return result;
} }
void *PushBytes(Arena *arena, u64 size, u64 align) void *PushBytes(Arena *arena, u64 size, u64 align)
@ -149,6 +147,7 @@ void *PushBytes(Arena *arena, u64 size, u64 align)
void *PushAlign(Arena *arena, u64 align) void *PushAlign(Arena *arena, u64 align)
{ {
void *result = 0;
if (align > 0) if (align > 0)
{ {
u64 aligned_start_pos = (arena->pos + (align - 1)); u64 aligned_start_pos = (arena->pos + (align - 1));
@ -156,19 +155,20 @@ void *PushAlign(Arena *arena, u64 align)
u64 align_bytes = aligned_start_pos - (u64)arena->pos; u64 align_bytes = aligned_start_pos - (u64)arena->pos;
if (align_bytes > 0) if (align_bytes > 0)
{ {
return (void *)PushStructsNoZero(arena, u8, align_bytes); result = (void *)PushStructsNoZero(arena, u8, align_bytes);
} }
else else
{ {
return (void *)(ArenaFirst(arena, u8) + arena->pos); result = (void *)(ArenaFirst(arena, u8) + arena->pos);
} }
} }
else else
{ {
/* 0 alignment */ /* 0 alignment */
Assert(0); Assert(0);
return (void *)(ArenaFirst(arena, u8) + arena->pos); result = (void *)(ArenaFirst(arena, u8) + arena->pos);
} }
return result;
} }
void PopTo(Arena *arena, u64 pos) void PopTo(Arena *arena, u64 pos)
@ -200,16 +200,16 @@ void *ArenaFirst_(Arena *arena, u64 align)
{ {
u64 aligned_start_pos = align - 1; u64 aligned_start_pos = align - 1;
aligned_start_pos -= aligned_start_pos % align; aligned_start_pos -= aligned_start_pos % align;
void *ptr = ((u8 *)arena + ArenaHeaderSize) + aligned_start_pos; void *result = ((u8 *)arena + ArenaHeaderSize) + aligned_start_pos;
return ptr; return result;
} }
void *ArenaNext_(Arena *arena, u64 align) void *ArenaNext_(Arena *arena, u64 align)
{ {
u64 aligned_start_pos = (arena->pos + (align - 1)); u64 aligned_start_pos = (arena->pos + (align - 1));
aligned_start_pos -= aligned_start_pos % align; aligned_start_pos -= aligned_start_pos % align;
void *ptr = ((u8 *)arena + ArenaHeaderSize) + aligned_start_pos; void *result = ((u8 *)arena + ArenaHeaderSize) + aligned_start_pos;
return ptr; return result;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -1,6 +1,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Wave types //~ Wave types
#define MaxThreads 256
#define DefaultWaveLaneSpinCount 500 #define DefaultWaveLaneSpinCount 500
AlignedStruct(WaveCtx, CachelineSize) AlignedStruct(WaveCtx, CachelineSize)

View File

@ -37,15 +37,22 @@ DWORD WINAPI W32_ThreadProc(LPVOID thread_args_vp)
void DispatchWave(String name, u32 num_lanes, WaveLaneEntryFunc *entry, void *udata) void DispatchWave(String name, u32 num_lanes, WaveLaneEntryFunc *entry, void *udata)
{ {
Arena *perm = PermArena(); Arena *perm = PermArena();
PERSIST Atomic64 num_threads_allocated = ZI;
/* Catch high lane count to prevent OS crash while debugging */ /* Catch high lane count to prevent OS crash */
u32 max_lanes = 4096; i64 old_num_threads_allocated = Atomic64FetchAdd(&num_threads_allocated, num_lanes);
if (num_lanes <= 0 || num_lanes > max_lanes) i64 new_num_threads_allocated = old_num_threads_allocated + num_lanes;
if (new_num_threads_allocated > MaxThreads)
{ {
Panic(StringF(perm, if (old_num_threads_allocated < MaxThreads)
"Tried to create a wave with %F lanes (valid range is [1, %F])", {
FmtUint(num_lanes), Panic(StringF(perm, "Maximum number of threads reached (%F)", FmtUint(MaxThreads)));
FmtUint(max_lanes))); }
else
{
/* Sleep until panic */
Sleep(INFINITE);
}
} }
WaveCtx *wave_ctx = PushStruct(perm, WaveCtx); WaveCtx *wave_ctx = PushStruct(perm, WaveCtx);

View File

@ -114,7 +114,6 @@ EmbedObj Embed(String store_name, String dir_path)
EntryNode *first_entry = 0; EntryNode *first_entry = 0;
EntryNode *last_entry = 0; EntryNode *last_entry = 0;
u64 entries_count = 0; u64 entries_count = 0;
{
for (StringListNode *file_node = files.first; file_node; file_node = file_node->next) for (StringListNode *file_node = files.first; file_node; file_node = file_node->next)
{ {
String file_name = file_node->s; String file_name = file_node->s;
@ -142,9 +141,7 @@ EmbedObj Embed(String store_name, String dir_path)
++entries_count; ++entries_count;
} }
} }
}
{
BB_Buff bb = BB_AcquireBuff(Gibi(2)); BB_Buff bb = BB_AcquireBuff(Gibi(2));
BB_Writer bw = BB_WriterFromBuff(&bb); BB_Writer bw = BB_WriterFromBuff(&bb);
@ -195,7 +192,6 @@ EmbedObj Embed(String store_name, String dir_path)
arc_contents.len = BB_GetNumBytesWritten(&bw); arc_contents.len = BB_GetNumBytesWritten(&bw);
arc_contents.text = BB_GetWrittenRaw(&bw); arc_contents.text = BB_GetWrittenRaw(&bw);
} }
}
/* Write archive to file */ /* Write archive to file */
String arc_path = StringF(perm, "%F.arc", FmtString(store_name)); String arc_path = StringF(perm, "%F.arc", FmtString(store_name));
@ -232,8 +228,8 @@ EmbedObj Embed(String store_name, String dir_path)
} }
else else
{ {
/* TODO: Compile object files using .incbin on non-windows platforms */ /* TODO: Generate object files using .incbin on non-windows platforms */
Panic(Lit("Embedded resource path not implemented for this platform")); Panic(Lit("Resource embedding not implemented for this platform"));
} }
return result; return result;
@ -790,7 +786,7 @@ void BuildEntryPoint(WaveLaneCtx *lane)
SetBuildStatus(build.res_dir_parse.errors.count > 0); SetBuildStatus(build.res_dir_parse.errors.count > 0);
} }
//- Prep objs //- Prep obj arrays
{ {
/* Gpu objs */ /* Gpu objs */
build.gpu_objs.count = build.gpu_parse.shader_entries_count; build.gpu_objs.count = build.gpu_parse.shader_entries_count;
@ -872,9 +868,9 @@ void BuildEntryPoint(WaveLaneCtx *lane)
SetBuildStatus(gpu_obj->return_code); SetBuildStatus(gpu_obj->return_code);
/* Final shader compilation embeds shader archive */ /* Final shader compilation lane embeds shader archive */
u32 finished_count = Atomic32FetchAdd(&build.gpu_objs.finished_count, 1) + 1; u32 finished_count = Atomic32FetchAdd(&build.gpu_objs.finished_count, 1) + 1;
if (finished_count == build.gpu_objs.count) if (finished_count == build.gpu_objs.count && GetBuildStatus() == 0)
{ {
EmbedObj *embed = &build.embed_objs.array[embed_idx]; EmbedObj *embed = &build.embed_objs.array[embed_idx];
*embed = Embed(shader_store_name, shader_store_name); *embed = Embed(shader_store_name, shader_store_name);
@ -882,10 +878,13 @@ void BuildEntryPoint(WaveLaneCtx *lane)
} }
++gpu_obj_idx; ++gpu_obj_idx;
} }
if (build.gpu_parse.shader_entries_count > 0)
{
++embed_idx;
}
} }
++embed_idx; /* Shader archive embed */
//- Embed resources //- Embed resource dirs
{ {
for (ResDir *rd = build.res_dir_parse.first_res_dir; rd; rd = rd->next) for (ResDir *rd = build.res_dir_parse.first_res_dir; rd; rd = rd->next)
{ {