µ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-decls.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_DECLS_H_
13#define CMSIS_PLUS_RTOS_OS_DECLS_H_
14
15// ----------------------------------------------------------------------------
16
18
19// Include the application specific definitions.
20#if defined(OS_USE_OS_APP_CONFIG_H)
21#include <cmsis-plus/os-app-config.h>
22#endif
23// Include the non-portable portable types, enums and constants declarations.
24#include <cmsis-plus/rtos/port/os-decls.h>
25
27
28// ----------------------------------------------------------------------------
29
30#if defined(__cplusplus)
31
32// ----------------------------------------------------------------------------
33
34#include <cstdint>
35#include <cstddef>
36#include <cerrno>
37#include <cstring>
38
39// ----------------------------------------------------------------------------
40
41#pragma GCC diagnostic push
42#if defined(__clang__)
43#pragma clang diagnostic ignored "-Wc++98-compat"
44#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
45#endif
46
47// ----------------------------------------------------------------------------
48
53namespace os
54{
58 namespace rtos
59 {
60 // ========================================================================
61
62 // Forward references.
63 class clock;
64 class clock_rtc;
65 class clock_systick;
66
67 class condition_variable;
68 class event_flags;
69 class memory_pool;
70 class message_queue;
71 class mutex;
72 class semaphore;
73 class thread;
74 class timer;
75
76 // ------------------------------------------------------------------------
77
78 namespace memory
79 {
80 template <typename T>
82 } /* namespace memory */
83
84 // ------------------------------------------------------------------------
85
95 using result_t = uint32_t;
96
159 namespace result
160 {
174 enum : result_t
175 {
179 ok = 0
180
181 };
182 } /* namespace result */
183
184 // ------------------------------------------------------------------------
185
190 namespace scheduler
191 {
200 using state_t = port::scheduler::state_t;
201
202 } /* namespace scheduler */
203
204 // ------------------------------------------------------------------------
205
210 namespace statistics
211 {
215 using counter_t = uint64_t;
216
220 using duration_t = uint64_t;
221
222 } /* namespace statistics */
223
224 // ------------------------------------------------------------------------
225
230 namespace interrupts
231 {
242 using state_t = port::interrupts::state_t;
243
244 } /* namespace interrupts */
245
246 // ------------------------------------------------------------------------
247
255 namespace flags
256 {
266 using mask_t = uint32_t;
267
277 using mode_t = uint32_t;
278
285 namespace mode
286 {
290 enum : mode_t
291 {
295 all = 1,
296
300 any = 2,
301
305 clear = 4
306 };
307 } /* namespace mode */
308
312 enum : mask_t
313 {
317 any = 0,
318
322 all = 0xFFFFFFFF
323 };
324
325 } /* namespace flags */
326
327 // ------------------------------------------------------------------------
328
333 namespace this_thread
334 {
335 }
336
337 // ------------------------------------------------------------------------
338
342 namespace internal
343 {
344
345 // ======================================================================
346
352 {
353 public:
362 object_named ();
363
369 object_named (const char* name);
370
374 object_named (const object_named&) = default;
375 object_named (object_named&&) = default;
376 // object_named&
377 // operator= (const object_named&) = default;
378 // object_named&
379 // operator= (object_named&&) = default;
387 ~object_named () = default;
388
393 public:
405 const char*
406 name (void) const;
407
412 protected:
425 const char* const name_ = "-";
426
434 };
435
436 // ======================================================================
437
443 {
444 public:
454
460 object_named_system (const char* name);
461
465 object_named_system (const object_named_system&) = default;
467 // object_named_system&
468 // operator= (const object_named_system&) = default;
469 // object_named_system&
470 // operator= (object_named_system&&) = default;
479
495 static void*
496 operator new (std::size_t bytes);
497
504 static void*
505 operator new[] (std::size_t bytes);
506
513 static void*
514 operator new (std::size_t bytes, void* ptr);
515
522 static void*
523 operator new[] (std::size_t bytes, void* ptr);
524
533 static void
534 operator delete (void* ptr, std::size_t bytes);
535
544 static void
545 operator delete[] (void* ptr, std::size_t bytes);
546
550 };
551
552 // ======================================================================
553
559 {
560 public:
571 constexpr attributes_clocked ();
572
576 attributes_clocked (const attributes_clocked&) = default;
579 operator= (const attributes_clocked&)
580 = default;
582 operator= (attributes_clocked&&)
583 = default;
592
612 rtos::clock* clock = nullptr;
613
618 protected:
627 };
628 } /* namespace internal */
629
630 // ========================================================================
631
641 {
642 public:
654 constexpr null_locker ();
655
660 null_locker (const null_locker&) = delete;
661 null_locker (null_locker&&) = delete;
663 operator= (const null_locker&)
664 = delete;
666 operator= (null_locker&&)
667 = delete;
668
676 ~null_locker ();
677
682 public:
695 void
696 lock (void);
697
705 void
706 unlock (void);
707
711 };
712
713 // ========================================================================
714 } /* namespace rtos */
715} /* namespace os */
716
717// ===== Inline & template implementations ====================================
718
719namespace os
720{
721 namespace rtos
722 {
723 // ========================================================================
724
726 {
727 }
728
729 inline void
731 {
732 // Does nothing.
733 }
734
735 inline void
737 {
738 // Does nothing.
739 }
740
741 namespace internal
742 {
743 // ======================================================================
744
752 inline const char*
754 {
755 return name_;
756 }
757
758 // ======================================================================
759
761 {
762 }
763
765 : object_named (name)
766 {
767 }
768
769 // ======================================================================
770
775 {
776 }
777
778 // ======================================================================
779 } /* namespace internal */
780 } /* namespace rtos */
781} /* namespace os */
782
783// ===== Port related definitions =============================================
784
785namespace os
786{
787 namespace rtos
788 {
789 namespace port
790 {
791 class thread;
792 class timer;
793 class mutex;
794
795 class condition_variable;
796 class semaphore;
797 class memory_pool;
798 class message_queue;
799 class event_flags;
800
801 // ======================================================================
802
803 namespace clock
804 {
812 using duration_t = uint32_t;
813
821 using timestamp_t = uint64_t;
822
823 using offset_t = int64_t;
824 } /* namespace clock */
825
826 // ----------------------------------------------------------------------
827
828 namespace interrupts
829 {
830
831 bool
833
834 // ====================================================================
835
837 {
838 public:
839 critical_section () = delete;
840
841 // Enter an IRQ critical section
843 enter (void);
844
845 // Exit an IRQ critical section
846 static void
848 };
849
850 // ====================================================================
851
853 {
854 public:
856
857 // Enter an IRQ uncritical section
859 enter (void);
860
861 // Exit an IRQ uncritical section
862 static void
864 };
865
866 } /* namespace interrupts */
867
868 // ----------------------------------------------------------------------
869
870 namespace scheduler
871 {
872
873 void
874 greeting (void);
875
878
879 [[noreturn]] void
880 start (void);
881
882 port::scheduler::state_t
883 lock (void);
884
885 port::scheduler::state_t
886 unlock (void);
887
888 port::scheduler::state_t
889 locked (port::scheduler::state_t state);
890
891 bool
892 locked (void);
893
894 void
896
897 stack::element_t*
898 switch_stacks (stack::element_t* sp);
899
900 void
902
903 bool
905
906 bool
908
909 } /* namespace scheduler */
910
911 // ----------------------------------------------------------------------
912
913 namespace this_thread
914 {
915
916 void
918
919 void
920 yield (void);
921
922 } /* namespace this_thread */
923
924 // ======================================================================
925
927 {
928 public:
929 context () = delete;
930
931 // Used to avoid a complex casts below,
932 // that might confuse the Eclipse formatter.
933 typedef void (*func_t) (void);
934
935 static void
936 create (void* context, void* func, void* args);
937 };
938 /* class context */
939
940 // ======================================================================
941 class thread
942 {
943 public:
944 thread () = delete;
945
946#if defined(OS_USE_RTOS_PORT_SCHEDULER)
947
948 static void
949 create (rtos::thread* obj);
950
951 [[noreturn]]
952 static void
953 destroy_this (rtos::thread* obj);
954
955 static void
956 destroy_other (rtos::thread* obj);
957
958 static void
959 resume (rtos::thread* obj);
960
961 // TODO: move it to port
962 using priority_t = uint8_t;
963
964 static priority_t
965 priority (rtos::thread* obj);
966
967 static result_t
968 priority (rtos::thread* obj, priority_t prio);
969
970 static result_t
971 detach (rtos::thread* obj);
972
973#endif
974 };
975 /* class thread */
976
977 // ======================================================================
979 {
980 public:
981 clock_systick () = delete;
982
983 static void
984 start (void);
985
986 static result_t
988
996 static void
998 };
999
1000 // ======================================================================
1001
1003 {
1004 public:
1005 clock_rtc () = delete;
1006
1014 static void
1016 };
1017
1018 // ======================================================================
1019
1021 {
1022 public:
1023 clock_highres () = delete;
1024
1025 static void
1026 start (void);
1027
1028 static uint32_t
1030
1031 static uint32_t
1033
1034 static uint32_t
1036 };
1037
1038 // ======================================================================
1039
1040 } /* namespace port */
1041 } /* namespace rtos */
1042} /* namespace os */
1043
1044#pragma GCC diagnostic pop
1045
1046// ----------------------------------------------------------------------------
1047
1048#pragma GCC diagnostic push
1049#if defined(__clang__)
1050#elif defined(__GNUC__)
1051#pragma GCC diagnostic ignored "-Waggregate-return"
1052#endif
1053// Must be included after the declarations
1055#pragma GCC diagnostic pop
1056
1057// ----------------------------------------------------------------------------
1058
1059extern "C"
1060{
1061
1066 // os-c-api.h
1067 // int
1068 // os_main (int argc, char* argv[]);
1069
1073}
1074
1075// ----------------------------------------------------------------------------
1076
1078
1079// ----------------------------------------------------------------------------
1080
1081// Required by os_assert_throw()
1083
1092#ifdef NDEBUG /* ANSI standard */
1093#define os_assert_err(__e, __er) \
1094 do \
1095 { \
1096 if (!(__e)) \
1097 return __er; \
1098 } \
1099 while (false)
1100#else
1101#define os_assert_err(__e, __er) assert (__e)
1102#endif
1103
1113#ifdef NDEBUG /* ANSI standard */
1114#define os_assert_throw(__e, __er) \
1115 do \
1116 { \
1117 if (!(__e)) \
1118 os::estd::__throw_system_error (__er, #__e); \
1119 } \
1120 while (false)
1121#else
1122#define os_assert_throw(__e, __er) assert (__e)
1123#endif
1124
1125#endif /* __cplusplus */
1126
1127// ----------------------------------------------------------------------------
1128
1129// Default definitions for various configuration macros.
1130// Redefine them in `<os-app-config.h>` to the actual values.
1131
1132#if !defined(OS_INTEGER_SYSTICK_FREQUENCY_HZ)
1133#define OS_INTEGER_SYSTICK_FREQUENCY_HZ (1000)
1134#endif
1135
1136// ----------------------------------------------------------------------------
1137
1138#if !defined(OS_INTEGER_RTOS_MAIN_STACK_SIZE_BYTES)
1139#define OS_INTEGER_RTOS_MAIN_STACK_SIZE_BYTES \
1140 (os::rtos::port::stack::default_size_bytes)
1141#endif
1142
1143#if !defined(OS_INTEGER_RTOS_IDLE_STACK_SIZE_BYTES)
1144#define OS_INTEGER_RTOS_IDLE_STACK_SIZE_BYTES \
1145 (os::rtos::port::stack::default_size_bytes)
1146#endif
1147
1148#if !defined(OS_BOOL_RTOS_SCHEDULER_PREEMPTIVE)
1149#define OS_BOOL_RTOS_SCHEDULER_PREEMPTIVE (true)
1150#endif
1151
1152#if !defined(OS_INTEGER_RTOS_REUSE_MAGIC)
1153#define OS_INTEGER_RTOS_REUSE_MAGIC (0xA55AAA55)
1154#endif
1155
1156// ----------------------------------------------------------------------------
1157
1158#endif /* CMSIS_PLUS_RTOS_OS_DECLS_H_ */
Generic clock.
Definition os-clocks.h:61
POSIX compliant condition variable.
Definition os-condvar.h:45
Synchronised event flags.
Definition os-evflags.h:57
Base class for attributes.
Definition os-decls.h:559
~attributes_clocked()=default
Destruct the named object instance.
constexpr attributes_clocked()
Construct a named object instance.
Definition os-decls.h:774
Base class for named system objects.
Definition os-decls.h:443
object_named_system()
Construct a named system object instance.
Definition os-decls.h:760
~object_named_system()=default
Destruct the named system object instance.
Base class for named objects.
Definition os-decls.h:352
object_named()
Construct a named object instance.
Definition os-core.cpp:620
const char * name(void) const
Get object name.
Definition os-decls.h:753
~object_named()=default
Destruct the named object instance.
Standard allocator based on the RTOS system default memory manager.
Definition os-memory.h:538
Synchronised memory pool, using the default RTOS allocator.
Definition os-mempool.h:67
POSIX compliant message queue, using the default RTOS allocator.
Definition os-mqueue.h:67
POSIX compliant mutex.
Definition os-mutex.h:52
constexpr null_locker()
Construct a null lockable object instance.
~null_locker()
Destruct the null lockable object instance.
Definition os-decls.h:725
void unlock(void)
Pretend to unlock the scheduler.
Definition os-decls.h:736
void lock(void)
Pretend to lock scheduler.
Definition os-decls.h:730
static uint32_t cycles_since_tick(void)
static uint32_t input_clock_frequency_hz(void)
static uint32_t cycles_per_tick(void)
static void internal_interrupt_service_routine(void)
RTC implementation hook.
static result_t wait_for(clock::duration_t ticks)
static void internal_interrupt_service_routine(void)
SysTick implementation hook.
void(* func_t)(void)
Definition os-decls.h:933
static void create(void *context, void *func, void *args)
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)
POSIX compliant semaphore.
POSIX compliant thread, using the default RTOS allocator.
Definition os-thread.h:251
User single-shot or periodic timer.
Definition os-timer.h:56
Standard thread.
clock_t clock(void)
@ all
Return when all flags are set.
Definition os-decls.h:295
@ clear
Ask for flags to be cleared after read.
Definition os-decls.h:305
@ any
Return when at least one flag is set.
Definition os-decls.h:300
@ all
Special mask to represent all flags.
Definition os-decls.h:322
@ any
Special mask to represent any flag.
Definition os-decls.h:317
uint32_t mode_t
Type of variables holding flags modes.
Definition os-decls.h:277
uint32_t mask_t
Type of variables holding flags masks.
Definition os-decls.h:266
port::interrupts::state_t state_t
Type of variables holding interrupts status codes.
Definition os-decls.h:242
uint32_t duration_t
Type of variables holding timer durations.
Definition os-decls.h:812
uint64_t timestamp_t
Type of variables holding time stamps.
Definition os-decls.h:821
port::scheduler::state_t unlock(void)
port::scheduler::state_t lock(void)
result_t initialize(void)
stack::element_t * switch_stacks(stack::element_t *sp)
@ ok
Function completed; no errors or events occurred.
Definition os-decls.h:179
port::scheduler::state_t state_t
Type of variables holding scheduler state codes.
Definition os-decls.h:200
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.
A namespace for functions applying to the current thread.