µ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-sched.h
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-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#ifndef CMSIS_PLUS_RTOS_OS_SCHED_H_
14#define CMSIS_PLUS_RTOS_OS_SCHED_H_
15
16// ----------------------------------------------------------------------------
17
18#if defined(__cplusplus)
19
20// ----------------------------------------------------------------------------
21
22#if defined(OS_USE_OS_APP_CONFIG_H)
23#include <cmsis-plus/os-app-config.h>
24#endif
25
28
29// ----------------------------------------------------------------------------
30
31#pragma GCC diagnostic push
32#if defined(__clang__)
33#pragma clang diagnostic ignored "-Wc++98-compat"
34#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
35#endif
36
37// ----------------------------------------------------------------------------
38
39namespace os
40{
41 namespace rtos
42 {
43 namespace scheduler
44 {
45
53 extern bool is_started_;
54
55#if !defined(OS_USE_RTOS_PORT_SCHEDULER)
56 extern bool is_preemptive_;
57 extern thread* volatile current_thread_;
58 extern internal::ready_threads_list ready_threads_list_;
59#endif /* !defined(OS_USE_RTOS_PORT_SCHEDULER) */
60
61 extern internal::terminated_threads_list terminated_threads_list_;
62
75 initialize (void);
76
84 [[noreturn]] void
85 start (void);
86
94 bool
95 started (void);
96
103 state_t
104 lock (void);
105
112 state_t
113 unlock (void);
114
120 state_t
121 locked (state_t state);
122
130 bool
131 locked (void);
132
140 bool
141 preemptive (void);
142
148 bool
149 preemptive (bool state);
150
151 // ----------------------------------------------------------------------
152
157 void
158 internal_switch_threads (void);
159
164 // ======================================================================
170 {
171 public:
172
184
189 // The rule of five.
190 critical_section (const critical_section&) = delete;
193 operator= (const critical_section&) = delete;
195 operator= (critical_section&&) = delete;
196
205
210 protected:
211
224 const state_t state_;
225
233 };
234
235 // ======================================================================
236
242 {
243 public:
244
256
261 // The rule of five.
262 uncritical_section (const uncritical_section&) = delete;
265 operator= (const uncritical_section&) = delete;
267 operator= (uncritical_section&&) = delete;
268
277
282 protected:
283
296 const state_t state_;
297
305 };
306
307 // ======================================================================
308
314 {
315 public:
316
327 constexpr
328 lockable ();
329
334 // The rule of five.
335 lockable (const lockable&) = delete;
336 lockable (lockable&&) = delete;
337 lockable&
338 operator= (const lockable&) = delete;
339 lockable&
340 operator= (lockable&&) = delete;
341
349 ~lockable ();
350
355 public:
356
369 void
370 lock (void);
371
378 bool
379 try_lock (void);
380
388 void
389 unlock (void);
390
395 protected:
396
409 state_t state_ = 0;
410
418 };
419
420 // ----------------------------------------------------------------------
421
425 namespace statistics
426 {
427#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES)
428
434 context_switches (void);
435
440 extern rtos::statistics::counter_t context_switches_;
441
446#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) */
447
448#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
449
456 cpu_cycles (void);
457
462 extern clock::timestamp_t switch_timestamp_;
463 extern rtos::statistics::duration_t cpu_cycles_;
464
469#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
470
471 } /* namespace statistics */
472 } /* namespace scheduler */
473
474 namespace interrupts
475 {
483 bool
484 in_handler_mode (void);
485
486 // ======================================================================
487
488 // TODO: define all levels of critical sections
489 // (kernel, real-time(level), complete)
490
491 // TODO: make template, parameter IRQ level
492
498 {
499 public:
500
512
517 // The rule of five.
518 critical_section (const critical_section&) = delete;
521 operator= (const critical_section&) = delete;
523 operator= (critical_section&&) = delete;
524
533
538 public:
539
551 static state_t
552 enter (void);
553
560 static void
561 exit (state_t state);
562
567 protected:
568
581 const state_t state_;
582
590 };
591
592 // ======================================================================
593
599 {
600 public:
601
613
618 // The rule of five.
619 uncritical_section (const uncritical_section&) = delete;
622 operator= (const uncritical_section&) = delete;
624 operator= (uncritical_section&&) = delete;
625
634
639 public:
640
652 static state_t
653 enter (void);
654
661 static void
662 exit (state_t state);
663
668 protected:
669
682 const state_t state_;
683
691 };
692
693 // ======================================================================
694
700 {
701 public:
702
713 constexpr
714 lockable ();
715
719 ~lockable ();
720
725 // The rule of five.
726 lockable (const lockable&) = delete;
727 lockable (lockable&&) = delete;
728 lockable&
729 operator= (const lockable&) = delete;
730 lockable&
731 operator= (lockable&&) = delete;
732
741 public:
742
755 void
756 lock (void);
757
764 bool
765 try_lock (void);
766
774 void
775 unlock (void);
776
781 protected:
782
795 state_t state_;
796
805 };
806
807 } /* namespace interrupts */
808 } /* namespace rtos */
809} /* namespace os */
810
811// ===== Inline & template implementations ====================================
812
813namespace os
814{
815 namespace rtos
816 {
817 namespace scheduler
818 {
826 inline bool
827 started (void)
828 {
829 return is_started_;
830 }
831
838 inline bool
840 {
841#if !defined(OS_USE_RTOS_PORT_SCHEDULER)
842 return is_preemptive_;
843#else
845#endif
846 }
847
855 inline bool
856 locked (void)
857 {
858 return port::scheduler::locked ();
859 }
860
868 inline state_t
869 lock (void)
870 {
871 return port::scheduler::lock ();
872 }
873
881 inline state_t
882 unlock (void)
883 {
884 return port::scheduler::unlock ();
885 }
886
898 inline state_t
900 {
901 return port::scheduler::locked (state);
902 }
903
910 inline
912 state_ (lock ())
913 {
914#if defined(OS_TRACE_RTOS_SCHEDULER)
915 trace::printf ("{C ");
916#endif
917 }
918
926 inline
928 {
929#if defined(OS_TRACE_RTOS_SCHEDULER)
930 trace::printf (" C}");
931#endif
932 locked (state_);
933 }
934
941 inline
943 state_ (unlock ())
944 {
945#if defined(OS_TRACE_RTOS_SCHEDULER)
946 trace::printf ("{U ");
947#endif
948 }
949
957 inline
959 {
960#if defined(OS_TRACE_RTOS_SCHEDULER)
961 trace::printf (" U}");
962#endif
963 locked (state_);
964 }
965
969 constexpr
971 state_ (port::scheduler::state::init)
972 {
973 }
974
978 inline
980 {
981 }
982
986 inline void
988 {
989 state_ = scheduler::lock ();
990 }
991
999 inline bool
1001 {
1002 state_ = scheduler::lock ();
1003 return true;
1004 }
1005
1009 inline void
1011 {
1012 scheduler::locked (state_);
1013 }
1014
1015 namespace statistics
1016 {
1017#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES)
1018
1035 {
1036 return context_switches_;
1037 }
1038
1039#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) */
1040
1041#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
1042
1060 {
1061 return cpu_cycles_;
1062 }
1063
1064#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
1065
1066#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) \
1067 || defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
1068
1081 inline void
1082 clear (void) {
1083#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES)
1084 context_switches_ = 0;
1085#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) */
1086
1087#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
1088 cpu_cycles_ = 0;
1089#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
1090 }
1091
1092#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) \
1093 || defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
1094
1095 } /* namespace statistics */
1096
1097 } /* namespace scheduler */
1098
1099 // ========================================================================
1100
1101 namespace interrupts
1102 {
1106 inline bool
1107 __attribute__((always_inline))
1109 {
1111 }
1112
1116 inline
1117 __attribute__((always_inline))
1119 state_ (enter ())
1120 {
1121 }
1122
1126 inline
1127 __attribute__((always_inline))
1129 {
1130 exit (state_);
1131 }
1132
1136 inline state_t
1137 __attribute__((always_inline))
1138 critical_section::enter (void)
1139 {
1141 }
1142
1146 inline void
1147 __attribute__((always_inline))
1149 {
1151 }
1152
1153 // ======================================================================
1154
1158 inline
1159 __attribute__((always_inline))
1161 state_ (enter ())
1162 {
1163 }
1164
1168 inline
1169 __attribute__((always_inline))
1171 {
1172 exit (state_);
1173 }
1174
1178 inline state_t
1179 __attribute__((always_inline))
1181 {
1183 }
1184
1188 inline void
1189 __attribute__((always_inline))
1191 {
1193 }
1194
1195 // ======================================================================
1196
1200 constexpr
1202 state_ (port::interrupts::state::init)
1203 {
1204 }
1205
1209 inline
1210 __attribute__((always_inline))
1212 {
1213 }
1214
1218 inline void
1219 __attribute__((always_inline))
1220 lockable::lock (void)
1221 {
1222 state_ = critical_section::enter ();
1223 }
1224
1232 inline bool
1233 __attribute__((always_inline))
1234 lockable::try_lock (void)
1235 {
1236 state_ = critical_section::enter ();
1237 return true;
1238 }
1239
1243 inline void
1244 __attribute__((always_inline))
1245 lockable::unlock (void)
1246 {
1247 critical_section::exit (state_);
1248 }
1249
1250 // ========================================================================
1251 }
1252
1253 } /* namespace rtos */
1254} /* namespace os */
1255
1256#pragma GCC diagnostic pop
1257
1258// ----------------------------------------------------------------------------
1259
1260#endif /* __cplusplus */
1261
1262// ----------------------------------------------------------------------------
1263
1264#endif /* CMSIS_PLUS_RTOS_OS_SCHED_H_ */
Interrupts critical section RAII helper.
Definition os-sched.h:498
critical_section()
Enter an interrupts critical section.
Definition os-sched.h:1118
static void exit(state_t state)
Exit the interrupts critical section.
Definition os-sched.h:1148
~critical_section()
Exit the interrupts critical section.
Definition os-sched.h:1128
static state_t enter(void)
Enter an interrupts critical section.
Definition os-sched.h:1138
Interrupts standard locker.
Definition os-sched.h:700
void lock(void)
Lock the interrupts.
Definition os-sched.h:1220
~lockable()
Destruct the interrupts lock.
Definition os-sched.h:1211
bool try_lock(void)
Try to lock the interrupts.
Definition os-sched.h:1234
void unlock(void)
Unlock the interrupts.
Definition os-sched.h:1245
constexpr lockable()
Construct an interrupts lock.
Definition os-sched.h:1201
Interrupts critical section RAII helper.
Definition os-sched.h:599
~uncritical_section()
Exit the interrupts uncritical section.
Definition os-sched.h:1170
static state_t enter(void)
Enter interrupts uncritical section.
Definition os-sched.h:1180
static void exit(state_t state)
Exit interrupts uncritical section.
Definition os-sched.h:1190
uncritical_section()
Enter an interrupts uncritical section.
Definition os-sched.h:1160
static rtos::interrupts::state_t enter(void)
static void exit(rtos::interrupts::state_t state)
static void exit(rtos::interrupts::state_t state)
static rtos::interrupts::state_t enter(void)
Scheduler critical section RAII helper.
Definition os-sched.h:170
critical_section()
Enter a critical section.
Definition os-sched.h:911
~critical_section()
Exit a critical section.
Definition os-sched.h:927
Scheduler standard locker.
Definition os-sched.h:314
void unlock(void)
Unlock the scheduler.
Definition os-sched.h:1010
constexpr lockable()
Construct a lockable object instance.
Definition os-sched.h:970
void lock(void)
Lock the scheduler.
Definition os-sched.h:987
~lockable()
Destruct the lockable object instance.
Definition os-sched.h:979
bool try_lock(void)
Try to lock the scheduler.
Definition os-sched.h:1000
Scheduler uncritical section RAII helper.
Definition os-sched.h:242
uncritical_section()
Enter a critical section.
Definition os-sched.h:942
~uncritical_section()
Exit a critical section.
Definition os-sched.h:958
Standard thread.
void exit(int code)
Definition exit.c:73
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
port::clock::timestamp_t timestamp_t
Type of variables holding clock time stamps.
Definition os-clocks.h:85
port::interrupts::state_t state_t
Type of variables holding interrupts statu codes.
Definition os-decls.h:243
bool in_handler_mode(void)
Check if the CPU is in handler mode.
Definition os-sched.h:1108
port::scheduler::state_t unlock(void)
port::scheduler::state_t lock(void)
rtos::statistics::duration_t cpu_cycles(void)
Get the total duration of all threads.
Definition os-sched.h:1059
rtos::statistics::counter_t context_switches(void)
Get the total number of context switches.
Definition os-sched.h:1034
port::scheduler::state_t state_t
Type of variables holding scheduler state codes.
Definition os-decls.h:202
state_t unlock(void)
Unlock the scheduler.
Definition os-sched.h:882
void start(void)
Start the RTOS scheduler.
Definition os-core.cpp:189
bool started(void)
Check if the scheduler was started.
Definition os-sched.h:827
state_t lock(void)
Lock the scheduler.
Definition os-sched.h:869
result_t initialize(void)
Initialise the RTOS scheduler.
Definition os-core.cpp:160
bool preemptive(void)
Check if the scheduler is in preemptive mode.
Definition os-sched.h:839
bool locked(void)
Check if the scheduler is locked.
Definition os-sched.h:856
uint64_t duration_t
Type of variables holding durations in CPU cycles.
Definition os-decls.h:222
uint64_t counter_t
Type of variables holding context switches counters.
Definition os-decls.h:217
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:96
System namespace.