Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_thread.c @ 691a0632

History | View | Annotate | Download (3.778 KB)

1 e545e620 Thomas Schöpping
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3 84f0ce9e Thomas Schöpping
Copyright (C) 2016..2019  Thomas Schöpping et al.
4 e545e620 Thomas Schöpping

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 53710ca3 Marc Rothmann
/**
20
 * @file    aos_thread.c
21
 * @brief   Thread code.
22
 *
23
 * @addtogroup aos_threads
24
 * @{
25
 */
26
27 3940ba8a Thomas Schöpping
#include <amiroos.h>
28
29 f3ac1c96 Thomas Schöpping
/******************************************************************************/
30
/* LOCAL DEFINITIONS                                                          */
31
/******************************************************************************/
32
33
/******************************************************************************/
34
/* EXPORTED VARIABLES                                                         */
35
/******************************************************************************/
36
37
/******************************************************************************/
38
/* LOCAL TYPES                                                                */
39
/******************************************************************************/
40
41
/******************************************************************************/
42
/* LOCAL VARIABLES                                                            */
43
/******************************************************************************/
44
45
/******************************************************************************/
46
/* LOCAL FUNCTIONS                                                            */
47
/******************************************************************************/
48
49
/******************************************************************************/
50
/* EXPORTED FUNCTIONS                                                         */
51
/******************************************************************************/
52 e545e620 Thomas Schöpping
53
/**
54
 * @brief   Lets the calling thread sleep until the specifide system uptime.
55
 *
56
 * @param[in] t     Deadline until the thread will sleep.
57
 */
58
void aosThdSleepUntilS(const aos_timestamp_t* t)
59
{
60
  aosDbgCheck(t != NULL);
61
62
  aos_timestamp_t uptime;
63
64
  // get the current system uptime
65
  aosSysGetUptimeX(&uptime);
66
67
  // while the remaining time is too long, it must be split into multiple sleeps
68 512abac1 Thomas Schöpping
  while ( (*t > uptime) && ((*t - uptime) > AOS_THD_MAX_SLEEP_US) ) {
69 1e5f7648 Thomas Schöpping
    chThdSleepS(chTimeUS2I(AOS_THD_MAX_SLEEP_US));
70 e545e620 Thomas Schöpping
    aosSysGetUptimeX(&uptime);
71
  }
72
73
  // sleep the remaining time
74
  if (*t > uptime) {
75 1e5f7648 Thomas Schöpping
    sysinterval_t rest = chTimeUS2I(*t - uptime);
76 e545e620 Thomas Schöpping
    if (rest > TIME_IMMEDIATE) {
77
      chThdSleepS(rest);
78
    }
79
  }
80
81
  return;
82
}
83 53710ca3 Marc Rothmann
84 5c9e9b9d Thomas Schöpping
#if ((AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE)) || defined(__DOXYGEN__)
85 aed3754b Thomas Schöpping
/**
86
 * @brief   Calculate the peak stack utilization for a specific thread so far in bytes.
87
 *
88
 * @param[in] thread    Thread to calculate the stack utilization for.
89
 *
90
 * @return  Absolute peak stack utilization in bytes.
91
 */
92
size_t aosThdGetStackPeakUtilization(thread_t* thread)
93
{
94
  aosDbgCheck(thread != NULL);
95
96
  size_t util;
97
  uint8_t* ptr = (uint8_t*)thread->wabase;
98
99
  chSysLock();
100
  while (*ptr == CH_DBG_STACK_FILL_VALUE && ptr < (uint8_t*)thread->wabase + aosThdGetStacksize(thread)) {
101
    ++ptr;
102
  }
103
  util = aosThdGetStacksize(thread) - (ptr - (uint8_t*)thread->wabase);
104
  chSysUnlock();
105
106
  return util;
107
}
108 5c9e9b9d Thomas Schöpping
#endif /* (AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE) */
109 aed3754b Thomas Schöpping
110 53710ca3 Marc Rothmann
/** @} */