ChibiOS/RT  5.1.0
chdynamic.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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; either version 3 of the License, or
9  (at your option) any later version.
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 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 = {
94  name,
95  wsp,
96  (stkalign_t *)((uint8_t *)wsp + size),
97  prio,
98  pf,
99  arg
100  };
101 
102 #if CH_DBG_FILL_THREADS == TRUE
103  _thread_memfill((uint8_t *)wsp,
104  (uint8_t *)wsp + size,
106 #endif
107 
108  chSysLock();
109  tp = chThdCreateSuspendedI(&td);
110  tp->flags = CH_FLAG_MODE_HEAP;
111  chSchWakeupS(tp, MSG_OK);
112  chSysUnlock();
113 
114  return tp;
115 }
116 #endif /* CH_CFG_USE_HEAP == TRUE */
117 
118 #if (CH_CFG_USE_MEMPOOLS == TRUE) || defined(__DOXYGEN__)
119 /**
120  * @brief Creates a new thread allocating the memory from the specified
121  * memory pool.
122  * @pre The configuration options @p CH_CFG_USE_DYNAMIC and
123  * @p CH_CFG_USE_MEMPOOLS must be enabled in order to use this
124  * function.
125  * @pre The pool must be initialized to contain only objects with
126  * alignment @p PORT_WORKING_AREA_ALIGN.
127  * @note A thread can terminate by calling @p chThdExit() or by simply
128  * returning from its main function.
129  * @note The memory allocated for the thread is not released automatically,
130  * it is responsibility of the creator thread to call @p chThdWait()
131  * and then release the allocated memory.
132  *
133  * @param[in] mp pointer to the memory pool object
134  * @param[in] name thread name
135  * @param[in] prio the priority level for the new thread
136  * @param[in] pf the thread function
137  * @param[in] arg an argument passed to the thread function. It can be
138  * @p NULL.
139  * @return The pointer to the @p thread_t structure allocated for
140  * the thread into the working space area.
141  * @retval NULL if the memory pool is empty.
142  *
143  * @api
144  */
146  tprio_t prio, tfunc_t pf, void *arg) {
147  thread_t *tp;
148  void *wsp;
149 
150  chDbgCheck(mp != NULL);
151 
152  wsp = chPoolAlloc(mp);
153  if (wsp == NULL) {
154  return NULL;
155  }
156 
157  thread_descriptor_t td = {
158  name,
159  wsp,
160  (stkalign_t *)((uint8_t *)wsp + mp->object_size),
161  prio,
162  pf,
163  arg
164  };
165 
166 #if CH_DBG_FILL_THREADS == TRUE
167  _thread_memfill((uint8_t *)wsp,
168  (uint8_t *)wsp + mp->object_size,
170 #endif
171 
172  chSysLock();
173  tp = chThdCreateSuspendedI(&td);
175  tp->mpool = mp;
176  chSchWakeupS(tp, MSG_OK);
177  chSysUnlock();
178 
179  return tp;
180 }
181 #endif /* CH_CFG_USE_MEMPOOLS == TRUE */
182 
183 #endif /* CH_CFG_USE_DYNAMIC == TRUE */
184 
185 /** @} */
Memory pool descriptor.
Definition: chmempools.h:68
static void chSysLock(void)
Enters the kernel lock state.
Definition: chsys.h:353
thread_t * chThdCreateSuspendedI(const thread_descriptor_t *tdp)
Creates a new thread into a static memory area.
Definition: chthreads.c:167
Type of a thread descriptor.
Definition: chthreads.h:57
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
void(* tfunc_t)(void *p)
Thread function.
Definition: chthreads.h:52
static void chSysUnlock(void)
Leaves the kernel lock state.
Definition: chsys.h:365
#define CH_FLAG_MODE_HEAP
Thread allocated from a Memory Heap.
Definition: chschd.h:109
void _thread_memfill(uint8_t *startp, uint8_t *endp, uint8_t v)
Memory fill utility.
Definition: chthreads.c:136
size_t object_size
Memory pool objects size.
Definition: chmempools.h:70
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:145
void * chPoolAlloc(memory_pool_t *mp)
Allocates an object from a memory pool.
Definition: chmempools.c:155
#define chDbgCheck(c)
Function parameters check.
Definition: chdebug.h:101
#define CH_FLAG_MODE_MPOOL
Thread allocated from a Memory Pool.
Definition: chschd.h:112
#define MSG_OK
Normal wakeup message.
Definition: chschd.h:39
void * mpool
Memory Pool where the thread workspace is returned.
Definition: chschd.h:305
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: chheap.c:172
void chSchWakeupS(thread_t *ntp, msg_t msg)
Wakes up a thread.
Definition: chschd.c:412
ChibiOS/RT main include file.
tmode_t flags
Various thread flags.
Definition: chschd.h:184
Structure describing a memory heap.
Definition: chheap.h:100
#define CH_DBG_STACK_FILL_VALUE
Fill value for thread stack area in debug mode.
Definition: chdebug.h:47
Structure representing a thread.
Definition: chschd.h:153