ChibiOS 21.11.5
chdynamic.c
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file rt/src/chdynamic.c
21 * @brief Dynamic threads code.
22 *
23 * @addtogroup dynamic_threads
24 * @details Dynamic threads related APIs and services.
25 * @{
26 */
27
28#include "ch.h"
29
30#if (CH_CFG_USE_DYNAMIC == TRUE) || defined(__DOXYGEN__)
31
32/*===========================================================================*/
33/* Module local definitions. */
34/*===========================================================================*/
35
36/*===========================================================================*/
37/* Module exported variables. */
38/*===========================================================================*/
39
40/*===========================================================================*/
41/* Module local types. */
42/*===========================================================================*/
43
44/*===========================================================================*/
45/* Module local variables. */
46/*===========================================================================*/
47
48/*===========================================================================*/
49/* Module local functions. */
50/*===========================================================================*/
51
52/*===========================================================================*/
53/* Module exported functions. */
54/*===========================================================================*/
55
56#if (CH_CFG_USE_HEAP == TRUE) || defined(__DOXYGEN__)
57/**
58 * @brief Creates a new thread allocating the memory from the heap.
59 * @pre The configuration options @p CH_CFG_USE_DYNAMIC and
60 * @p CH_CFG_USE_HEAP must be enabled in order to use this function.
61 * @note A thread can terminate by calling @p chThdExit() or by simply
62 * returning from its main function.
63 * @note The allocated thread memory is not released automatically, it is
64 * caller responsibility to call @p chThdRelease() or @p chThdWait()
65 * in order to release the allocated memory.
66 *
67 * @param[in] heapp heap from which allocate the memory or @p NULL for the
68 * default heap
69 * @param[in] size size of the working area to be allocated
70 * @param[in] name thread name
71 * @param[in] prio the priority level for the new thread
72 * @param[in] pf the thread function
73 * @param[in] arg an argument passed to the thread function. It can be
74 * @p NULL.
75 * @return The pointer to the @p thread_t structure allocated for
76 * the thread into the working space area.
77 * @retval NULL if the memory cannot be allocated.
78 *
79 * @api
80 */
82 const char *name, tprio_t prio,
83 tfunc_t pf, void *arg) {
84 thread_t *tp;
85 void *wbase, *wend;
86
87 wbase = chHeapAllocAligned(heapp, size, PORT_WORKING_AREA_ALIGN);
88 if (wbase == NULL) {
89 return NULL;
90 }
91 wend = (void *)((uint8_t *)wbase + size);
92
93 thread_descriptor_t td = THD_DESCRIPTOR(name, wbase, wend, prio, pf, arg);
94
95#if CH_DBG_FILL_THREADS == TRUE
96 __thd_stackfill((uint8_t *)wbase, (uint8_t *)wend);
97#endif
98
99 chSysLock();
100 tp = chThdCreateSuspendedI(&td);
102 chSchWakeupS(tp, MSG_OK);
103 chSysUnlock();
104
105 return tp;
106}
107#endif /* CH_CFG_USE_HEAP == TRUE */
108
109#if (CH_CFG_USE_MEMPOOLS == TRUE) || defined(__DOXYGEN__)
110/**
111 * @brief Creates a new thread allocating the memory from the specified
112 * memory pool.
113 * @pre The configuration options @p CH_CFG_USE_DYNAMIC and
114 * @p CH_CFG_USE_MEMPOOLS must be enabled in order to use this
115 * function.
116 * @pre The pool must be initialized to contain only objects with
117 * alignment @p PORT_WORKING_AREA_ALIGN.
118 * @note A thread can terminate by calling @p chThdExit() or by simply
119 * returning from its main function.
120 * @note The allocated thread memory is not released automatically, it is
121 * caller responsibility to call @p chThdRelease() or @p chThdWait()
122 * in order to release the allocated memory.
123 *
124 * @param[in] mp pointer to the memory pool object
125 * @param[in] name thread name
126 * @param[in] prio the priority level for the new thread
127 * @param[in] pf the thread function
128 * @param[in] arg an argument passed to the thread function. It can be
129 * @p NULL.
130 * @return The pointer to the @p thread_t structure allocated for
131 * the thread into the working space area.
132 * @retval NULL if the memory pool is empty.
133 *
134 * @api
135 */
137 tprio_t prio, tfunc_t pf, void *arg) {
138 thread_t *tp;
139 void *wbase, *wend;
140
141 chDbgCheck(mp != NULL);
142
143 wbase = chPoolAlloc(mp);
144 if (wbase == NULL) {
145 return NULL;
146 }
147 wend = (void *)((uint8_t *)wbase + mp->object_size);
148
149 thread_descriptor_t td = THD_DESCRIPTOR(name, wbase, wend, prio, pf, arg);
150
151#if CH_DBG_FILL_THREADS == TRUE
152 __thd_stackfill((uint8_t *)wbase, (uint8_t *)wend);
153#endif
154
155 chSysLock();
156 tp = chThdCreateSuspendedI(&td);
158 tp->mpool = mp;
159 chSchWakeupS(tp, MSG_OK);
160 chSysUnlock();
161
162 return tp;
163}
164#endif /* CH_CFG_USE_MEMPOOLS == TRUE */
165
166#endif /* CH_CFG_USE_DYNAMIC == TRUE */
167
168/** @} */
#define chSysUnlock()
Leaves the kernel lock state.
#define chSchWakeupS(ntp, msg)
Wakes up a thread.
#define chSysLock()
Enters the kernel lock state.
#define chDbgCheck(c)
Function parameters check.
Definition chdebug.h:117
thread_t * chThdCreateFromHeap(memory_heap_t *heapp, size_t size, const char *name, tprio_t prio, tfunc_t pf, void *arg)
Creates a new thread allocating the memory from the heap.
Definition chdynamic.c:81
thread_t * chThdCreateFromMemoryPool(memory_pool_t *mp, const char *name, tprio_t prio, tfunc_t pf, void *arg)
Creates a new thread allocating the memory from the specified memory pool.
Definition chdynamic.c:136
uint32_t tprio_t
Definition chearly.h:86
struct ch_thread thread_t
Type of a thread structure.
Definition chearly.h:132
struct memory_heap memory_heap_t
Type of a memory heap.
Definition chmemheaps.h:71
void * chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align)
Allocates a block of memory from the heap by using the first-fit algorithm.
Definition chmemheaps.c:171
void * chPoolAlloc(memory_pool_t *mp)
Allocates an object from a memory pool.
Definition chmempools.c:160
#define PORT_WORKING_AREA_ALIGN
Working Areas alignment constant.
Definition chcore.h:61
#define MSG_OK
Normal wakeup message.
Definition chschd.h:38
#define CH_FLAG_MODE_MPOOL
Thread allocated from a Memory Pool.
Definition chschd.h:102
#define CH_FLAG_MODE_HEAP
Thread allocated from a Memory Heap.
Definition chschd.h:100
void(* tfunc_t)(void *p)
Thread function.
Definition chthreads.h:51
void __thd_stackfill(uint8_t *startp, uint8_t *endp)
Stack fill utility.
Definition chthreads.c:139
#define THD_DESCRIPTOR(name, wbase, wend, prio, funcp, arg)
Thread descriptor initializer with no affinity.
Definition chthreads.h:187
thread_t * chThdCreateSuspendedI(const thread_descriptor_t *tdp)
Creates a new thread.
Definition chthreads.c:170
void * mpool
Memory Pool where the thread workspace is returned.
Definition chobjects.h:340
tmode_t flags
Various thread flags.
Definition chobjects.h:207
Memory pool descriptor.
Definition chmempools.h:63
size_t object_size
Memory pool objects size.
Definition chmempools.h:65
Type of a thread descriptor.
Definition chthreads.h:56