Statistics
| Branch: | Tag: | Revision:

amiro-os / core / src / aos_thread.c @ 1ca1aee9

History | View | Annotate | Download (5.59 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  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 <amiroos.h>
28

    
29
/******************************************************************************/
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

    
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
  aos_timestamp_t uptime;
61

    
62
  // get the current system uptime
63
  aosSysGetUptimeX(&uptime);
64

    
65
  // while the remaining time is too long, it must be split into multiple sleeps
66
  while ( (t > uptime) && ((t - uptime) > AOS_THD_MAX_SLEEP_US) ) {
67
    chThdSleepS(chTimeUS2I(AOS_THD_MAX_SLEEP_US));
68
    aosSysGetUptimeX(&uptime);
69
  }
70

    
71
  // sleep the remaining time
72
  if (t > uptime) {
73
    sysinterval_t rest = chTimeUS2I(t - uptime);
74
    if (rest > TIME_IMMEDIATE) {
75
      chThdSleepS(rest);
76
    }
77
  }
78

    
79
  return;
80
}
81

    
82
#if ((AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE)) || defined(__DOXYGEN__)
83
/**
84
 * @brief   Calculate the peak stack utilization for a specific thread so far in bytes.
85
 *
86
 * @param[in] thread    Thread to calculate the stack utilization for.
87
 *
88
 * @return  Absolute peak stack utilization in bytes.
89
 */
90
size_t aosThdGetStackPeakUtilization(thread_t* thread)
91
{
92
  aosDbgCheck(thread != NULL);
93

    
94
  // iterator through the stack
95
  // note: since the stack is filled from top to bottom, the loop searches for the first irregular byte from the bottom (stack end).
96
  for (uint8_t* ptr = (uint8_t*)thread->wabase; ptr < (uint8_t*)thread->wabase + aosThdGetStacksize(thread); ++ptr) {
97
    if (*ptr != CH_DBG_STACK_FILL_VALUE) {
98
      return aosThdGetStacksize(thread) - (size_t)(--ptr - (uint8_t*)thread->wabase);
99
    }
100
  }
101

    
102
  return 0;
103
}
104
#endif /* (AMIROOS_CFG_DBG == true) && (CH_DBG_FILL_THREADS == TRUE) */
105

    
106
#if ((CH_CFG_USE_REGISTRY == TRUE) || (CH_CFG_USE_THREADHIERARCHY == TRUE)) || defined(__DOXYGEN__)
107

    
108
/**
109
 * @brief   Retrieve the first/root thread in the system.
110
 *
111
 * @return  Pointer to the first/root thread.
112
 */
113
thread_t* aosThreadGetFirst(void)
114
{
115
#if (CH_CFG_USE_REGISTRY == TRUE)
116
  return chRegFirstThread();
117
#elif (CH_CFG_USE_THREADHIERARCHY == TRUE)
118
  thread_t* tp = chThdGetSelfX();
119
  while (tp->parent) {
120
    tp = tp->parent;
121
  }
122
  return tp;
123
#endif
124
}
125

    
126
/**
127
 * @brief   Retrieve the next thread in the system.
128
 *
129
 * @param[in] thread  Current thread to retrieve the 'successor' to.
130
 *
131
 * @return  Pointer to the next thread, or NULL if there are no further threads.
132
 */
133
thread_t* aosThreadGetNext(thread_t* thread)
134
{
135
  aosDbgCheck(thread != NULL);
136

    
137
#if (CH_CFG_USE_REGISTRY == TRUE)
138

    
139
  return chRegNextThread(thread);
140

    
141
#elif (CH_CFG_USE_THREADHIERARCHY == TRUE)
142

    
143
#if (CH_CFG_NO_IDLE_THREAD == FALSE)
144
  // idle thread is last (see below)
145
  if (thread == chSysGetIdleThreadX()) {
146
    return NULL;
147
  }
148
#endif /* (CH_CFG_NO_IDLE_THREAD == FALSE) */
149

    
150
  // iterate through threads in a depth-first approach
151
  if (thread->children != NULL) {
152
    return thread->children;
153
  } else {
154
    while (thread != NULL && thread->sibling == NULL) {
155
      thread = thread->parent;
156
    }
157
#if (CH_CFG_NO_IDLE_THREAD == FALSE)
158
    // append idle thread explicitely as it is not part of the hierarchy tree
159
    return (thread != NULL) ? thread->sibling : chSysGetIdleThreadX();
160
#else /* (CH_CFG_NO_IDLE_THREAD == FALSE) */
161
    return (thread != NULL) ? thread->sibling : NULL;
162
#endif /* (CH_CFG_NO_IDLE_THREAD == FALSE) */
163
  }
164

    
165
#endif /* (CH_CFG_USE_THREADHIERARCHY == TRUE) */
166
}
167

    
168
#endif /* (CH_CFG_USE_REGISTRY == TRUE) || (CH_CFG_USE_THREADHIERARCHY == TRUE) */
169

    
170
/** @} */