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,87 +114,83 @@ 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;
if (F_IsFile(file_name))
{ {
String file_name = file_node->s; String entry_name = file_name;
if (F_IsFile(file_name)) if (entry_name.len > (dir_path.len + 1))
{ {
String entry_name = file_name; entry_name.len -= dir_path.len + 1;
if (entry_name.len > (dir_path.len + 1)) entry_name.text += dir_path.len + 1;
{
entry_name.len -= dir_path.len + 1;
entry_name.text += dir_path.len + 1;
}
entry_name = StringF(perm, "%F/%F", FmtString(store_name), FmtString(entry_name));
for (u64 i = 0; i < entry_name.len; ++i)
{
if (entry_name.text[i] == '\\')
{
entry_name.text[i] = '/';
}
}
EntryNode *en = PushStruct(perm, EntryNode);
en->entry_name = entry_name;
en->file_name = file_name;
SllQueuePush(first_entry, last_entry, en);
++entries_count;
} }
entry_name = StringF(perm, "%F/%F", FmtString(store_name), FmtString(entry_name));
for (u64 i = 0; i < entry_name.len; ++i)
{
if (entry_name.text[i] == '\\')
{
entry_name.text[i] = '/';
}
}
EntryNode *en = PushStruct(perm, EntryNode);
en->entry_name = entry_name;
en->file_name = file_name;
SllQueuePush(first_entry, last_entry, en);
++entries_count;
} }
} }
BB_Buff bb = BB_AcquireBuff(Gibi(2));
BB_Writer bw = BB_WriterFromBuff(&bb);
/* Write magic */
BB_WriteUBits(&bw, ResourceEmbeddedMagic, 64);
/* Write header */
BB_WriteUBits(&bw, entries_count, 64);
/* Reserve entries space */
u64 entry_size = 8 /* Name start */
+ 8 /* Name end */
+ 8 /* Data start */
+ 8; /* Data end */
u8 *entries_start = BB_GetWrittenRaw(&bw) + BB_GetNumBytesWritten(&bw);
u64 entries_size = entry_size * entries_count;
String entries_str = STRING(entries_size, entries_start);
BB_WriteSeekBytes(&bw, entries_size);
BB_Buff entries_bb = BB_BuffFromString(entries_str);
BB_Writer entries_bw = BB_WriterFromBuff(&entries_bb);
/* Write entries */
for (EntryNode *en = first_entry; en; en = en->next)
{ {
BB_Buff bb = BB_AcquireBuff(Gibi(2)); /* TODO: Copy file data directly into archive file */
BB_Writer bw = BB_WriterFromBuff(&bb); String file_data = F_DataFromFile(perm, en->file_name);
/* Write magic */ /* Write name */
BB_WriteUBits(&bw, ResourceEmbeddedMagic, 64); BB_WriteAlignBytes(&bw, 64);
u64 name_start = BB_GetNumBytesWritten(&bw) + 1;
BB_WriteString(&bw, en->entry_name);
u64 name_len = BB_GetNumBytesWritten(&bw) - name_start;
/* Write header */ /* Write data */
BB_WriteUBits(&bw, entries_count, 64); BB_WriteAlignBytes(&bw, 64);
/* FIXME: Why no +1 here? */
u64 data_start = BB_GetNumBytesWritten(&bw);
BB_WriteBytes(&bw, file_data);
u64 data_len = BB_GetNumBytesWritten(&bw) - data_start;
/* Reserve entries space */ /* Write entry */
u64 entry_size = 8 /* Name start */ BB_WriteUBits(&entries_bw, name_start, 64);
+ 8 /* Name end */ BB_WriteUBits(&entries_bw, name_len, 64);
+ 8 /* Data start */ BB_WriteUBits(&entries_bw, data_start, 64);
+ 8; /* Data end */ BB_WriteUBits(&entries_bw, data_len, 64);
u8 *entries_start = BB_GetWrittenRaw(&bw) + BB_GetNumBytesWritten(&bw);
u64 entries_size = entry_size * entries_count;
String entries_str = STRING(entries_size, entries_start);
BB_WriteSeekBytes(&bw, entries_size);
BB_Buff entries_bb = BB_BuffFromString(entries_str);
BB_Writer entries_bw = BB_WriterFromBuff(&entries_bb);
/* Write entries */
for (EntryNode *en = first_entry; en; en = en->next)
{
/* TODO: Copy file data directly into archive file */
String file_data = F_DataFromFile(perm, en->file_name);
/* Write name */
BB_WriteAlignBytes(&bw, 64);
u64 name_start = BB_GetNumBytesWritten(&bw) + 1;
BB_WriteString(&bw, en->entry_name);
u64 name_len = BB_GetNumBytesWritten(&bw) - name_start;
/* Write data */
BB_WriteAlignBytes(&bw, 64);
/* FIXME: Why no +1 here? */
u64 data_start = BB_GetNumBytesWritten(&bw);
BB_WriteBytes(&bw, file_data);
u64 data_len = BB_GetNumBytesWritten(&bw) - data_start;
/* Write entry */
BB_WriteUBits(&entries_bw, name_start, 64);
BB_WriteUBits(&entries_bw, name_len, 64);
BB_WriteUBits(&entries_bw, data_start, 64);
BB_WriteUBits(&entries_bw, data_len, 64);
}
arc_contents.len = BB_GetNumBytesWritten(&bw);
arc_contents.text = BB_GetWrittenRaw(&bw);
} }
arc_contents.len = BB_GetNumBytesWritten(&bw);
arc_contents.text = BB_GetWrittenRaw(&bw);
} }
/* Write archive to file */ /* Write archive to file */
@ -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)
{ {