amiro-os / core / src / aos_thread.c @ f3ac1c96
History | View | Annotate | Download (3.78 KB)
| 1 |
/*
|
|---|---|
| 2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
| 3 |
Copyright (C) 2016..2019 Thomas Schöpping et al.
|
| 4 |
|
| 5 |
This program is free software: you can redistribute it and/or modify
|
| 6 |
it under the terms of the GNU General Public License as published by
|
| 7 |
the Free Software Foundation, either version 3 of the License, or
|
| 8 |
(at your option) any later version.
|
| 9 |
|
| 10 |
This program 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 aos_thread.c
|
| 21 |
* @brief Thread code.
|
| 22 |
*
|
| 23 |
* @addtogroup aos_threads
|
| 24 |
* @{
|
| 25 |
*/
|
| 26 |
|
| 27 |
#include <aos_thread.h> |
| 28 |
/******************************************************************************/
|
| 29 |
/* LOCAL DEFINITIONS */
|
| 30 |
/******************************************************************************/
|
| 31 |
|
| 32 |
/******************************************************************************/
|
| 33 |
/* EXPORTED VARIABLES */
|
| 34 |
/******************************************************************************/
|
| 35 |
|
| 36 |
/******************************************************************************/
|
| 37 |
/* LOCAL TYPES */
|
| 38 |
/******************************************************************************/
|
| 39 |
|
| 40 |
/******************************************************************************/
|
| 41 |
/* LOCAL VARIABLES */
|
| 42 |
/******************************************************************************/
|
| 43 |
|
| 44 |
/******************************************************************************/
|
| 45 |
/* LOCAL FUNCTIONS */
|
| 46 |
/******************************************************************************/
|
| 47 |
|
| 48 |
/******************************************************************************/
|
| 49 |
/* EXPORTED FUNCTIONS */
|
| 50 |
/******************************************************************************/
|
| 51 |
|
| 52 |
/**
|
| 53 |
* @brief Lets the calling thread sleep until the specifide system uptime.
|
| 54 |
*
|
| 55 |
* @param[in] t Deadline until the thread will sleep.
|
| 56 |
*/
|
| 57 |
void aosThdSleepUntilS(const aos_timestamp_t* t) |
| 58 |
{
|
| 59 |
aosDbgCheck(t != NULL);
|
| 60 |
|
| 61 |
aos_timestamp_t uptime; |
| 62 |
|
| 63 |
// get the current system uptime
|
| 64 |
aosSysGetUptimeX(&uptime); |
| 65 |
|
| 66 |
// while the remaining time is too long, it must be split into multiple sleeps
|
| 67 |
while ( (*t > uptime) && ((*t - uptime) > AOS_THD_MAX_SLEEP_US) ) {
|
| 68 |
chThdSleepS(chTimeUS2I(AOS_THD_MAX_SLEEP_US)); |
| 69 |
aosSysGetUptimeX(&uptime); |
| 70 |
} |
| 71 |
|
| 72 |
// sleep the remaining time
|
| 73 |
if (*t > uptime) {
|
| 74 |
sysinterval_t rest = chTimeUS2I(*t - uptime); |
| 75 |
if (rest > TIME_IMMEDIATE) {
|
| 76 |
chThdSleepS(rest); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return;
|
| 81 |
} |
| 82 |
|
| 83 |
#if ((AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE)) || defined(__DOXYGEN__) |
| 84 |
/**
|
| 85 |
* @brief Calculate the peak stack utilization for a specific thread so far in bytes.
|
| 86 |
*
|
| 87 |
* @param[in] thread Thread to calculate the stack utilization for.
|
| 88 |
*
|
| 89 |
* @return Absolute peak stack utilization in bytes.
|
| 90 |
*/
|
| 91 |
size_t aosThdGetStackPeakUtilization(thread_t* thread) |
| 92 |
{
|
| 93 |
aosDbgCheck(thread != NULL);
|
| 94 |
|
| 95 |
size_t util; |
| 96 |
uint8_t* ptr = (uint8_t*)thread->wabase; |
| 97 |
|
| 98 |
chSysLock(); |
| 99 |
while (*ptr == CH_DBG_STACK_FILL_VALUE && ptr < (uint8_t*)thread->wabase + aosThdGetStacksize(thread)) {
|
| 100 |
++ptr; |
| 101 |
} |
| 102 |
util = aosThdGetStacksize(thread) - (ptr - (uint8_t*)thread->wabase); |
| 103 |
chSysUnlock(); |
| 104 |
|
| 105 |
return util;
|
| 106 |
} |
| 107 |
#endif /* (AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE) */ |
| 108 |
|
| 109 |
/** @} */
|