µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os-idle.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <cmsis-plus/rtos/os.h>
29
30// ----------------------------------------------------------------------------
31
32#if defined(__clang__)
33#pragma clang diagnostic ignored "-Wc++98-compat"
34#endif
35
36// ----------------------------------------------------------------------------
37
38using namespace os;
39using namespace os::rtos;
40
41// ----------------------------------------------------------------------------
42
43void*
45
46void
48
67bool
68__attribute__((weak))
70{
71 return false;
72}
73
74void
75__attribute__((weak))
77{
78 while (!scheduler::terminated_threads_list_.empty ())
79 {
81 {
82 // ----- Enter critical section ---------------------------------
84 node =
85 const_cast<internal::waiting_thread_node*> (scheduler::terminated_threads_list_.head ());
86 node->unlink ();
87 // ----- Exit critical section ----------------------------------
88 }
89 node->thread_->internal_destroy_ ();
90
92 }
93
94#if defined(OS_HAS_INTERRUPTS_STACK)
95 // Simple test to verify that the interrupts
96 // did not underflow the stack.
97 assert(rtos::interrupts::stack ()->check_bottom_magic ());
98#endif
99
101 {
102 port::scheduler::wait_for_interrupt ();
103 }
104}
105
106#if !defined(OS_USE_RTOS_PORT_SCHEDULER) || defined(__DOXYGEN__)
107
111extern thread* os_idle_thread;
112
113thread* os_idle_thread;
114
115#pragma GCC diagnostic push
116#if defined(__clang__)
117#pragma clang diagnostic ignored "-Wexit-time-destructors"
118#pragma clang diagnostic ignored "-Wglobal-constructors"
119#pragma clang diagnostic ignored "-Wmissing-variable-declarations"
120#endif
121
122#if defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
123
125 { "idle", os_idle, nullptr};
126
127#else
128
129static std::unique_ptr<thread> os_idle_thread_;
130
131#endif /* defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
132
133#pragma GCC diagnostic pop
134
135void
136__attribute__((weak))
138{
139#if defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS)
140
141 // The thread object instance was created by the static constructors.
142 os_idle_thread = &os_idle_thread_;
143
144#else
145
146 thread::attributes attr = thread::initializer;
148
149 // No need for an explicit delete, it is deallocated by the unique_ptr.
150 os_idle_thread_ = std::unique_ptr<thread> (
151 new thread ("idle", os_idle, nullptr, attr));
152
153 os_idle_thread = os_idle_thread_.get ();
154
155#endif /* defined(OS_EXCLUDE_DYNAMIC_MEMORY_ALLOCATIONS) */
156}
157
158void*
159os_idle (thread::func_args_t args __attribute__((unused)))
160{
161
162 // The thread was created with the default priority, and the
163 // idle thread must run with th lowest possible priority.
164
165#if defined(OS_BOOL_RTOS_THREAD_IDLE_PRIORITY_BELOW_IDLE)
166 // The CMSIS RTOS validator creates threads with `priority::idle`,
167 // so, to be sure that the system idle thread has the lowest priority,
168 // go one step below the idle priority.
169 this_thread::thread ().priority (thread::priority::idle - 1);
170#else
171 this_thread::thread ().priority (thread::priority::idle);
172#endif
173
174 while (true)
175 {
177
178 // Possibly switch to threads that were resumed during sleep.
180 }
181}
182
186#endif /* !defined(OS_USE_RTOS_PORT_SCHEDULER) */
187
188// ----------------------------------------------------------------------------
Double linked list node, with thread reference.
Definition os-lists.h:72
rtos::thread * thread_
Pointer to waiting thread.
Definition os-lists.h:120
Interrupts critical section RAII helper.
Definition os-sched.h:524
Thread attributes.
Definition os-thread.h:791
std::size_t th_stack_size_bytes
Size of the user defined storage for the thread stack, in bytes.
Definition os-thread.h:851
Template of a POSIX compliant thread with local stack.
Definition os-thread.h:1816
_func_args_t func_args_t
Type of thread function arguments.
Definition os-thread.h:409
Standard thread.
#define OS_INTEGER_RTOS_IDLE_STACK_SIZE_BYTES
Define the idle thread stack size.
void os_startup_create_thread_idle(void)
Create the idle thread.
bool os_rtos_idle_enter_power_saving_mode_hook(void)
Hook to enter a power saving mode.
Definition os-idle.cpp:69
class thread::stack * stack(void)
Get the interrupts stack.
Definition os-core.cpp:564
RTOS namespace.
Definition os-flags.h:54
System namespace.
void yield() noexcept
Yield the CPU to the next ready thread.
void os_rtos_idle_actions(void)
Definition os-idle.cpp:76
void * os_idle(thread::func_args_t args)
Single file µOS++ RTOS definitions.