Skip to content

Commit c46f7f5

Browse files
cfriedtkartben
authored andcommitted
doc: kernel: threads: add dynamic thread stack docs
Add documentation to indicate that, aside from static thread stack allocation, Zephyr supports dynamically allocated thread stacks. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent 582c983 commit c46f7f5

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

doc/kernel/services/threads/index.rst

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,12 @@ require their regions to be of some power of two in size, and aligned to its
189189
own size.
190190

191191
Because of this, portable code can't simply pass an arbitrary character buffer
192-
to :c:func:`k_thread_create`. Special macros exist to instantiate stacks,
193-
prefixed with ``K_KERNEL_STACK`` and ``K_THREAD_STACK``.
192+
to :c:func:`k_thread_create`. Special macros exist to instantiate stacks
193+
statically, prefixed with ``K_KERNEL_STACK`` and ``K_THREAD_STACK``.
194+
195+
Additionally, stacks may be instantiated dynamically using
196+
:c:func:`k_thread_stack_alloc` and subsequently freed with
197+
:c:func:`k_thread_stack_free`.
194198

195199
Kernel-only Stacks
196200
==================
@@ -411,8 +415,9 @@ Spawning a Thread
411415
A thread is spawned by defining its stack area and its thread control block,
412416
and then calling :c:func:`k_thread_create`.
413417

414-
The stack area must be defined using :c:macro:`K_THREAD_STACK_DEFINE` or
415-
:c:macro:`K_KERNEL_STACK_DEFINE` to ensure it is properly set up in memory.
418+
The stack area may be statically allocated using
419+
:c:macro:`K_THREAD_STACK_DEFINE` or :c:macro:`K_KERNEL_STACK_DEFINE` to ensure
420+
it is properly set up in memory.
416421

417422
The size parameter for the stack must be one of three values:
418423

@@ -426,6 +431,9 @@ The size parameter for the stack must be one of three values:
426431
macros, the return value of :c:macro:`K_KERNEL_STACK_SIZEOF()` for that
427432
object.
428433

434+
Alternatively, the stack area may be dynamically allocated using
435+
:c:func:`k_thread_stack_alloc` and freed using :c:func:`k_thread_stack_free`.
436+
429437
The thread spawning function returns its thread id, which can be used
430438
to reference the thread.
431439

@@ -470,6 +478,25 @@ The following code has the same effect as the code segment above.
470478
thread immediately. The corresponding parameter to :c:macro:`K_THREAD_DEFINE`
471479
is a duration in integral milliseconds, so the equivalent argument is 0.
472480

481+
The following code dynamically allocates a thread stack, waits until the thread
482+
has joined, and then frees the dynamically allocated thread stack.
483+
484+
.. code-block:: c
485+
486+
extern void my_entry_point(void *, void *, void *);
487+
488+
k_tid_t my_tid;
489+
void *my_stack_area;
490+
491+
my_stack_area = k_thread_stack_alloc(CONFIG_DYNAMIC_THREAD_STACK_SIZE);
492+
my_tid = k_thread_create(&my_thread_data, my_stack_area,
493+
CONFIG_DYNAMIC_THREAD_STACK_SIZE,
494+
my_entry_point,
495+
NULL, NULL, NULL,
496+
MY_PRIORITY, 0, K_NO_WAIT);
497+
k_thread_join(my_tid, K_FOREVER);
498+
k_thread_stack_free(my_stack_area);
499+
473500
User Mode Constraints
474501
---------------------
475502

include/zephyr/kernel.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,21 +332,25 @@ void k_thread_foreach_unlocked_filter_by_cpu(unsigned int cpu,
332332
/**
333333
* @brief Dynamically allocate a thread stack.
334334
*
335-
* Dynamically allocate a thread stack either from a pool of thread stacks of size
336-
* @kconfig{CONFIG_DYNAMIC_THREAD_POOL_SIZE}, or from the system heap. Order is determined by the
337-
* kconfig{CONFIG_DYNAMIC_THREAD_PREFER_ALLOC} and @kconfig{CONFIG_DYNAMIC_THREAD_PREFER_POOL}
338-
* options. Thread stacks from the pool are of maximum size
339-
* @kconfig{CONFIG_DYNAMIC_THREAD_STACK_SIZE}.
335+
* Dynamically allocate a thread stack either from a pool of thread stacks of
336+
* size @kconfig{CONFIG_DYNAMIC_THREAD_POOL_SIZE}, or from the system heap.
337+
* Order is determined by the @kconfig{CONFIG_DYNAMIC_THREAD_PREFER_ALLOC} and
338+
* @kconfig{CONFIG_DYNAMIC_THREAD_PREFER_POOL} options. Thread stacks from the
339+
* pool are of maximum size @kconfig{CONFIG_DYNAMIC_THREAD_STACK_SIZE}.
340340
*
341-
* Relevant stack creation flags include:
342-
* - @ref K_USER allocate a userspace thread (requires @kconfig{CONFIG_USERSPACE})
341+
* @note When no longer required, thread stacks allocated with
342+
* `k_thread_stack_alloc()` must be freed with @ref k_thread_stack_free to
343+
* avoid leaking memory.
343344
*
344345
* @param size Stack size in bytes.
345346
* @param flags Stack creation flags, or 0.
346347
*
347348
* @retval the allocated thread stack on success.
348349
* @retval NULL on failure.
349350
*
351+
* Relevant stack creation flags include:
352+
* - @ref K_USER allocate a userspace thread (requires @kconfig{CONFIG_USERSPACE})
353+
*
350354
* @see @kconfig{CONFIG_DYNAMIC_THREAD}
351355
*/
352356
__syscall k_thread_stack_t *k_thread_stack_alloc(size_t size, int flags);
@@ -389,8 +393,7 @@ __syscall int k_thread_stack_free(k_thread_stack_t *stack);
389393
* enabled.
390394
*
391395
* Alternatively, the stack may be dynamically allocated using
392-
* @ref k_thread_stack_alloc. If this is the case, then the stack should
393-
* be freed using @ref k_thread_stack_free after joining the thread.
396+
* @ref k_thread_stack_alloc.
394397
*
395398
* The stack_size parameter has constraints. It must either be:
396399
*

0 commit comments

Comments
 (0)