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