@@ -189,8 +189,12 @@ require their regions to be of some power of two in size, and aligned to its
189
189
own size.
190
190
191
191
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 `.
194
198
195
199
Kernel-only Stacks
196
200
==================
@@ -411,8 +415,9 @@ Spawning a Thread
411
415
A thread is spawned by defining its stack area and its thread control block,
412
416
and then calling :c:func: `k_thread_create `.
413
417
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.
416
421
417
422
The size parameter for the stack must be one of three values:
418
423
@@ -426,6 +431,9 @@ The size parameter for the stack must be one of three values:
426
431
macros, the return value of :c:macro: `K_KERNEL_STACK_SIZEOF() ` for that
427
432
object.
428
433
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
+
429
437
The thread spawning function returns its thread id, which can be used
430
438
to reference the thread.
431
439
@@ -470,6 +478,25 @@ The following code has the same effect as the code segment above.
470
478
thread immediately. The corresponding parameter to :c:macro: `K_THREAD_DEFINE `
471
479
is a duration in integral milliseconds, so the equivalent argument is 0.
472
480
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
+
473
500
User Mode Constraints
474
501
---------------------
475
502
0 commit comments