ChibiOS  21.6.0
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 memory allocated for the thread is not released automatically,
65  * it is responsibility of the creator thread to call @p chThdWait()
66  * and then 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 *wsp;
87 
88  wsp = chHeapAllocAligned(heapp, size, PORT_WORKING_AREA_ALIGN);
89  if (wsp == NULL) {
90  return NULL;
91  }
92 
93  thread_descriptor_t td = THD_DESCRIPTOR(name, wsp,
94  (stkalign_t *)((uint8_t *)wsp + size),
95  prio, pf, arg);
96 
97 #if CH_DBG_FILL_THREADS == TRUE
98  __thd_memfill((uint8_t *)wsp,
99  (uint8_t *)wsp + size,
101 #endif
102 
103  chSysLock();
104  tp = chThdCreateSuspendedI(&td);
105  tp->flags = CH_FLAG_MODE_HEAP;
106  chSchWakeupS(tp, MSG_OK);
107  chSysUnlock();
108 
109  return tp;
110 }
111 #endif /* CH_CFG_USE_HEAP == TRUE */
112 
113 #if (CH_CFG_USE_MEMPOOLS == TRUE) || defined(__DOXYGEN__)
114 /**
115  * @brief Creates a new thread allocating the memory from the specified
116  * memory pool.
117  * @pre The configuration options @p CH_CFG_USE_DYNAMIC and
118  * @p CH_CFG_USE_MEMPOOLS must be enabled in order to use this
119  * function.
120  * @pre The pool must be initialized to contain only objects with
121  * alignment @p PORT_WORKING_AREA_ALIGN.
122  * @note A thread can terminate by calling @p chThdExit() or by simply
123  * returning from its main function.
124  * @note The memory allocated for the thread is not released automatically,
125  * it is responsibility of the creator thread to call @p chThdWait()
126  * and then release the allocated memory.
127  *
128  * @param[in] mp pointer to the memory pool object
129  * @param[in] name thread name
130  * @param[in] prio the priority level for the new thread
131  * @param[in] pf the thread function
132  * @param[in] arg an argument passed to the thread function. It can be
133  * @p NULL.
134  * @return The pointer to the @p thread_t structure allocated for
135  * the thread into the working space area.
136  * @retval NULL if the memory pool is empty.
137  *
138  * @api
139  */
141  tprio_t prio, tfunc_t pf, void *arg) {
142  thread_t *tp;
143  void *wsp;
144 
145  chDbgCheck(mp != NULL);
146 
147  wsp = chPoolAlloc(mp);
148  if (wsp == NULL) {
149  return NULL;
150  }
151 
152  thread_descriptor_t td = THD_DESCRIPTOR(name, wsp,
153  (stkalign_t *)((uint8_t *)wsp + mp->object_size),
154  prio, pf, arg);
155 
156 #if CH_DBG_FILL_THREADS == TRUE
157  __thd_memfill((uint8_t *)wsp,
158  (uint8_t *)wsp + mp->object_size,
160 #endif
161 
162  chSysLock();
163  tp = chThdCreateSuspendedI(&td);
165  tp->mpool = mp;
166  chSchWakeupS(tp, MSG_OK);
167  chSysUnlock();
168 
169  return tp;
170 }
171 #endif /* CH_CFG_USE_MEMPOOLS == TRUE */
172 
173 #endif /* CH_CFG_USE_DYNAMIC == TRUE */
174 
175 /** @} */
memory_pool_t
Memory pool descriptor.
Definition: chmempools.h:64
tprio_t
uint32_t tprio_t
Definition: chearly.h:87
stkalign_t
port_stkalign_t stkalign_t
Definition: chearly.h:80
chThdCreateFromMemoryPool
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:140
chThdCreateSuspendedI
thread_t * chThdCreateSuspendedI(const thread_descriptor_t *tdp)
Creates a new thread into a static memory area.
Definition: chthreads.c:172
tfunc_t
void(* tfunc_t)(void *p)
Thread function.
Definition: chthreads.h:52
CH_DBG_STACK_FILL_VALUE
#define CH_DBG_STACK_FILL_VALUE
Fill value for thread stack area in debug mode.
Definition: chdebug.h:47
chHeapAllocAligned
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
CH_FLAG_MODE_MPOOL
#define CH_FLAG_MODE_MPOOL
Thread allocated from a Memory Pool.
Definition: chschd.h:103
chDbgCheck
#define chDbgCheck(c)
Function parameters check.
Definition: chdebug.h:118
ch_thread
Structure representing a thread.
Definition: chobjects.h:156
CH_FLAG_MODE_HEAP
#define CH_FLAG_MODE_HEAP
Thread allocated from a Memory Heap.
Definition: chschd.h:101
__thd_memfill
void __thd_memfill(uint8_t *startp, uint8_t *endp, uint8_t v)
Memory fill utility.
Definition: chthreads.c:141
memory_heap
Structure describing a memory heap.
Definition: chmemheaps.h:96
thread_descriptor_t
Type of a thread descriptor.
Definition: chthreads.h:57
memory_pool_t::object_size
size_t object_size
Memory pool objects size.
Definition: chmempools.h:66
MSG_OK
#define MSG_OK
Normal wakeup message.
Definition: chschd.h:39
THD_DESCRIPTOR
#define THD_DESCRIPTOR(name, wbase, wend, prio, funcp, arg)
Thread descriptor initializer with no affinity.
Definition: chthreads.h:188
ch_thread::mpool
void * mpool
Memory Pool where the thread workspace is returned.
Definition: chobjects.h:331
PORT_WORKING_AREA_ALIGN
#define PORT_WORKING_AREA_ALIGN
Working Areas alignment constant.
Definition: chcore.h:62
chPoolAlloc
void * chPoolAlloc(memory_pool_t *mp)
Allocates an object from a memory pool.
Definition: chmempools.c:161
chThdCreateFromHeap
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
chSysUnlock
#define chSysUnlock()
Leaves the kernel lock state.
Definition: nil/include/ch.h:1053
ch_thread::flags
tmode_t flags
Various thread flags.
Definition: chobjects.h:210
chSchWakeupS
void chSchWakeupS(thread_t *ntp, msg_t msg)
Wakes up a thread.
Definition: chschd.c:393
chSysLock
#define chSysLock()
Enters the kernel lock state.
Definition: nil/include/ch.h:1043