µ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++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2016-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#ifndef CMSIS_PLUS_RTOS_OS_SCHED_H_
13#define CMSIS_PLUS_RTOS_OS_SCHED_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(__cplusplus)
18
19// ----------------------------------------------------------------------------
20
21#if defined(OS_USE_OS_APP_CONFIG_H)
22#include <cmsis-plus/os-app-config.h>
23#endif
24
27
28// ----------------------------------------------------------------------------
29
30#pragma GCC diagnostic push
31#if defined(__clang__)
32#pragma clang diagnostic ignored "-Wc++98-compat"
33#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
34#endif
35
36// ----------------------------------------------------------------------------
37
38namespace os
39{
40 namespace rtos
41 {
42 namespace scheduler
43 {
44
52 extern bool is_started_;
53
54#if !defined(OS_USE_RTOS_PORT_SCHEDULER)
55 extern bool is_preemptive_;
56 extern thread* volatile current_thread_;
57 extern internal::ready_threads_list ready_threads_list_;
58#endif /* !defined(OS_USE_RTOS_PORT_SCHEDULER) */
59
60 extern internal::terminated_threads_list terminated_threads_list_;
61
74 initialize (void);
75
83 [[noreturn]] void
84 start (void);
85
93 bool
94 started (void);
95
102 state_t
103 lock (void);
104
111 state_t
112 unlock (void);
113
119 state_t
120 locked (state_t state);
121
129 bool
130 locked (void);
131
139 bool
140 preemptive (void);
141
147 bool
148 preemptive (bool state);
149
150 // ----------------------------------------------------------------------
151
156 void
157 internal_switch_threads (void);
158
163 // ======================================================================
171 {
172 public:
184
189 // The rule of five.
190 critical_section (const critical_section&) = delete;
193 operator= (const critical_section&)
194 = delete;
196 operator= (critical_section&&)
197 = delete;
198
207
212 protected:
225 const state_t state_;
226
234 };
235
236 // ======================================================================
237
245 {
246 public:
258
263 // The rule of five.
264 uncritical_section (const uncritical_section&) = delete;
267 operator= (const uncritical_section&)
268 = delete;
270 operator= (uncritical_section&&)
271 = delete;
272
281
286 protected:
299 const state_t state_;
300
308 };
309
310 // ======================================================================
311
317 {
318 public:
329 constexpr lockable ();
330
335 // The rule of five.
336 lockable (const lockable&) = delete;
337 lockable (lockable&&) = delete;
338 lockable&
339 operator= (const lockable&)
340 = delete;
341 lockable&
342 operator= (lockable&&)
343 = delete;
344
352 ~lockable ();
353
358 public:
371 void
372 lock (void);
373
380 bool
381 try_lock (void);
382
390 void
391 unlock (void);
392
397 protected:
410 state_t state_ = 0;
411
419 };
420
421 // ----------------------------------------------------------------------
422
426 namespace statistics
427 {
428#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES)
429
436 context_switches (void);
437
442 extern rtos::statistics::counter_t context_switches_;
443
448#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) */
449
450#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
451
458 cpu_cycles (void);
459
464 extern clock::timestamp_t switch_timestamp_;
465 extern rtos::statistics::duration_t cpu_cycles_;
466
471#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
472
473 } /* namespace statistics */
474 } /* namespace scheduler */
475
476 namespace interrupts
477 {
485 bool
486 in_handler_mode (void);
487
488 // ======================================================================
489
490 // TODO: define all levels of critical sections
491 // (kernel, real-time(level), complete)
492
493 // TODO: make template, parameter IRQ level
494
502 {
503 public:
515
520 // The rule of five.
521 critical_section (const critical_section&) = delete;
524 operator= (const critical_section&)
525 = delete;
527 operator= (critical_section&&)
528 = delete;
529
538
543 public:
555 static state_t
556 enter (void);
557
565 static void
566 exit (state_t state);
567
572 protected:
585 const state_t state_;
586
594 };
595
596 // ======================================================================
597
605 {
606 public:
618
623 // The rule of five.
624 uncritical_section (const uncritical_section&) = delete;
627 operator= (const uncritical_section&)
628 = delete;
630 operator= (uncritical_section&&)
631 = delete;
632
641
646 public:
658 static state_t
659 enter (void);
660
667 static void
668 exit (state_t state);
669
674 protected:
687 const state_t state_;
688
696 };
697
698 // ======================================================================
699
705 {
706 public:
717 constexpr lockable ();
718
722 ~lockable ();
723
728 // The rule of five.
729 lockable (const lockable&) = delete;
730 lockable (lockable&&) = delete;
731 lockable&
732 operator= (const lockable&)
733 = delete;
734 lockable&
735 operator= (lockable&&)
736 = delete;
737
746 public:
759 void
760 lock (void);
761
768 bool
769 try_lock (void);
770
778 void
779 unlock (void);
780
785 protected:
798 state_t state_;
799
807 };
808
809 } /* namespace interrupts */
810 } /* namespace rtos */
811} /* namespace os */
812
813// ===== Inline & template implementations ====================================
814
815namespace os
816{
817 namespace rtos
818 {
819 namespace scheduler
820 {
828 inline bool
829 started (void)
830 {
831 return is_started_;
832 }
833
840 inline bool
842 {
843#if !defined(OS_USE_RTOS_PORT_SCHEDULER)
844 return is_preemptive_;
845#else
847#endif
848 }
849
857 inline bool
858 locked (void)
859 {
860 return port::scheduler::locked ();
861 }
862
870 inline state_t
871 lock (void)
872 {
873 return port::scheduler::lock ();
874 }
875
883 inline state_t
884 unlock (void)
885 {
886 return port::scheduler::unlock ();
887 }
888
900 inline state_t
902 {
903 return port::scheduler::locked (state);
904 }
905
913 {
914#if defined(OS_TRACE_RTOS_SCHEDULER)
915 trace::printf ("{C ");
916#endif
917 }
918
927 {
928#if defined(OS_TRACE_RTOS_SCHEDULER)
929 trace::printf (" C}");
930#endif
931 locked (state_);
932 }
933
941 {
942#if defined(OS_TRACE_RTOS_SCHEDULER)
943 trace::printf ("{U ");
944#endif
945 }
946
955 {
956#if defined(OS_TRACE_RTOS_SCHEDULER)
957 trace::printf (" U}");
958#endif
959 locked (state_);
960 }
961
965 constexpr lockable::lockable () : state_ (port::scheduler::state::init)
966 {
967 }
968
973 {
974 }
975
979 inline void
981 {
982 state_ = scheduler::lock ();
983 }
984
992 inline bool
994 {
995 state_ = scheduler::lock ();
996 return true;
997 }
998
1002 inline void
1004 {
1005 scheduler::locked (state_);
1006 }
1007
1008 namespace statistics
1009 {
1010#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES)
1011
1028 {
1029 return context_switches_;
1030 }
1031
1032#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) */
1033
1034#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
1035
1053 {
1054 return cpu_cycles_;
1055 }
1056
1057#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
1058
1059#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) \
1060 || defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
1061
1074 inline void
1075 clear (void)
1076 {
1077#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES)
1078 context_switches_ = 0;
1079#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) */
1080
1081#if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
1082 cpu_cycles_ = 0;
1083#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
1084 }
1085
1086#endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) \
1087 || defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
1088
1089 } /* namespace statistics */
1090
1091 } /* namespace scheduler */
1092
1093 // ========================================================================
1094
1095 namespace interrupts
1096 {
1100 inline bool __attribute__ ((always_inline))
1102 {
1104 }
1105
1109 inline __attribute__ ((always_inline))
1111 : state_ (enter ())
1112 {
1113 }
1114
1118 inline __attribute__ ((always_inline))
1120 {
1121 exit (state_);
1122 }
1123
1127 inline state_t __attribute__ ((always_inline))
1128 critical_section::enter (void)
1129 {
1131 }
1132
1136 inline void __attribute__ ((always_inline))
1138 {
1140 }
1141
1142 // ======================================================================
1143
1147 inline __attribute__ ((always_inline))
1149 : state_ (enter ())
1150 {
1151 }
1152
1156 inline __attribute__ ((always_inline))
1158 {
1159 exit (state_);
1160 }
1161
1165 inline state_t __attribute__ ((always_inline))
1167 {
1169 }
1170
1174 inline void __attribute__ ((always_inline))
1176 {
1178 }
1179
1180 // ======================================================================
1181
1185 constexpr lockable::lockable () : state_ (port::interrupts::state::init)
1186 {
1187 }
1188
1192 inline __attribute__ ((always_inline)) lockable::~lockable ()
1193 {
1194 }
1195
1199 inline void __attribute__ ((always_inline))
1200 lockable::lock (void)
1201 {
1202 state_ = critical_section::enter ();
1203 }
1204
1212 inline bool __attribute__ ((always_inline))
1213 lockable::try_lock (void)
1214 {
1215 state_ = critical_section::enter ();
1216 return true;
1217 }
1218
1222 inline void __attribute__ ((always_inline))
1223 lockable::unlock (void)
1224 {
1225 critical_section::exit (state_);
1226 }
1227
1228 // ======================================================================
1229 } // namespace interrupts
1230
1231 } /* namespace rtos */
1232} /* namespace os */
1233
1234#pragma GCC diagnostic pop
1235
1236// ----------------------------------------------------------------------------
1237
1238#endif /* __cplusplus */
1239
1240// ----------------------------------------------------------------------------
1241
1242#endif /* CMSIS_PLUS_RTOS_OS_SCHED_H_ */
Interrupts critical section RAII helper.
Definition os-sched.h:502
critical_section()
Enter an interrupts critical section.
Definition os-sched.h:1110
static void exit(state_t state)
Exit the interrupts critical section.
Definition os-sched.h:1137
~critical_section()
Exit the interrupts critical section.
Definition os-sched.h:1119
static state_t enter(void)
Enter an interrupts critical section.
Definition os-sched.h:1128
Interrupts standard locker.
Definition os-sched.h:705
void lock(void)
Lock the interrupts.
Definition os-sched.h:1200
~lockable()
Destruct the interrupts lock.
Definition os-sched.h:1192
bool try_lock(void)
Try to lock the interrupts.
Definition os-sched.h:1213
void unlock(void)
Unlock the interrupts.
Definition os-sched.h:1223
constexpr lockable()
Construct an interrupts lock.
Definition os-sched.h:1185
Interrupts critical section RAII helper.
Definition os-sched.h:605
~uncritical_section()
Exit the interrupts uncritical section.
Definition os-sched.h:1157
static state_t enter(void)
Enter interrupts uncritical section.
Definition os-sched.h:1166
static void exit(state_t state)
Exit interrupts uncritical section.
Definition os-sched.h:1175
uncritical_section()
Enter an interrupts uncritical section.
Definition os-sched.h:1148
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:171
critical_section()
Enter a critical section.
Definition os-sched.h:912
~critical_section()
Exit a critical section.
Definition os-sched.h:926
Scheduler standard locker.
Definition os-sched.h:317
void unlock(void)
Unlock the scheduler.
Definition os-sched.h:1003
constexpr lockable()
Construct a lockable object instance.
Definition os-sched.h:965
void lock(void)
Lock the scheduler.
Definition os-sched.h:980
~lockable()
Destruct the lockable object instance.
Definition os-sched.h:972
bool try_lock(void)
Try to lock the scheduler.
Definition os-sched.h:993
Scheduler uncritical section RAII helper.
Definition os-sched.h:245
uncritical_section()
Enter a critical section.
Definition os-sched.h:940
~uncritical_section()
Exit a critical section.
Definition os-sched.h:954
Standard thread.
void exit(int code)
Definition exit.c:70
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
port::clock::timestamp_t timestamp_t
Type of variables holding clock time stamps.
Definition os-clocks.h:88
port::interrupts::state_t state_t
Type of variables holding interrupts status codes.
Definition os-decls.h:242
bool in_handler_mode(void)
Check if the CPU is in handler mode.
Definition os-sched.h:1101
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:1052
rtos::statistics::counter_t context_switches(void)
Get the total number of context switches.
Definition os-sched.h:1027
port::scheduler::state_t state_t
Type of variables holding scheduler state codes.
Definition os-decls.h:200
state_t unlock(void)
Unlock the scheduler.
Definition os-sched.h:884
void start(void)
Start the RTOS scheduler.
Definition os-core.cpp:193
bool started(void)
Check if the scheduler was started.
Definition os-sched.h:829
state_t lock(void)
Lock the scheduler.
Definition os-sched.h:871
result_t initialize(void)
Initialise the RTOS scheduler.
Definition os-core.cpp:164
bool preemptive(void)
Check if the scheduler is in preemptive mode.
Definition os-sched.h:841
bool locked(void)
Check if the scheduler is locked.
Definition os-sched.h:858
uint64_t duration_t
Type of variables holding durations in CPU cycles.
Definition os-decls.h:220
uint64_t counter_t
Type of variables holding context switches counters.
Definition os-decls.h:215
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:95
System namespace.