Skip to content

Commit a022537

Browse files
authored
Merge pull request #28 from choco-technologies/copilot/expand-stack-size-for-threads
Add DMOSI_THREAD_STACK_OVERHEAD to account for thread startup stack usage
2 parents 2f517a0 + 7f3b8bf commit a022537

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

include/dmosi.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ typedef struct {
372372
uint64_t runtime_ms; /**< Thread runtime in milliseconds */
373373
} dmosi_thread_info_t;
374374

375+
/**
376+
* @brief Extra stack bytes reserved for thread and module startup overhead
377+
*
378+
* The thread entry wrapper and module loading infrastructure consume stack
379+
* before the module's own main function is invoked. This constant accounts
380+
* for that overhead so that the effective stack available to the module
381+
* matches the size declared in its header.
382+
*/
383+
#define DMOSI_THREAD_STACK_OVERHEAD 512
384+
375385
/**
376386
* @brief Create a thread
377387
*

src/dmosi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,12 @@ static Dmod_Pid_t dmod_spawn_module_internal(Dmod_Context_t* Context, int argc,
593593
spawn_args->argv = argv;
594594
spawn_args->process = new_process;
595595

596-
// Get stack size from Context header
596+
// Get stack size from Context header and add overhead for thread/module startup
597597
uint64_t stack_size = Dmod_GetStackSize(Context);
598598
if (stack_size == 0) {
599599
stack_size = DMOSI_DEFAULT_STACK_SIZE;
600600
}
601+
stack_size += DMOSI_THREAD_STACK_OVERHEAD;
601602

602603
// Inherit priority from current thread
603604
int priority = dmosi_thread_get_priority(NULL);

0 commit comments

Comments
 (0)