System/FFI/LibC: Round-robin mem_task selection for malloc

This fixes an issue where multiple HTTP requests would arrive
simultaneously, and the subsequent calls to @rsa_import would
end up clobbering the shared mem_task's CHeapCtrl, leading to #GP.

Eventually, we may want to simplify this to only init the CHeapCtrl,
rather than creating the entire CTask, but for now it seems ok.
This commit is contained in:
Alec Murphy 2025-03-10 07:51:19 -04:00
parent 546cbaf18d
commit e982c9584a
2 changed files with 35 additions and 1 deletions

View file

@ -118,11 +118,18 @@ U0 ntohs()
POP_SYSV_REGS
}
U64 @malloc(I64 size)
{
U64 res = MAlloc(size, malloc_mem_task[malloc_current_mem_task % MALLOC_MEM_TASK_COUNT]->code_heap);
malloc_current_mem_task++;
return res;
}
U0 malloc()
{
PUSH_SYSV_REGS
GET_SYSV_ARGS
MAlloc(p0, slon_mem_task->code_heap);
@malloc(p0);
POP_SYSV_REGS
}

View file

@ -51,6 +51,13 @@ I64 @t(Bool _condition, I64 _true, I64 _false)
return _false;
}
U0 @slon_mem_task_loop()
{
while (1) {
Sleep(1);
};
}
// Before doing anything else, we:
// 1. Mark memory in code heap below 0x1000000 as used.
@ -62,6 +69,26 @@ sys_code_bp->mem_free_lst = ShrinkMemBlkByPags(sys_code_bp->mem_free_lst, 131072
// 3. Enable SSE
@sse_enable;
// 4. Init mem_tasks
CTask* slon_mem_task = Spawn(&@slon_mem_task_loop, , "SlonMemTask");
#define MALLOC_MEM_TASK_COUNT 4
CTask** malloc_mem_task = CAlloc(sizeof(CTask*) * MALLOC_MEM_TASK_COUNT, slon_mem_task);
I64 malloc_current_mem_task = 0;
U0 @malloc_mem_tasks_init()
{
U8* scratch_buffer[64];
I64 i;
for (i = 0; i < MALLOC_MEM_TASK_COUNT; i++) {
StrPrint(scratch_buffer, "SlonMallocTask%d", i);
malloc_mem_task[i] = Spawn(&@slon_mem_task_loop, , scratch_buffer);
}
}
@malloc_mem_tasks_init;
U0 dd() { DocDump(adam_task->put_doc); }
//@patch_jmp_rel32(&Dbg2, &Reboot); // Reboot instead of crashing to the debugger
U0 NoBeep(I8, Bool) {};