µOS++ IIIe Reference  v6.3.15
“Perfekt ist nicht gut genug”
The third edition of µOS++, a POSIX inspired open source system, written in C++.
os-c-wrapper.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 /*
29  * The code provides an implementation of the C API for the µOS++
30  * and legacy CMSIS, using the CMSIC++ RTOS definitions.
31  */
32 
33 #include <cmsis-plus/rtos/os.h>
35 
36 // ----------------------------------------------------------------------------
37 
38 using namespace os;
39 using namespace os::rtos;
40 
41 // ----------------------------------------------------------------------------
42 
43 // Validate C typedefs sizes & alignment.
44 
45 static_assert(sizeof(os_result_t) == sizeof(result_t), "adjust size of os_result_t");
46 static_assert(alignof(os_result_t) == alignof(result_t), "adjust align of os_result_t");
47 
48 static_assert(sizeof(os_flags_mode_t) == sizeof(flags::mode_t), "adjust size of os_flags_mode_t");
49 static_assert(alignof(os_flags_mode_t) == alignof(flags::mode_t), "adjust align of os_flags_mode_t");
50 
51 static_assert(sizeof(os_flags_mask_t) == sizeof(flags::mask_t), "adjust size of os_flags_mask_t");
52 static_assert(alignof(os_flags_mask_t) == alignof(flags::mask_t), "adjust align of os_flags_mask_t");
53 
54 static_assert(sizeof(os_sched_state_t) == sizeof(scheduler::state_t), "adjust size of os_sched_state_t");
55 static_assert(alignof(os_sched_state_t) == alignof(scheduler::state_t), "adjust align of os_sched_state_t");
56 
57 static_assert(sizeof(os_irq_state_t) == sizeof(interrupts::state_t), "adjust size of os_irq_state_t");
58 static_assert(alignof(os_irq_state_t) == alignof(interrupts::state_t), "adjust align of os_irq_state_t");
59 
60 static_assert(sizeof(os_clock_timestamp_t) == sizeof(clock::timestamp_t), "adjust size of os_port_clock_timestamp_t");
61 static_assert(alignof(os_clock_timestamp_t) == alignof(clock::timestamp_t), "adjust align of os_port_clock_timestamp_t");
62 
63 static_assert(sizeof(os_clock_duration_t) == sizeof(clock::duration_t), "adjust size of os_port_clock_duration_t");
64 static_assert(alignof(os_clock_duration_t) == alignof(clock::duration_t), "adjust align of os_port_clock_duration_t");
65 
66 static_assert(sizeof(os_clock_offset_t) == sizeof(clock::offset_t), "adjust size of os_port_clock_offset_t");
67 static_assert(alignof(os_clock_offset_t) == alignof(clock::offset_t), "adjust align of os_port_clock_offset_t");
68 
69 static_assert(sizeof(os_statistics_counter_t) == sizeof(statistics::counter_t), "adjust size of os_statistics_counter_t");
70 static_assert(alignof(os_statistics_counter_t) == alignof(statistics::counter_t), "adjust align of os_statistics_counter_t");
71 
72 static_assert(sizeof(os_statistics_duration_t) == sizeof(statistics::duration_t), "adjust size of os_statistics_duration_t");
73 static_assert(alignof(os_statistics_duration_t) == alignof(statistics::duration_t), "adjust align of os_statistics_duration_t");
74 
75 static_assert(sizeof(os_thread_func_args_t) == sizeof(thread::func_args_t), "adjust size of os_thread_func_args_t");
76 static_assert(alignof(os_thread_func_args_t) == alignof(thread::func_args_t), "adjust align of os_thread_func_args_t");
77 
78 static_assert(sizeof(os_thread_func_t) == sizeof(thread::func_t), "adjust size of os_thread_func_t");
79 static_assert(alignof(os_thread_func_t) == alignof(thread::func_t), "adjust align of os_thread_func_t");
80 
81 static_assert(sizeof(os_thread_state_t) == sizeof(thread::state_t), "adjust size of os_thread_state_t");
82 static_assert(alignof(os_thread_state_t) == alignof(thread::state_t), "adjust align of os_thread_state_t");
83 
84 static_assert(sizeof(os_thread_prio_t) == sizeof(thread::priority_t), "adjust size of os_thread_prio_t");
85 static_assert(alignof(os_thread_prio_t) == alignof(thread::priority_t), "adjust align of os_thread_prio_t");
86 
87 static_assert(sizeof(os_timer_func_args_t) == sizeof(timer::func_args_t), "adjust size of os_timer_func_args_t");
88 static_assert(alignof(os_timer_func_args_t) == alignof(timer::func_args_t), "adjust align of os_timer_func_args_t");
89 
90 static_assert(sizeof(os_timer_func_t) == sizeof(timer::func_t), "adjust size of os_timer_func_t");
91 static_assert(alignof(os_timer_func_t) == alignof(timer::func_t), "adjust align of os_timer_func_t");
92 
93 static_assert(sizeof(os_timer_type_t) == sizeof(timer::type_t), "adjust size of os_timer_type_t");
94 static_assert(alignof(os_timer_type_t) == alignof(timer::type_t), "adjust align of os_timer_type_t");
95 
96 static_assert(sizeof(os_timer_state_t) == sizeof(timer::state_t), "adjust size of os_timer_state_t");
97 static_assert(alignof(os_timer_state_t) == alignof(timer::state_t), "adjust align of os_timer_state_t");
98 
99 static_assert(sizeof(os_mutex_count_t) == sizeof(mutex::count_t), "adjust size of os_mutex_count_t");
100 static_assert(alignof(os_mutex_count_t) == alignof(mutex::count_t), "adjust align of os_mutex_count_t");
101 
102 static_assert(sizeof(os_mutex_type_t) == sizeof(mutex::type_t), "adjust size of os_mutex_type_t");
103 static_assert(alignof(os_mutex_type_t) == alignof(mutex::type_t), "adjust align of os_mutex_type_t");
104 
105 static_assert(sizeof(os_mutex_protocol_t) == sizeof(mutex::protocol_t), "adjust size of os_mutex_protocol_t");
106 static_assert(alignof(os_mutex_protocol_t) == alignof(mutex::protocol_t), "adjust align of os_mutex_protocol_t");
107 
108 static_assert(sizeof(os_mutex_robustness_t) == sizeof(mutex::robustness_t), "adjust size of os_mutex_robustness_t");
109 static_assert(alignof(os_mutex_robustness_t) == alignof(mutex::robustness_t), "adjust align of os_mutex_robustness_t");
110 
111 static_assert(sizeof(os_semaphore_count_t) == sizeof(semaphore::count_t), "adjust size of os_semaphore_count_t");
112 static_assert(alignof(os_semaphore_count_t) == alignof(semaphore::count_t), "adjust align of os_semaphore_count_t");
113 
114 static_assert(sizeof(os_mempool_size_t) == sizeof(memory_pool::size_t), "adjust size of os_mempool_size_t");
115 static_assert(alignof(os_mempool_size_t) == alignof(memory_pool::size_t), "adjust align of os_mempool_size_t");
116 
117 static_assert(sizeof(os_mqueue_size_t) == sizeof(message_queue::size_t), "adjust size of os_mqueue_size_t");
118 static_assert(alignof(os_mqueue_size_t) == alignof(message_queue::size_t), "adjust align of os_mqueue_size_t");
119 
120 static_assert(sizeof(os_mqueue_msg_size_t) == sizeof(message_queue::msg_size_t), "adjust size of os_mqueue_msg_size_t");
121 static_assert(alignof(os_mqueue_msg_size_t) == alignof(message_queue::msg_size_t), "adjust align of os_mqueue_msg_size_t");
122 
123 static_assert(sizeof(os_mqueue_index_t) == sizeof(message_queue::index_t), "adjust size of os_mqueue_index_t");
124 static_assert(alignof(os_mqueue_index_t) == alignof(message_queue::index_t), "adjust align of os_mqueue_index_t");
125 
126 static_assert(sizeof(os_mqueue_prio_t) == sizeof(message_queue::priority_t), "adjust size of os_mqueue_prio_t");
127 static_assert(alignof(os_mqueue_prio_t) == alignof(message_queue::priority_t), "adjust align of os_mqueue_prio_t");
128 
129 // ----------------------------------------------------------------------------
130 
131 // Validate C enumeration values
132 
133 #pragma GCC diagnostic push
134 #pragma GCC diagnostic ignored "-Wenum-compare"
135 
136 static_assert(os_thread_priority_idle == thread::priority::idle, "adjust os_thread_priority_idle");
137 static_assert(os_thread_priority_low == thread::priority::low, "adjust os_thread_priority_low");
138 static_assert(os_thread_priority_below_normal == thread::priority::below_normal, "adjust os_thread_priority_below_normal");
139 static_assert(os_thread_priority_normal == thread::priority::normal, "adjust os_thread_priority_normal");
140 static_assert(os_thread_priority_above_normal == thread::priority::above_normal, "adjust os_thread_priority_above_normal");
141 static_assert(os_thread_priority_high == thread::priority::high, "adjust os_thread_priority_high");
142 static_assert(os_thread_priority_realtime == thread::priority::realtime, "adjust os_thread_priority_realtime");
143 static_assert(os_thread_priority_error == thread::priority::error, "adjust os_thread_priority_error");
144 
145 static_assert(os_flags_mode_all == flags::mode::all, "adjust os_flags_mode_all");
146 static_assert(os_flags_mode_any == flags::mode::any, "adjust os_flags_mode_any");
147 static_assert(os_flags_mode_clear == flags::mode::clear, "adjust os_flags_mode_clear");
148 
149 static_assert(os_thread_state_undefined == thread::state::undefined, "adjust os_thread_state_undefined");
150 static_assert(os_thread_state_ready == thread::state::ready, "adjust os_thread_state_ready");
151 static_assert(os_thread_state_running == thread::state::running, "adjust os_thread_state_running");
152 static_assert(os_thread_state_suspended == thread::state::suspended, "adjust os_thread_state_suspended");
153 static_assert(os_thread_state_terminated == thread::state::terminated, "adjust os_thread_state_terminated");
154 static_assert(os_thread_state_destroyed == thread::state::destroyed, "adjust os_thread_state_destroyed");
155 
156 static_assert(os_timer_once == timer::run::once, "adjust os_timer_once");
157 static_assert(os_timer_periodic == timer::run::periodic, "adjust os_timer_periodic");
158 
159 static_assert(os_mutex_protocol_none == mutex::protocol::none, "adjust os_mutex_protocol_none");
160 static_assert(os_mutex_protocol_inherit == mutex::protocol::inherit, "adjust os_mutex_protocol_inherit");
161 static_assert(os_mutex_protocol_protect == mutex::protocol::protect, "adjust os_mutex_protocol_protect");
162 
163 static_assert(os_mutex_robustness_stalled == mutex::robustness::stalled, "adjust os_mutex_robustness_stalled");
164 static_assert(os_mutex_robustness_robust == mutex::robustness::robust, "adjust os_mutex_robustness_robust");
165 
166 static_assert(os_mutex_type_normal == mutex::type::normal, "adjust os_mutex_type_normal");
167 static_assert(os_mutex_type_errorcheck == mutex::type::errorcheck, "adjust os_mutex_type_errorcheck");
168 static_assert(os_mutex_type_recursive == mutex::type::recursive, "adjust os_mutex_type_recursive");
169 static_assert(os_mutex_type_default == mutex::type::default_, "adjust os_mutex_type_default");
170 
171 #pragma GCC diagnostic pop
172 
173 // ----------------------------------------------------------------------------
174 
175 // Validate C structs sizes (should match the C++ objects sizes).
176 // Validate offset of individual members (if needed, validate member size).
177 
178 #pragma GCC diagnostic push
179 #pragma GCC diagnostic ignored "-Winvalid-offsetof"
180 
181 static_assert(sizeof(rtos::clock) == sizeof(os_clock_t), "adjust os_clock_t size");
182 
183 static_assert(sizeof(rtos::thread) == sizeof(os_thread_t), "adjust os_thread_t size");
184 static_assert(sizeof(rtos::thread::attributes) == sizeof(os_thread_attr_t), "adjust os_thread_attr_t size");
185 static_assert(offsetof(rtos::thread::attributes, th_stack_address) == offsetof(os_thread_attr_t, th_stack_address), "adjust os_thread_attr_t members");
186 static_assert(offsetof(rtos::thread::attributes, th_stack_size_bytes) == offsetof(os_thread_attr_t, th_stack_size_bytes), "adjust os_thread_attr_t members");
187 static_assert(offsetof(rtos::thread::attributes, th_priority) == offsetof(os_thread_attr_t, th_priority), "adjust os_thread_attr_t members");
188 
189 static_assert(sizeof(rtos::timer) == sizeof(os_timer_t), "adjust size of os_timer_t");
190 static_assert(sizeof(rtos::timer::attributes) == sizeof(os_timer_attr_t), "adjust size of os_timer_attr_t");
191 static_assert(offsetof(rtos::timer::attributes, tm_type) == offsetof(os_timer_attr_t, tm_type), "adjust os_timer_attr_t members");
192 
193 static_assert(sizeof(rtos::mutex) == sizeof(os_mutex_t), "adjust size of os_mutex_t");
194 static_assert(sizeof(rtos::mutex::attributes) == sizeof(os_mutex_attr_t), "adjust size of os_mutex_attr_t");
195 static_assert(offsetof(rtos::mutex::attributes, mx_priority_ceiling) == offsetof(os_mutex_attr_t, mx_priority_ceiling), "adjust os_mutex_attr_t members");
196 static_assert(offsetof(rtos::mutex::attributes, mx_protocol) == offsetof(os_mutex_attr_t, mx_protocol), "adjust os_mutex_attr_t members");
197 static_assert(offsetof(rtos::mutex::attributes, mx_robustness) == offsetof(os_mutex_attr_t, mx_robustness), "adjust os_mutex_attr_t members");
198 static_assert(offsetof(rtos::mutex::attributes, mx_type) == offsetof(os_mutex_attr_t, mx_type), "adjust os_mutex_attr_t members");
199 static_assert(offsetof(rtos::mutex::attributes, mx_max_count) == offsetof(os_mutex_attr_t, mx_max_count), "adjust os_mutex_attr_t members");
200 
201 static_assert(sizeof(rtos::condition_variable) == sizeof(os_condvar_t), "adjust size of os_condvar_t");
202 static_assert(sizeof(rtos::condition_variable::attributes) == sizeof(os_condvar_attr_t), "adjust size of os_condvar_attr_t");
203 
204 static_assert(sizeof(rtos::semaphore) == sizeof(os_semaphore_t), "adjust size of os_semaphore_t");
205 static_assert(sizeof(rtos::semaphore::attributes) == sizeof(os_semaphore_attr_t), "adjust size of os_semaphore_attr_t");
206 static_assert(offsetof(rtos::semaphore::attributes, sm_initial_value) == offsetof(os_semaphore_attr_t, sm_initial_value), "adjust os_semaphore_attr_t members");
207 static_assert(offsetof(rtos::semaphore::attributes, sm_max_value) == offsetof(os_semaphore_attr_t, sm_max_value), "adjust os_semaphore_attr_t members");
208 
209 static_assert(sizeof(rtos::memory_pool) == sizeof(os_mempool_t), "adjust size of os_mempool_t");
210 static_assert(sizeof(rtos::memory_pool::attributes) == sizeof(os_mempool_attr_t), "adjust size of os_mempool_attr_t");
211 static_assert(offsetof(rtos::memory_pool::attributes, mp_pool_address) == offsetof(os_mempool_attr_t, mp_pool_address), "adjust os_mempool_attr_t members");
212 static_assert(offsetof(rtos::memory_pool::attributes, mp_pool_size_bytes) == offsetof(os_mempool_attr_t, mp_pool_size_bytes), "adjust os_mempool_attr_t members");
213 
214 static_assert(sizeof(rtos::message_queue) == sizeof(os_mqueue_t), "adjust size of os_mqueue_t");
215 static_assert(sizeof(rtos::message_queue::attributes) == sizeof(os_mqueue_attr_t), "adjust size of os_mqueue_attr_t");
216 static_assert(offsetof(rtos::message_queue::attributes, mq_queue_address) == offsetof(os_mqueue_attr_t, mq_queue_addr), "adjust os_mqueue_attr_t members");
217 static_assert(offsetof(rtos::message_queue::attributes, mq_queue_size_bytes) == offsetof(os_mqueue_attr_t, mq_queue_size_bytes), "adjust os_mqueue_attr_t members");
218 
219 static_assert(sizeof(rtos::event_flags) == sizeof(os_evflags_t), "adjust size of os_evflags_t");
220 static_assert(sizeof(rtos::event_flags::attributes) == sizeof(os_evflags_attr_t), "adjust size of os_evflags_attr_t");
221 
222 static_assert(sizeof(class thread::stack) == sizeof(os_thread_stack_t), "adjust size of os_thread_stack_t");
223 static_assert(sizeof(class thread::context) == sizeof(os_thread_context_t), "adjust size of os_thread_context_t");
224 
225 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) \
226  || defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
227 static_assert(sizeof(class thread::statistics) == sizeof(os_thread_statistics_t), "adjust size of os_thread_statistics_t");
228 #endif
229 
230 static_assert(sizeof(internal::timer_node) == sizeof(os_internal_clock_timer_node_t), "adjust size of os_internal_clock_timer_node_t");
231 
232 #pragma GCC diagnostic pop
233 
234 #pragma GCC diagnostic push
235 #pragma GCC diagnostic ignored "-Wold-style-cast"
236 
237 // ----------------------------------------------------------------------------
238 
249 {
251 }
252 
261 void
263 {
264  scheduler::start ();
265 }
266 
275 bool
277 {
278  return scheduler::started ();
279 }
280 
291 {
292  return scheduler::lock ();
293 }
294 
305 {
306  return scheduler::unlock ();
307 }
308 
319 {
320  return scheduler::locked (state);
321 }
322 
331 bool
333 {
334  return scheduler::locked ();
335 }
336 
345 bool
347 {
348  return scheduler::preemptive ();
349 }
350 
359 bool
361 {
362  return scheduler::preemptive (state);
363 }
364 
365 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES)
366 
377 {
379 }
380 
381 #endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) */
382 
383 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
384 
395 {
397 }
398 
399 #endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
400 
401 // ----------------------------------------------------------------------------
402 
411 bool
413 {
414  return interrupts::in_handler_mode ();
415 }
416 
417 // ----------------------------------------------------------------------------
418 
429 {
431 }
432 
441 void
443 {
445 }
446 
447 // ----------------------------------------------------------------------------
448 
459 {
461 }
462 
471 void
473 {
475 }
476 
477 #if defined(OS_HAS_INTERRUPTS_STACK) || defined(__DOXYGEN__)
478 
490  {
491  return reinterpret_cast<os_thread_stack_t*> (rtos::interrupts::stack ());
492  }
493 
494 #endif
495 
496 // ----------------------------------------------------------------------------
497 
508 {
509  return (os_thread_t*) &this_thread::thread ();
510 }
511 
520 void
522 {
523  return this_thread::suspend ();
524 }
525 
534 void
535 os_this_thread_exit (void* exit_ptr)
536 {
537  this_thread::exit (exit_ptr);
538 }
539 
550  os_flags_mode_t mode)
551 {
552  return (os_result_t) this_thread::flags_wait (mask, oflags, mode);
553 }
554 
565  os_flags_mode_t mode)
566 {
567  return (os_result_t) this_thread::flags_try_wait (mask, oflags, mode);
568 }
569 
580  os_clock_duration_t timeout,
581  os_flags_mask_t* oflags, os_flags_mode_t mode)
582 {
583  return (os_result_t) this_thread::flags_timed_wait (mask, timeout, oflags,
584  mode);
585 }
586 
597 {
598  return (os_result_t) this_thread::flags_clear (mask, oflags);
599 }
600 
611 {
612  return (os_flags_mask_t) this_thread::flags_get (mask, mode);
613 }
614 
615 // ----------------------------------------------------------------------------
616 
625 void
627 {
628  assert (attr != nullptr);
629  new (attr) thread::attributes ();
630 }
631 
642 void
644  os_thread_func_t func, const os_thread_func_args_t args,
645  const os_thread_attr_t* attr)
646 {
647  assert (thread != nullptr);
648  if (attr == nullptr)
649  {
650  attr = (const os_thread_attr_t*) &thread::initializer;
651  }
652  new (thread) rtos::thread (name, (thread::func_t) func,
653  (thread::func_args_t) args,
654  (const thread::attributes&) *attr);
655 }
656 
667 void
669 {
670  assert (thread != nullptr);
671  (reinterpret_cast<rtos::thread&> (*thread)).~thread ();
672 }
673 
689 os_thread_new (const char* name, os_thread_func_t func,
690  const os_thread_func_args_t args, const os_thread_attr_t* attr)
691 {
692  if (attr == nullptr)
693  {
694  attr = (const os_thread_attr_t*) &thread::initializer;
695  }
696  return reinterpret_cast<os_thread_t*> (new rtos::thread (
697  name, (thread::func_t) func, (thread::func_args_t) args,
698  (const thread::attributes&) *attr));
699 }
700 
715 void
717 {
718  assert (thread != nullptr);
719  delete reinterpret_cast<rtos::thread*> (thread);
720 }
721 
730 const char*
732 {
733  assert (thread != nullptr);
734  return (reinterpret_cast<rtos::thread&> (*thread)).name ();
735 }
736 
747 {
748  assert (thread != nullptr);
749  return (os_thread_prio_t) (reinterpret_cast<rtos::thread&> (*thread)).priority ();
750 }
751 
762 {
763  assert (thread != nullptr);
764  return (os_result_t) (reinterpret_cast<rtos::thread&> (*thread)).priority (
765  prio);
766 }
767 
777 os_thread_join (os_thread_t* thread, void** exit_ptr)
778 {
779  assert (thread != nullptr);
780  return (os_result_t) reinterpret_cast<rtos::thread&> (*thread).join (exit_ptr);
781 }
782 
791 void
793 {
794  assert (thread != nullptr);
795  return (reinterpret_cast<rtos::thread&> (*thread)).resume ();
796 }
797 
808  os_flags_mask_t* oflags)
809 {
810  assert (thread != nullptr);
811  return (os_result_t) (reinterpret_cast<rtos::thread&> (*thread)).flags_raise (
812  mask, oflags);
813 }
814 
825 {
826  assert (thread != nullptr);
827  return reinterpret_cast<os_thread_state_t> ((reinterpret_cast<rtos::thread&> (*thread)).state ());
828 }
829 
830 #if defined(OS_INCLUDE_RTOS_CUSTOM_THREAD_USER_STORAGE) || defined(__DOXYGEN__)
831 
844 os_thread_user_storage_t*
846  {
847  assert(thread != nullptr);
848  return (reinterpret_cast<rtos::thread&> (*thread)).user_storage ();
849  }
850 
851 #endif /* defined(OS_INCLUDE_RTOS_CUSTOM_THREAD_USER_STORAGE) */
852 
863 {
864  assert (thread != nullptr);
865  return reinterpret_cast<os_thread_stack_t*> (&(reinterpret_cast<rtos::thread&> (*thread)).stack ());
866 }
867 
868 // ----------------------------------------------------------------------------
869 
878 size_t
880 {
881  return thread::stack::default_size ();
882 }
883 
892 size_t
894 {
895  return thread::stack::default_size (size_bytes);
896 }
897 
906 size_t
908 {
909  return thread::stack::min_size ();
910 }
911 
920 size_t
921 os_thread_stack_set_min_size (size_t size_bytes)
922 {
923  return thread::stack::min_size (size_bytes);
924 }
925 
936 {
937  assert (stack != nullptr);
938  return (reinterpret_cast<class rtos::thread::stack&> (*stack)).bottom ();
939 }
940 
951 {
952  assert (stack != nullptr);
953  return (reinterpret_cast<class rtos::thread::stack&> (*stack)).top ();
954 }
955 
964 size_t
966 {
967  assert (stack != nullptr);
968  return (reinterpret_cast<class rtos::thread::stack&> (*stack)).size ();
969 }
970 
979 size_t
981 {
982  assert (stack != nullptr);
983  return (reinterpret_cast<class rtos::thread::stack&> (*stack)).available ();
984 }
985 
994 bool
996 {
997  assert (stack != nullptr);
998  return (reinterpret_cast<class rtos::thread::stack&> (*stack)).check_bottom_magic ();
999 }
1000 
1009 bool
1011 {
1012  assert (stack != nullptr);
1013  return (reinterpret_cast<class rtos::thread::stack&> (*stack)).check_top_magic ();
1014 }
1015 
1016 // ----------------------------------------------------------------------------
1017 
1018 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES)
1019 
1030 {
1031  assert (thread != nullptr);
1032  return static_cast<os_statistics_counter_t> ((reinterpret_cast<rtos::thread&> (*thread)).statistics ().context_switches ());
1033 }
1034 
1035 #endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CONTEXT_SWITCHES) */
1036 
1037 #if defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES)
1038 
1049 {
1050  assert (thread != nullptr);
1051  return static_cast<os_statistics_duration_t> ((reinterpret_cast<rtos::thread&> (*thread)).statistics ().cpu_cycles ());
1052 }
1053 
1054 #endif /* defined(OS_INCLUDE_RTOS_STATISTICS_THREAD_CPU_CYCLES) */
1055 
1056 // ----------------------------------------------------------------------------
1057 
1068 {
1069  return reinterpret_cast<os_iterator_t> (scheduler::children_threads (
1070  reinterpret_cast<rtos::thread*> (thread)).begin ().get_iterator_pointer ());
1071 }
1072 
1086 {
1087  return reinterpret_cast<os_iterator_t> (scheduler::children_threads (
1088  reinterpret_cast<rtos::thread*> (thread)).end ().get_iterator_pointer ());
1089 }
1090 
1098 os_thread_t*
1100 {
1101  // Construct a local iterator object based on the pointer.
1102  thread::threads_list::iterator it
1103  { reinterpret_cast<utils::double_list_links*> (iterator) };
1104  return reinterpret_cast<os_thread_t*> (&(*it));
1105 }
1106 
1116 {
1117  thread::threads_list::iterator it
1118  { reinterpret_cast<utils::double_list_links*> (iterator) };
1119  ++it;
1120 
1121  return reinterpret_cast<os_iterator_t> (it.get_iterator_pointer ());
1122 }
1123 
1124 // ----------------------------------------------------------------------------
1125 
1134 const char*
1136 {
1137  assert (clock != nullptr);
1138  return (reinterpret_cast<rtos::clock&> (*clock)).name ();
1139 }
1140 
1151 {
1152  assert (clock != nullptr);
1153  return (os_clock_timestamp_t) (reinterpret_cast<rtos::clock&> (*clock)).now ();
1154 }
1155 
1166 {
1167  assert (clock != nullptr);
1168  return (os_clock_timestamp_t) (reinterpret_cast<rtos::clock&> (*clock)).steady_now ();
1169 }
1170 
1181 {
1182  assert (clock != nullptr);
1183  return (os_result_t) (reinterpret_cast<rtos::clock&> (*clock)).sleep_for (
1184  duration);
1185 }
1186 
1197 {
1198  assert (clock != nullptr);
1199  return (os_result_t) (reinterpret_cast<rtos::clock&> (*clock)).sleep_until (
1200  timestamp);
1201 }
1202 
1213 {
1214  assert (clock != nullptr);
1215  return (os_result_t) (reinterpret_cast<rtos::clock&> (*clock)).wait_for (
1216  timeout);
1217 }
1218 
1229 {
1230  assert (clock != nullptr);
1231  return (os_clock_offset_t) (reinterpret_cast<rtos::clock&> (*clock)).offset ();
1232 
1233 }
1234 
1245 {
1246  assert (clock != nullptr);
1247  assert (clock != nullptr);
1248  return (os_clock_offset_t) (reinterpret_cast<rtos::clock&> (*clock)).offset (
1249  (clock::offset_t) offset);
1250 
1251 }
1252 
1261 os_clock_t*
1263 {
1264  return (os_clock_t*) &sysclock;
1265 }
1266 
1275 os_clock_t*
1277 {
1278  return (os_clock_t*) &rtclock;
1279 }
1280 
1289 os_clock_t*
1291 {
1292  return (os_clock_t*) &hrclock;
1293 }
1294 
1295 // ----------------------------------------------------------------------------
1296 
1307 {
1308  return (os_clock_timestamp_t) sysclock.now ();
1309 }
1310 
1321 {
1322  return (os_result_t) sysclock.sleep_for (duration);
1323 }
1324 
1335 {
1336  return (os_result_t) sysclock.sleep_until (timestamp);
1337 }
1338 
1349 {
1350  return (os_result_t) sysclock.wait_for (timeout);
1351 }
1352 
1353 // ----------------------------------------------------------------------------
1354 
1363 void
1365 {
1366  assert (attr != nullptr);
1367  new (attr) timer::attributes ();
1368 }
1369 
1378 void
1380 {
1381  assert (attr != nullptr);
1382  new (attr) timer::attributes_periodic ();
1383 }
1384 
1393 const os_timer_attr_t*
1395 {
1397 }
1398 
1409 void
1411  os_timer_func_t function, os_timer_func_args_t args,
1412  const os_timer_attr_t* attr)
1413 {
1414  assert (timer != nullptr);
1415  if (attr == nullptr)
1416  {
1418  }
1419  new (timer) rtos::timer (name, (timer::func_t) function,
1420  (timer::func_args_t) args,
1421  (const timer::attributes&) *attr);
1422 }
1423 
1434 void
1436 {
1437  assert (timer != nullptr);
1438  (reinterpret_cast<rtos::timer&> (*timer)).~timer ();
1439 }
1440 
1455 os_timer_t*
1456 os_timer_new (const char* name, os_timer_func_t function,
1457  os_timer_func_args_t args, const os_timer_attr_t* attr)
1458 {
1459  if (attr == nullptr)
1460  {
1462  }
1463  return reinterpret_cast<os_timer_t*> (new rtos::timer (
1464  name, (timer::func_t) function, (timer::func_args_t) args,
1465  (const timer::attributes&) *attr));
1466 }
1467 
1482 void
1484 {
1485  assert (timer != nullptr);
1486  delete reinterpret_cast<rtos::timer*> (timer);
1487 }
1488 
1497 const char*
1499 {
1500  assert (timer != nullptr);
1501  return (reinterpret_cast<rtos::timer&> (*timer)).name ();
1502 }
1503 
1514 {
1515  assert (timer != nullptr);
1516  return (os_result_t) (reinterpret_cast<rtos::timer&> (*timer)).start (period);
1517 }
1518 
1529 {
1530  assert (timer != nullptr);
1531  return (os_result_t) (reinterpret_cast<rtos::timer&> (*timer)).stop ();
1532 }
1533 
1534 // ----------------------------------------------------------------------------
1535 
1544 void
1546 {
1547  assert (attr != nullptr);
1548  new (attr) mutex::attributes ();
1549 }
1550 
1559 void
1561 {
1562  assert (attr != nullptr);
1563  new (attr) mutex::attributes_recursive ();
1564 }
1565 
1574 const os_mutex_attr_t*
1576 {
1578 }
1579 
1590 void
1592  const os_mutex_attr_t* attr)
1593 {
1594  assert (mutex != nullptr);
1595  if (attr == nullptr)
1596  {
1597  attr = (const os_mutex_attr_t*) &mutex::initializer_normal;
1598  }
1599  new (mutex) rtos::mutex (name, (const mutex::attributes&) *attr);
1600 }
1601 
1612 void
1614  const os_mutex_attr_t* attr)
1615 {
1616  assert (mutex != nullptr);
1617  if (attr == nullptr)
1618  {
1620  }
1621  new (mutex) rtos::mutex_recursive (name, (const mutex::attributes&) *attr);
1622 }
1623 
1635 void
1637 {
1638  assert (mutex != nullptr);
1639  (reinterpret_cast<rtos::mutex&> (*mutex)).~mutex ();
1640 }
1641 
1656 os_mutex_t*
1657 os_mutex_new (const char* name, const os_mutex_attr_t* attr)
1658 {
1659  if (attr == nullptr)
1660  {
1661  attr = (const os_mutex_attr_t*) &mutex::initializer_normal;
1662  }
1663  return reinterpret_cast<os_mutex_t*> (new rtos::mutex (
1664  name, (const mutex::attributes&) *attr));
1665 }
1666 
1681 os_mutex_t*
1682 os_mutex_recursive_new (const char* name, const os_mutex_attr_t* attr)
1683 {
1684  if (attr == nullptr)
1685  {
1687  }
1688  return reinterpret_cast<os_mutex_t*> (new rtos::mutex_recursive (
1689  name, (const mutex::attributes&) *attr));
1690 }
1691 
1707 void
1709 {
1710  assert (mutex != nullptr);
1711  delete reinterpret_cast<rtos::mutex*> (mutex);
1712 }
1713 
1722 const char*
1724 {
1725  assert (mutex != nullptr);
1726  return (reinterpret_cast<rtos::mutex&> (*mutex)).name ();
1727 }
1728 
1739 {
1740  assert (mutex != nullptr);
1741  return (os_result_t) (reinterpret_cast<rtos::mutex&> (*mutex)).lock ();
1742 }
1743 
1754 {
1755  assert (mutex != nullptr);
1756  return (os_result_t) (reinterpret_cast<rtos::mutex&> (*mutex)).try_lock ();
1757 }
1758 
1769 {
1770  assert (mutex != nullptr);
1771  return (os_result_t) (reinterpret_cast<rtos::mutex&> (*mutex)).timed_lock (
1772  timeout);
1773 }
1774 
1785 {
1786  assert (mutex != nullptr);
1787  return (os_result_t) (reinterpret_cast<rtos::mutex&> (*mutex)).unlock ();
1788 }
1789 
1800 {
1801  assert (mutex != nullptr);
1802  return (os_thread_prio_t) (reinterpret_cast<rtos::mutex&> (*mutex)).prio_ceiling ();
1803 }
1804 
1815  os_thread_prio_t* old_prio_ceiling)
1816 {
1817  assert (mutex != nullptr);
1818  return (os_result_t) (reinterpret_cast<rtos::mutex&> (*mutex)).prio_ceiling (
1819  prio_ceiling, old_prio_ceiling);
1820 }
1821 
1832 {
1833  assert (mutex != nullptr);
1834  return (os_result_t) (reinterpret_cast<rtos::mutex&> (*mutex)).consistent ();
1835 }
1836 
1845 os_thread_t*
1847 {
1848  assert (mutex != nullptr);
1849  return (os_thread_t*) (reinterpret_cast<rtos::mutex&> (*mutex)).owner ();
1850 }
1851 
1862 {
1863  assert (mutex != nullptr);
1864  return (reinterpret_cast<rtos::mutex&> (*mutex)).type ();
1865 }
1866 
1877 {
1878  assert (mutex != nullptr);
1879  return (reinterpret_cast<rtos::mutex&> (*mutex)).protocol ();
1880 }
1881 
1892 {
1893  assert (mutex != nullptr);
1894  return (reinterpret_cast<rtos::mutex&> (*mutex)).robustness ();
1895 }
1896 
1907 {
1908  assert (mutex != nullptr);
1909  return (os_result_t) (reinterpret_cast<rtos::mutex&> (*mutex)).reset ();
1910 }
1911 
1912 // ----------------------------------------------------------------------------
1913 
1922 void
1924 {
1925  assert (attr != nullptr);
1926  new (attr) condition_variable::attributes ();
1927 }
1928 
1939 void
1940 os_condvar_construct (os_condvar_t* condvar, const char* name,
1941  const os_condvar_attr_t* attr)
1942 {
1943  assert (condvar != nullptr);
1944  if (attr == nullptr)
1945  {
1947  }
1948  new (condvar) condition_variable (name,
1949  (const condition_variable::attributes&) *attr);
1950 }
1951 
1962 void
1964 {
1965  assert (condvar != nullptr);
1966  (reinterpret_cast<condition_variable&> (*condvar)).~condition_variable ();
1967 }
1968 
1983 os_condvar_t*
1984 os_condvar_new (const char* name, const os_condvar_attr_t* attr)
1985 {
1986  if (attr == nullptr)
1987  {
1989  }
1990  return reinterpret_cast<os_condvar_t*> (new condition_variable (
1991  name, (const condition_variable::attributes&) *attr));
1992 }
1993 
2008 void
2010 {
2011  assert (condvar != nullptr);
2012  delete reinterpret_cast<condition_variable*> (condvar);
2013 }
2014 
2023 const char*
2025 {
2026  assert (condvar != nullptr);
2027  return (reinterpret_cast<condition_variable&> (*condvar)).name ();
2028 }
2029 
2040 {
2041  assert (condvar != nullptr);
2042  return (os_result_t) (reinterpret_cast<condition_variable&> (*condvar)).signal ();
2043 }
2044 
2055 {
2056  assert (condvar != nullptr);
2057  return (os_result_t) (reinterpret_cast<condition_variable&> (*condvar)).broadcast ();
2058 }
2059 
2070 {
2071  assert (condvar != nullptr);
2072  return (os_result_t) (reinterpret_cast<condition_variable&> (*condvar)).wait (
2073  reinterpret_cast<rtos::mutex&> (*mutex));
2074 }
2075 
2086  os_clock_duration_t timeout)
2087 {
2088  assert (condvar != nullptr);
2089  return (os_result_t) (reinterpret_cast<condition_variable&> (*condvar)).timed_wait (
2090  reinterpret_cast<rtos::mutex&> (*mutex), timeout);
2091 }
2092 
2093 // ----------------------------------------------------------------------------
2094 
2103 void
2105 {
2106  assert (attr != nullptr);
2107  new (attr) semaphore::attributes
2108  { };
2109 }
2110 
2119 void
2121  const os_semaphore_count_t initial_value)
2122 {
2123  assert (attr != nullptr);
2124  new (attr) semaphore::attributes_binary
2125  { initial_value };
2126 }
2127 
2136 void
2138  const os_semaphore_count_t max_value,
2139  const os_semaphore_count_t initial_value)
2140 {
2141  assert (attr != nullptr);
2143  { max_value, initial_value };
2144 }
2145 
2154 const os_semaphore_attr_t*
2156 {
2158 }
2159 
2170 void
2172  const os_semaphore_attr_t* attr)
2173 {
2174  assert (semaphore != nullptr);
2175  if (attr == nullptr)
2176  {
2178  }
2179  new (semaphore) rtos::semaphore
2180  { name, (const semaphore::attributes&) *attr };
2181 }
2182 
2193 void
2195  const os_semaphore_count_t initial_value)
2196 {
2197  assert (semaphore != nullptr);
2198  new (semaphore) rtos::semaphore_binary
2199  { name, initial_value };
2200 }
2201 
2212 void
2214  const os_semaphore_count_t max_value,
2215  const os_semaphore_count_t initial_value)
2216 {
2217  assert (semaphore != nullptr);
2218  new (semaphore) rtos::semaphore_counting
2219  { name, max_value, initial_value };
2220 }
2221 
2233 void
2235 {
2236  assert (semaphore != nullptr);
2237  (reinterpret_cast<rtos::semaphore&> (*semaphore)).~semaphore ();
2238 }
2239 
2255 os_semaphore_new (const char* name, const os_semaphore_attr_t* attr)
2256 {
2257  if (attr == nullptr)
2258  {
2260  }
2261  return reinterpret_cast<os_semaphore_t*> (new rtos::semaphore
2262  { name, (const semaphore::attributes&) *attr });
2263 }
2264 
2280 os_semaphore_binary_new (const char* name,
2281  const os_semaphore_count_t initial_value)
2282 {
2283  return reinterpret_cast<os_semaphore_t*> (new rtos::semaphore_binary
2284  { name, initial_value });
2285 }
2286 
2302 os_semaphore_counting_new (const char* name,
2303  const os_semaphore_count_t max_value,
2304  const os_semaphore_count_t initial_value)
2305 {
2306  return reinterpret_cast<os_semaphore_t*> (new rtos::semaphore_counting
2307  { name, max_value, initial_value });
2308 }
2309 
2325 void
2327 {
2328  assert (semaphore != nullptr);
2329  delete reinterpret_cast<rtos::semaphore*> (semaphore);
2330 }
2331 
2340 const char*
2342 {
2343  assert (semaphore != nullptr);
2344  return (reinterpret_cast<rtos::semaphore&> (*semaphore)).name ();
2345 }
2346 
2357 {
2358  assert (semaphore != nullptr);
2359  return (os_result_t) (reinterpret_cast<rtos::semaphore&> (*semaphore)).post ();
2360 }
2361 
2372 {
2373  assert (semaphore != nullptr);
2374  return (os_result_t) (reinterpret_cast<rtos::semaphore&> (*semaphore)).wait ();
2375 }
2376 
2387 {
2388  assert (semaphore != nullptr);
2389  return (os_result_t) (reinterpret_cast<rtos::semaphore&> (*semaphore)).try_wait ();
2390 }
2391 
2402 {
2403  assert (semaphore != nullptr);
2404  return (os_result_t) (reinterpret_cast<rtos::semaphore&> (*semaphore)).timed_wait (
2405  timeout);
2406 }
2407 
2418 {
2419  assert (semaphore != nullptr);
2420  return (os_semaphore_count_t) (reinterpret_cast<rtos::semaphore&> (*semaphore)).value ();
2421 }
2422 
2433 {
2434  assert (semaphore != nullptr);
2435  return (os_result_t) (reinterpret_cast<rtos::semaphore&> (*semaphore)).reset ();
2436 }
2437 
2448 {
2449  assert (semaphore != nullptr);
2450  return (os_semaphore_count_t) (reinterpret_cast<rtos::semaphore&> (*semaphore)).initial_value ();
2451 }
2452 
2463 {
2464  assert (semaphore != nullptr);
2465  return (os_semaphore_count_t) (reinterpret_cast<rtos::semaphore&> (*semaphore)).max_value ();
2466 }
2467 
2468 // ----------------------------------------------------------------------------
2469 
2478 void
2480 {
2481  assert (attr != nullptr);
2482  new (attr) memory_pool::attributes ();
2483 }
2484 
2495 void
2496 os_mempool_construct (os_mempool_t* mempool, const char* name, size_t blocks,
2497  size_t block_size_bytes, const os_mempool_attr_t* attr)
2498 {
2499  assert (mempool != nullptr);
2500  if (attr == nullptr)
2501  {
2503  }
2504  new (mempool) memory_pool (name, blocks, block_size_bytes,
2505  (const memory_pool::attributes&) *attr);
2506 }
2507 
2518 void
2520 {
2521  assert (mempool != nullptr);
2522  (reinterpret_cast<memory_pool&> (*mempool)).~memory_pool ();
2523 }
2524 
2539 os_mempool_t*
2540 os_mempool_new (const char* name, size_t blocks, size_t block_size_bytes,
2541  const os_mempool_attr_t* attr)
2542 {
2543  if (attr == nullptr)
2544  {
2546  }
2547  return reinterpret_cast<os_mempool_t*> (new memory_pool (
2548  name, blocks, block_size_bytes, (const memory_pool::attributes&) *attr));
2549 }
2550 
2565 void
2567 {
2568  assert (mempool != nullptr);
2569  delete reinterpret_cast<memory_pool*> (mempool);
2570 }
2571 
2580 const char*
2582 {
2583  assert (mempool != nullptr);
2584  return (reinterpret_cast<memory_pool&> (*mempool)).name ();
2585 }
2586 
2595 void*
2597 {
2598  assert (mempool != nullptr);
2599  return (reinterpret_cast<memory_pool&> (*mempool)).alloc ();
2600 }
2601 
2610 void*
2612 {
2613  assert (mempool != nullptr);
2614  return (reinterpret_cast<memory_pool&> (*mempool)).try_alloc ();
2615 }
2616 
2625 void*
2627 {
2628  assert (mempool != nullptr);
2629  return (reinterpret_cast<memory_pool&> (*mempool)).timed_alloc (timeout);
2630 }
2631 
2641 os_mempool_free (os_mempool_t* mempool, void* block)
2642 {
2643  assert (mempool != nullptr);
2644  return (os_result_t) (reinterpret_cast<memory_pool&> (*mempool)).free (block);
2645 }
2646 
2655 size_t
2657 {
2658  assert (mempool != nullptr);
2659  return (reinterpret_cast<memory_pool&> (*mempool)).capacity ();
2660 }
2661 
2670 size_t
2672 {
2673  assert (mempool != nullptr);
2674  return (reinterpret_cast<memory_pool&> (*mempool)).count ();
2675 }
2676 
2685 size_t
2687 {
2688  assert (mempool != nullptr);
2689  return (reinterpret_cast<memory_pool&> (*mempool)).block_size ();
2690 }
2691 
2700 bool
2702 {
2703  assert (mempool != nullptr);
2704  return (reinterpret_cast<memory_pool&> (*mempool)).empty ();
2705 }
2706 
2715 bool
2717 {
2718  assert (mempool != nullptr);
2719  return (reinterpret_cast<memory_pool&> (*mempool)).full ();
2720 }
2721 
2732 {
2733  assert (mempool != nullptr);
2734  return (os_result_t) (reinterpret_cast<memory_pool&> (*mempool)).reset ();
2735 }
2736 
2745 void*
2747 {
2748  assert (mempool != nullptr);
2749  return (void*) (reinterpret_cast<memory_pool&> (*mempool)).pool ();
2750 }
2751 
2752 // --------------------------------------------------------------------------
2753 
2762 void
2764 {
2765  assert (attr != nullptr);
2766  new (attr) message_queue::attributes ();
2767 }
2768 
2779 void
2780 os_mqueue_construct (os_mqueue_t* mqueue, const char* name, size_t msgs,
2781  size_t msg_size_bytes, const os_mqueue_attr_t* attr)
2782 {
2783  assert (mqueue != nullptr);
2784  if (attr == nullptr)
2785  {
2787  }
2788  new (mqueue) message_queue (name, msgs, msg_size_bytes,
2789  (const message_queue::attributes&) *attr);
2790 }
2791 
2802 void
2804 {
2805  assert (mqueue != nullptr);
2806  (reinterpret_cast<message_queue&> (*mqueue)).~message_queue ();
2807 }
2808 
2823 os_mqueue_t*
2824 os_mqueue_new (const char* name, size_t msgs, size_t msg_size_bytes,
2825  const os_mqueue_attr_t* attr)
2826 {
2827  if (attr == nullptr)
2828  {
2830  }
2831  return reinterpret_cast<os_mqueue_t*> (new message_queue (
2832  name, msgs, msg_size_bytes, (const message_queue::attributes&) *attr));
2833 }
2834 
2849 void
2851 {
2852  assert (mqueue != nullptr);
2853  delete reinterpret_cast<message_queue*> (mqueue);
2854 }
2855 
2864 const char*
2866 {
2867  assert (mqueue != nullptr);
2868  return (reinterpret_cast<message_queue&> (*mqueue)).name ();
2869 }
2870 
2880 os_mqueue_send (os_mqueue_t* mqueue, const void* msg, size_t nbytes,
2881  os_mqueue_prio_t mprio)
2882 {
2883  assert (mqueue != nullptr);
2884  return (os_result_t) (reinterpret_cast<message_queue&> (*mqueue)).send (
2885  msg, nbytes, mprio);
2886 }
2887 
2897 os_mqueue_try_send (os_mqueue_t* mqueue, const void* msg, size_t nbytes,
2898  os_mqueue_prio_t mprio)
2899 {
2900  assert (mqueue != nullptr);
2901  return (os_result_t) (reinterpret_cast<message_queue&> (*mqueue)).try_send (
2902  msg, nbytes, mprio);
2903 }
2904 
2914 os_mqueue_timed_send (os_mqueue_t* mqueue, const void* msg, size_t nbytes,
2915  os_clock_duration_t timeout, os_mqueue_prio_t mprio)
2916 {
2917  assert (mqueue != nullptr);
2918  return (os_result_t) (reinterpret_cast<message_queue&> (*mqueue)).timed_send (
2919  msg, nbytes, timeout, mprio);
2920 }
2921 
2931 os_mqueue_receive (os_mqueue_t* mqueue, void* msg, size_t nbytes,
2932  os_mqueue_prio_t* mprio)
2933 {
2934  assert (mqueue != nullptr);
2935  return (os_result_t) (reinterpret_cast<message_queue&> (*mqueue)).receive (
2936  msg, nbytes, mprio);
2937 }
2938 
2948 os_mqueue_try_receive (os_mqueue_t* mqueue, void* msg, size_t nbytes,
2949  os_mqueue_prio_t* mprio)
2950 {
2951  assert (mqueue != nullptr);
2952  return (os_result_t) (reinterpret_cast<message_queue&> (*mqueue)).try_receive (
2953  msg, nbytes, mprio);
2954 }
2955 
2965 os_mqueue_timed_receive (os_mqueue_t* mqueue, void* msg, size_t nbytes,
2966  os_clock_duration_t timeout, os_mqueue_prio_t* mprio)
2967 {
2968  assert (mqueue != nullptr);
2969  return (os_result_t) (reinterpret_cast<message_queue&> (*mqueue)).timed_receive (
2970  msg, nbytes, timeout, mprio);
2971 }
2972 
2981 size_t
2983 {
2984  assert (mqueue != nullptr);
2985  return (reinterpret_cast<message_queue&> (*mqueue)).length ();
2986 }
2987 
2996 size_t
2998 {
2999  assert (mqueue != nullptr);
3000  return (reinterpret_cast<message_queue&> (*mqueue)).capacity ();
3001 }
3002 
3011 size_t
3013 {
3014  assert (mqueue != nullptr);
3015  return (reinterpret_cast<message_queue&> (*mqueue)).msg_size ();
3016 }
3017 
3026 bool
3028 {
3029  assert (mqueue != nullptr);
3030  return (reinterpret_cast<message_queue&> (*mqueue)).empty ();
3031 }
3032 
3041 bool
3043 {
3044  assert (mqueue != nullptr);
3045  return (reinterpret_cast<message_queue&> (*mqueue)).full ();
3046 }
3047 
3058 {
3059  assert (mqueue != nullptr);
3060  return (os_result_t) (reinterpret_cast<message_queue&> (*mqueue)).reset ();
3061 }
3062 
3063 // --------------------------------------------------------------------------
3064 
3073 void
3075 {
3076  assert (attr != nullptr);
3077  new (attr) event_flags::attributes ();
3078 }
3079 
3090 void
3091 os_evflags_construct (os_evflags_t* evflags, const char* name,
3092  const os_evflags_attr_t* attr)
3093 {
3094  assert (evflags != nullptr);
3095  if (attr == nullptr)
3096  {
3098  }
3099  new (evflags) event_flags (name, (const event_flags::attributes&) *attr);
3100 }
3101 
3112 void
3114 {
3115  assert (evflags != nullptr);
3116  (reinterpret_cast<event_flags&> (*evflags)).~event_flags ();
3117 }
3118 
3133 os_evflags_t*
3134 os_evflags_new (const char* name, const os_evflags_attr_t* attr)
3135 {
3136  if (attr == nullptr)
3137  {
3139  }
3140  return reinterpret_cast<os_evflags_t*> (new event_flags (
3141  name, (const event_flags::attributes&) *attr));
3142 }
3143 
3158 void
3160 {
3161  assert (evflags != nullptr);
3162  delete reinterpret_cast<event_flags*> (evflags);
3163 }
3164 
3173 const char*
3175 {
3176  assert (evflags != nullptr);
3177  return (reinterpret_cast<event_flags&> (*evflags)).name ();
3178 }
3179 
3190  os_flags_mask_t* oflags, os_flags_mode_t mode)
3191 {
3192  assert (evflags != nullptr);
3193  return (os_result_t) (reinterpret_cast<event_flags&> (*evflags)).wait (mask,
3194  oflags,
3195  mode);
3196 }
3197 
3208  os_flags_mask_t* oflags, os_flags_mode_t mode)
3209 {
3210  assert (evflags != nullptr);
3211  return (os_result_t) (reinterpret_cast<event_flags&> (*evflags)).try_wait (
3212  mask, oflags, mode);
3213 }
3214 
3225  os_clock_duration_t timeout, os_flags_mask_t* oflags,
3226  os_flags_mode_t mode)
3227 {
3228  assert (evflags != nullptr);
3229  return (os_result_t) (reinterpret_cast<event_flags&> (*evflags)).timed_wait (
3230  mask, timeout, oflags, mode);
3231 }
3232 
3243  os_flags_mask_t* oflags)
3244 {
3245  assert (evflags != nullptr);
3246  return (os_result_t) (reinterpret_cast<event_flags&> (*evflags)).raise (
3247  mask, oflags);
3248 }
3249 
3260  os_flags_mask_t* oflags)
3261 {
3262  assert (evflags != nullptr);
3263  return (os_result_t) (reinterpret_cast<event_flags&> (*evflags)).clear (
3264  mask, oflags);
3265 }
3266 
3277  os_flags_mode_t mode)
3278 {
3279  assert (evflags != nullptr);
3280  return (os_flags_mask_t) (reinterpret_cast<event_flags&> (*evflags)).get (
3281  mask, mode);
3282 }
3283 
3292 bool
3294 {
3295  assert (evflags != nullptr);
3296  return (reinterpret_cast<event_flags&> (*evflags)).waiting ();
3297 }
3298 
3299 // --------------------------------------------------------------------------
3300 
3309 os_memory_t*
3311 {
3312  return reinterpret_cast<os_memory_t*> (rtos::memory::get_default_resource ());
3313 }
3314 
3324 void*
3325 os_memory_allocate (os_memory_t* memory, size_t bytes, size_t alignment)
3326 {
3327  assert (memory != nullptr);
3328  return (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).allocate (
3329  bytes, alignment);
3330 }
3331 
3341 void
3342 os_memory_deallocate (os_memory_t* memory, void* addr, size_t bytes,
3343  size_t alignment)
3344 {
3345  assert (memory != nullptr);
3346  (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).deallocate (
3347  addr, bytes, alignment);
3348 }
3349 
3359 void
3361 {
3362  assert (memory != nullptr);
3363  (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).reset ();
3364 }
3365 
3375 bool
3377 {
3378  assert (memory != nullptr);
3379  return (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).coalesce ();
3380 }
3381 
3391 size_t
3393 {
3394  assert (memory != nullptr);
3395  return (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).total_bytes ();
3396 }
3397 
3407 size_t
3409 {
3410  assert (memory != nullptr);
3411  return (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).allocated_bytes ();
3412 }
3413 
3423 size_t
3425 {
3426  assert (memory != nullptr);
3427  return (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).free_bytes ();
3428 }
3429 
3439 size_t
3441 {
3442  assert (memory != nullptr);
3443  return (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).allocated_chunks ();
3444 }
3445 
3455 size_t
3457 {
3458  assert (memory != nullptr);
3459  return (reinterpret_cast<rtos::memory::memory_resource&> (*memory)).free_chunks ();
3460 }
3461 
3462 // ****************************************************************************
3463 // ***** Legacy CMSIS RTOS implementation *****
3464 
3466 
3467 // ----------------------------------------------------------------------------
3468 // ==== Kernel Control Functions ====
3469 
3492 osStatus
3494 {
3496  {
3497  return osErrorISR;
3498  }
3499 
3501  return osOK;
3502 }
3503 
3514 osStatus
3516 {
3517  scheduler::start ();
3518  // In µOS++ this does not return.
3519 }
3520 
3529 int32_t
3531 {
3532  return scheduler::started () ? 1 : 0;
3533 }
3534 
3535 #if (defined (osFeature_SysTick) && (osFeature_SysTick != 0))
3536 
3550 uint32_t
3551 osKernelSysTick (void)
3552 {
3553  // Get the current SysTick timestamp down to cpu cycles.
3554  return static_cast<uint32_t> (hrclock.now ());
3555 }
3556 
3557 #endif // System Timer available
3558 
3559 // ----------------------------------------------------------------------------
3560 // ==== Thread Management ====
3561 
3576 osThreadId
3577 osThreadCreate (const osThreadDef_t* thread_def, void* args)
3578 {
3580  {
3581  return nullptr;
3582  }
3583 
3584  if (thread_def == nullptr)
3585  {
3586  return nullptr;
3587  }
3588 
3589  thread::attributes attr;
3590  attr.th_priority = thread_def->tpriority;
3591  attr.th_stack_size_bytes = thread_def->stacksize;
3592 
3593  // Creating thread with invalid priority should fail (validator requirement).
3594  if (thread_def->tpriority >= osPriorityError)
3595  {
3596  return nullptr;
3597  }
3598 
3599  // Find a free slot in the tread definitions array.
3600  for (uint32_t i = 0; i < thread_def->instances; ++i)
3601  {
3602  thread* th = (thread*) &thread_def->data[i];
3603  if (th->state () == thread::state::undefined
3604  || th->state () == thread::state::destroyed)
3605  {
3606  if (attr.th_stack_size_bytes > 0)
3607  {
3608  attr.th_stack_address = &thread_def->stack[(i)
3609  * ((thread_def->stacksize + sizeof(uint64_t) - 1)
3610  / sizeof(uint64_t))];
3611  }
3612  new (th) thread (thread_def->name,
3613  (thread::func_t) thread_def->pthread, args, attr);
3614 
3615  // No need to yield here, already done by constructor.
3616  return reinterpret_cast<osThreadId> (th);
3617  }
3618  }
3619  return nullptr;
3620 }
3621 
3628 osThreadId
3630 {
3632  {
3633  return nullptr;
3634  }
3635 
3636  return reinterpret_cast<osThreadId> (&this_thread::thread ());
3637 }
3638 
3650 osStatus
3652 {
3654  {
3655  return osErrorISR;
3656  }
3657 
3658  if (thread_id == nullptr)
3659  {
3660  return osErrorParameter;
3661  }
3662 
3663  thread::state_t state =
3664  (reinterpret_cast<rtos::thread&> (*thread_id)).state ();
3665  if (state == thread::state::undefined)
3666  {
3667  return osErrorResource;
3668  }
3669 
3670  if ((reinterpret_cast<thread*> (thread_id)) == &this_thread::thread ())
3671  {
3672  this_thread::exit ();
3673  /* NOTREACHED */
3674  }
3675  else
3676  {
3677  (reinterpret_cast<rtos::thread&> (*thread_id)).kill ();
3678  }
3679 
3680  return osOK;
3681 }
3682 
3691 osStatus
3693 {
3695  {
3696  return osErrorISR;
3697  }
3698 
3699  this_thread::yield ();
3700 
3701  return osOK;
3702 }
3703 
3712 osStatus
3714 {
3716  {
3717  return osErrorISR;
3718  }
3719 
3720  if (thread_id == nullptr)
3721  {
3722  return osErrorParameter;
3723  }
3724 
3725  thread::state_t state =
3726  (reinterpret_cast<rtos::thread&> (*thread_id)).state ();
3727  if (state == thread::state::undefined || state >= thread::state::destroyed)
3728  {
3729  return osErrorResource;
3730  }
3731 
3732  if (priority < osPriorityIdle || priority >= osPriorityError)
3733  {
3734  return osErrorValue;
3735  }
3736 
3737  // Call C++ mutator.
3738  thread::priority_t prio = static_cast<thread::priority_t> (priority);
3739  result_t res =
3740  ((reinterpret_cast<rtos::thread&> (*thread_id)).priority (prio));
3741 
3742  // A mandatory yield is needed here, must be done
3743  // by the implementation.
3744 
3745  if (res == result::ok)
3746  {
3747  return osOK;
3748  }
3749  else if (res == EINVAL)
3750  {
3751  return osErrorValue;
3752  }
3753  else
3754  {
3755  return osErrorOS;
3756  }
3757 }
3758 
3766 osPriority
3768 {
3770  {
3771  return osPriorityError;
3772  }
3773 
3774  if (thread_id == nullptr)
3775  {
3776  return osPriorityError;
3777  }
3778 
3779  // Call C++ accessor.
3780  thread::priority_t prio =
3781  (reinterpret_cast<rtos::thread&> (*thread_id)).priority ();
3782  return static_cast<osPriority> (prio);
3783 }
3784 
3785 // ----------------------------------------------------------------------------
3786 // ==== Generic Wait Functions ====
3787 
3801 osStatus
3802 osDelay (uint32_t millisec)
3803 {
3805  {
3806  return osErrorISR;
3807  }
3808 
3809  result_t res = sysclock.sleep_for (
3810  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)));
3811 
3812  if (res == ETIMEDOUT)
3813  {
3814  return osEventTimeout;
3815  }
3816  else
3817  {
3818  return osErrorOS;
3819  }
3820 }
3821 
3822 #if (defined (osFeature_Wait) && (osFeature_Wait != 0))
3823 
3824 #pragma GCC diagnostic push
3825 #pragma GCC diagnostic ignored "-Waggregate-return"
3826 
3844 osEvent
3845 osWait (uint32_t millisec)
3846 {
3847  osEvent event;
3848 
3850  {
3851  event.status = osErrorISR;
3852  return event;
3853  }
3854 
3855  result_t res = sysclock.wait_for (
3856  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)));
3857 
3858  // TODO: return events
3859  if (res == ETIMEDOUT)
3860  {
3861  event.status = osEventTimeout;
3862  }
3863  else
3864  {
3865  event.status = osErrorOS;
3866  }
3867 
3868  return event;
3869 }
3870 
3871 #pragma GCC diagnostic pop
3872 
3873 #endif // Generic Wait available
3874 
3875 // ----------------------------------------------------------------------------
3876 // ==== Timer Management Functions ====
3877 
3886 osTimerId
3887 osTimerCreate (const osTimerDef_t* timer_def, os_timer_type type, void* args)
3888 {
3890  {
3891  return nullptr;
3892  }
3893 
3894  if (timer_def == nullptr)
3895  {
3896  return nullptr;
3897  }
3898 
3899  timer::attributes attr;
3900  attr.tm_type = (timer::type_t) type;
3901 
3902  new ((void*) timer_def->data) timer (timer_def->name,
3903  (timer::func_t) timer_def->ptimer,
3904  (timer::func_args_t) args, attr);
3905 
3906  return reinterpret_cast<osTimerId> (timer_def->data);
3907 }
3908 
3915 osStatus
3916 osTimerStart (osTimerId timer_id, uint32_t millisec)
3917 {
3919  {
3920  return osErrorISR;
3921  }
3922 
3923  if (timer_id == nullptr)
3924  {
3925  return osErrorParameter;
3926  }
3927 
3928  result_t res = (reinterpret_cast<rtos::timer&> (*timer_id)).start (
3929  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)));
3930 
3931  if (res == result::ok)
3932  {
3933  return osOK;
3934  }
3935  else
3936  {
3937  return osErrorOS;
3938  }
3939 }
3940 
3946 osStatus
3948 {
3950  {
3951  return osErrorISR;
3952  }
3953 
3954  if (timer_id == nullptr)
3955  {
3956  return osErrorParameter;
3957  }
3958 
3959  result_t res = (reinterpret_cast<rtos::timer&> (*timer_id)).stop ();
3960  if (res == result::ok)
3961  {
3962  return osOK;
3963  }
3964  else if (res == EAGAIN)
3965  {
3966  return osErrorResource;
3967  }
3968  else
3969  {
3970  return osErrorOS;
3971  }
3972 }
3973 
3980 osStatus
3982 {
3984  {
3985  return osErrorISR;
3986  }
3987 
3988  if (timer_id == nullptr)
3989  {
3990  return osErrorParameter;
3991  }
3992 
3993  (reinterpret_cast<rtos::timer&> (*timer_id)).~timer ();
3994  return osOK;
3995 }
3996 
3997 // ----------------------------------------------------------------------------
3998 // ==== Signal Management ====
3999 
4006 int32_t
4007 osSignalSet (osThreadId thread_id, int32_t signals)
4008 {
4009  if (thread_id == nullptr)
4010  {
4011  return (int32_t) 0x80000000;
4012  }
4013 
4014  if (signals == (int32_t) 0x80000000)
4015  {
4016  return (int32_t) 0x80000000;
4017  }
4018 
4019  flags::mask_t osig;
4020  ((thread*) (thread_id))->flags_raise ((flags::mask_t) signals, &osig);
4021  return (int32_t) osig;
4022 }
4023 
4030 int32_t
4031 osSignalClear (osThreadId thread_id, int32_t signals)
4032 {
4033  if (thread_id == nullptr)
4034  {
4035  return (int32_t) 0x80000000;
4036  }
4037 
4038  if (interrupts::in_handler_mode () || (signals == 0))
4039  {
4040  return (int32_t) 0x80000000;
4041  }
4042 
4043  flags::mask_t sig;
4044 
4045 #if defined(OS_INCLUDE_RTOS_THREAD_PUBLIC_FLAGS_CLEAR)
4046  ((thread*) (thread_id))->flags_clear ((flags::mask_t) signals, &sig);
4047 #else
4048  assert(((thread* ) (thread_id)) == &this_thread::thread ());
4049  // IGNORE THREAD ID!
4050  this_thread::flags_clear ((flags::mask_t) signals, &sig);
4051 #endif
4052  return (int32_t) sig;
4053 }
4054 
4055 #pragma GCC diagnostic push
4056 #pragma GCC diagnostic ignored "-Waggregate-return"
4057 
4080 osEvent
4081 osSignalWait (int32_t signals, uint32_t millisec)
4082 {
4083  osEvent event;
4084 
4086  {
4087  event.status = osErrorISR;
4088  return event;
4089  }
4090 
4091  if ((uint32_t) signals & 0x80000000)
4092  {
4093  event.status = osErrorValue;
4094  return event;
4095  }
4096 
4097  result_t res;
4098  if (millisec == osWaitForever)
4099  {
4100  res = this_thread::flags_wait ((flags::mask_t) signals,
4101  (flags::mask_t*) &event.value.signals);
4102  }
4103  else if (millisec == 0)
4104  {
4105  res = this_thread::flags_try_wait ((flags::mask_t) signals,
4106  (flags::mask_t*) &event.value.signals);
4107  }
4108  else
4109  {
4111  (flags::mask_t) signals,
4112  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)),
4113  (flags::mask_t*) &event.value.signals);
4114  }
4115 
4116  if (res == result::ok)
4117  {
4118  event.status = osEventSignal;
4119  }
4120  else if (res == EWOULDBLOCK)
4121  {
4122  event.status = osOK; // Only for try_flags_wait().
4123  }
4124  else if (res == ETIMEDOUT)
4125  {
4126  event.status = osEventTimeout; // Only for timed_flags_wait().
4127  }
4128  else if (res == EPERM)
4129  {
4130  event.status = osErrorISR;
4131  }
4132  else if (res == EINVAL)
4133  {
4134  event.status = osErrorValue;
4135  }
4136  else
4137  {
4138  event.status = osErrorOS;
4139  }
4140 
4141  return event;
4142 }
4143 
4144 #pragma GCC diagnostic pop
4145 
4146 // ----------------------------------------------------------------------------
4147 
4148 // ==== Mutex Management ====
4149 
4156 osMutexId
4157 osMutexCreate (const osMutexDef_t* mutex_def)
4158 {
4160  {
4161  return nullptr;
4162  }
4163 
4164  if (mutex_def == nullptr)
4165  {
4166  return nullptr;
4167  }
4168 
4169  mutex::attributes attr;
4172 
4173  new ((void*) mutex_def->data) mutex (mutex_def->name, attr);
4174 
4175  return reinterpret_cast<osMutexId> (mutex_def->data);
4176 }
4177 
4196 osStatus
4197 osMutexWait (osMutexId mutex_id, uint32_t millisec)
4198 {
4200  {
4201  return osErrorISR;
4202  }
4203 
4204  if (mutex_id == nullptr)
4205  {
4206  return osErrorParameter;
4207  }
4208 
4209  result_t ret;
4210  if (millisec == osWaitForever)
4211  {
4212  ret = (reinterpret_cast<rtos::mutex&> (*mutex_id)).lock ();
4213  // osErrorResource:
4214  }
4215  else if (millisec == 0)
4216  {
4217  ret = (reinterpret_cast<rtos::mutex&> (*mutex_id)).try_lock ();
4218  }
4219  else
4220  {
4221  ret = (reinterpret_cast<rtos::mutex&> (*mutex_id)).timed_lock (
4222  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)));
4223  // osErrorTimeoutResource:
4224  }
4225 
4226  if (ret == result::ok)
4227  {
4228  // osOK: the mutex has been obtained.
4229  return osOK;
4230  }
4231  else if (ret == EWOULDBLOCK)
4232  {
4233  // The mutex could not be obtained when no timeout was specified.
4234  // Only for try_lock().
4235  return osErrorResource;
4236  }
4237  else if (ret == ENOTRECOVERABLE)
4238  {
4239  // The mutex could not be obtained when no timeout was specified.
4240  // Only for lock().
4241  return osErrorResource;
4242  }
4243  else if (ret == ETIMEDOUT)
4244  {
4245  // The mutex could not be obtained in the given time.
4246  // Only for timed_lock().
4247  return osErrorTimeoutResource;
4248  }
4249  else
4250  {
4251  return osErrorOS;
4252  }
4253 
4254  /* NOTREACHED */
4255 }
4256 
4265 osStatus
4267 {
4269  {
4270  return osErrorISR;
4271  }
4272 
4273  if (mutex_id == nullptr)
4274  {
4275  return osErrorParameter;
4276  }
4277 
4278  result_t res;
4279  res = (reinterpret_cast<rtos::mutex&> (*mutex_id)).unlock ();
4280 
4281  if (res == result::ok)
4282  {
4283  return osOK;
4284  }
4285  else if ((res == EPERM) || (res == ENOTRECOVERABLE))
4286  {
4287  return osErrorResource;
4288  }
4289  else
4290  {
4291  return osErrorOS;
4292  }
4293 }
4294 
4304 osStatus
4306 {
4308  {
4309  return osErrorISR;
4310  }
4311 
4312  if (mutex_id == nullptr)
4313  {
4314  return osErrorParameter;
4315  }
4316 
4317  (reinterpret_cast<rtos::mutex&> (*mutex_id)).~mutex ();
4318  return osOK;
4319 }
4320 
4321 // ----------------------------------------------------------------------------
4322 
4323 // ==== Semaphore Management Functions ====
4324 
4325 #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0))
4326 
4337 osSemaphoreCreate (const osSemaphoreDef_t* semaphore_def, int32_t count)
4338 {
4340  {
4341  return nullptr;
4342  }
4343 
4344  if (semaphore_def == nullptr)
4345  {
4346  return nullptr;
4347  }
4348 
4349  semaphore::attributes attr;
4350  attr.sm_initial_value = (semaphore::count_t) count;
4351  // The logic is very strange, the CMSIS expects both the max-count to be the
4352  // same as count, and also to accept a count of 0, which leads to
4353  // useless semaphores. We patch this behaviour in the wrapper, the main
4354  // object uses a more realistic max_value.
4355  attr.sm_max_value = (semaphore::count_t) (
4356  count == 0 ? osFeature_Semaphore : count);
4357 
4358  new ((void*) semaphore_def->data) semaphore (semaphore_def->name, attr);
4359 
4360  return reinterpret_cast<osSemaphoreId> (semaphore_def->data);
4361 }
4362 
4385 int32_t
4386 osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec)
4387 {
4389  {
4390  return -1;
4391  }
4392 
4393  if (semaphore_id == nullptr)
4394  {
4395  return -1;
4396  }
4397 
4398  result_t res;
4399  if (millisec == osWaitForever)
4400  {
4401  res = (reinterpret_cast<rtos::semaphore&> (*semaphore_id)).wait ();
4402  }
4403  else if (millisec == 0)
4404  {
4405  res = (reinterpret_cast<rtos::semaphore&> (*semaphore_id)).try_wait ();
4406  if (res == EWOULDBLOCK)
4407  {
4408  return 0;
4409  }
4410  }
4411  else
4412  {
4413  res = (reinterpret_cast<rtos::semaphore&> (*semaphore_id)).timed_wait (
4414  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)));
4415  if (res == ETIMEDOUT)
4416  {
4417  return 0;
4418  }
4419  }
4420 
4421  if (res == 0)
4422  {
4423  int count =
4424  (int32_t) (reinterpret_cast<rtos::semaphore&> (*semaphore_id)).value ();
4425  return count + 1;
4426  }
4427  else
4428  {
4429  return -1;
4430  }
4431 }
4432 
4440 osStatus
4441 osSemaphoreRelease (osSemaphoreId semaphore_id)
4442 {
4443  if (semaphore_id == nullptr)
4444  {
4445  return osErrorParameter;
4446  }
4447 
4448  if ((reinterpret_cast<rtos::semaphore&> (*semaphore_id)).initial_value ()
4449  == 0)
4450  {
4451  return osErrorResource;
4452  }
4453 
4454  result_t res = (reinterpret_cast<rtos::semaphore&> (*semaphore_id)).post ();
4455 
4456  if (res == result::ok)
4457  {
4458  return osOK;
4459  }
4460  else if (res == EAGAIN)
4461  {
4462  return osErrorResource;
4463  }
4464  else
4465  {
4466  return osErrorOS;
4467  }
4468 }
4469 
4479 osStatus
4480 osSemaphoreDelete (osSemaphoreId semaphore_id)
4481 {
4483  {
4484  return osErrorISR;
4485  }
4486 
4487  if (semaphore_id == nullptr)
4488  {
4489  return osErrorParameter;
4490  }
4491 
4492  (reinterpret_cast<rtos::semaphore&> (*semaphore_id)).~semaphore ();
4493  return osOK;
4494 }
4495 
4496 #endif /* Semaphore available */
4497 
4498 // ----------------------------------------------------------------------------
4499 // ==== Memory Pool Management Functions ====
4500 
4501 #if (defined (osFeature_Pool) && (osFeature_Pool != 0))
4502 
4509 osPoolId
4510 osPoolCreate (const osPoolDef_t* pool_def)
4511 {
4513  {
4514  return nullptr;
4515  }
4516 
4517  if (pool_def == nullptr)
4518  {
4519  return nullptr;
4520  }
4521 
4523  attr.mp_pool_address = pool_def->pool;
4524  attr.mp_pool_size_bytes = pool_def->pool_sz;
4525 
4526  new ((void*) pool_def->data) memory_pool (pool_def->name,
4527  (std::size_t) pool_def->items,
4528  (std::size_t) pool_def->item_sz,
4529  attr);
4530 
4531  return reinterpret_cast<osPoolId> (pool_def->data);
4532 }
4533 
4540 void*
4541 osPoolAlloc (osPoolId pool_id)
4542 {
4543  if (pool_id == nullptr)
4544  {
4545  return nullptr;
4546  }
4547 
4548  return (reinterpret_cast<memory_pool&> (*pool_id)).try_alloc ();
4549 }
4550 
4557 void*
4558 osPoolCAlloc (osPoolId pool_id)
4559 {
4560  if (pool_id == nullptr)
4561  {
4562  return nullptr;
4563  }
4564 
4565  void* ret;
4566  ret = (reinterpret_cast<memory_pool&> (*pool_id)).try_alloc ();
4567  if (ret != nullptr)
4568  {
4569  memset (ret, 0,
4570  (reinterpret_cast<memory_pool&> (*pool_id)).block_size ());
4571  }
4572 
4573  return ret;
4574 }
4575 
4582 osStatus
4583 osPoolFree (osPoolId pool_id, void* block)
4584 {
4585  if (pool_id == nullptr)
4586  {
4587  return osErrorParameter;
4588  }
4589 
4590  if (block == nullptr)
4591  {
4592  return osErrorParameter;
4593  }
4594 
4595  result_t res;
4596  res = (reinterpret_cast<memory_pool&> (*pool_id)).free (block);
4597 
4598  if (res == result::ok)
4599  {
4600  return osOK;
4601  }
4602  else if (res == EINVAL)
4603  {
4604  return osErrorValue;
4605  }
4606  else
4607  {
4608  return osErrorOS;
4609  }
4610 }
4611 
4612 #endif /* Memory Pool Management available */
4613 
4614 // ----------------------------------------------------------------------------
4615 // ==== Message Queue Management Functions ====
4616 
4617 #if (defined (osFeature_MessageQ) && (osFeature_MessageQ != 0))
4618 
4626 osMessageCreate (const osMessageQDef_t* queue_def,
4627  osThreadId thread_id __attribute__((unused)))
4628 {
4630  {
4631  return nullptr;
4632  }
4633 
4634  if (queue_def == nullptr)
4635  {
4636  return nullptr;
4637  }
4638 
4640  attr.mq_queue_address = queue_def->queue;
4641  attr.mq_queue_size_bytes = queue_def->queue_sz;
4642 
4643  new ((void*) queue_def->data) message_queue (queue_def->name,
4644  (std::size_t) queue_def->items,
4645  (std::size_t) queue_def->item_sz,
4646  attr);
4647 
4648  return reinterpret_cast<osMessageQId> (queue_def->data);
4649 }
4650 
4667 osStatus
4668 osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec)
4669 {
4670 #pragma GCC diagnostic push
4671 #if defined ( __clang__ )
4672 #pragma clang diagnostic ignored "-Wint-to-pointer-cast"
4673 #endif
4674 
4675  if (queue_id == nullptr)
4676  {
4677  return osErrorParameter;
4678  }
4679 
4680  result_t res;
4681  if (millisec == osWaitForever)
4682  {
4684  {
4685  return osErrorParameter;
4686  }
4687  res = (reinterpret_cast<message_queue&> (*queue_id)).send (
4688  (const char*) &info, sizeof(uint32_t), 0);
4689  // osOK, osErrorResource, osErrorParameter
4690  }
4691  else if (millisec == 0)
4692  {
4693  res = (reinterpret_cast<message_queue&> (*queue_id)).try_send (
4694  (const char*) &info, sizeof(uint32_t), 0);
4695  // osOK, osErrorResource, osErrorParameter
4696  }
4697  else
4698  {
4700  {
4701  return osErrorParameter;
4702  }
4703  res = (reinterpret_cast<message_queue&> (*queue_id)).timed_send (
4704  (const char*) &info, sizeof(uint32_t),
4705  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)), 0);
4706  // osOK, osErrorTimeoutResource, osErrorParameter
4707  }
4708 
4709  if (res == result::ok)
4710  {
4711  // The message was put into the queue.
4712  return osOK;
4713  }
4714  else if (res == EWOULDBLOCK)
4715  {
4716  // No memory in the queue was available
4717  return osErrorResource;
4718  }
4719  else if (res == ETIMEDOUT)
4720  {
4721  // No memory in the queue was available during the given time limit.
4722  return osErrorTimeoutResource;
4723  }
4724  else if (res == EINVAL || res == EMSGSIZE)
4725  {
4726  // A parameter is invalid or outside of a permitted range.
4727  return osErrorParameter;
4728  }
4729  else
4730  {
4731  return osErrorOS;
4732  }
4733 
4734 #pragma GCC diagnostic pop
4735 }
4736 
4737 #pragma GCC diagnostic push
4738 #pragma GCC diagnostic ignored "-Waggregate-return"
4739 
4758 osEvent
4759 osMessageGet (osMessageQId queue_id, uint32_t millisec)
4760 {
4761  osEvent event;
4762  result_t res;
4763 
4764  if (queue_id == nullptr)
4765  {
4766  event.status = osErrorParameter;
4767  return event;
4768  }
4769 
4770  if (millisec == osWaitForever)
4771  {
4773  {
4774  event.status = osErrorParameter;
4775  return event;
4776  }
4777  res = (reinterpret_cast<message_queue&> (*queue_id)).receive (
4778  (char*) &event.value.v, sizeof(uint32_t), nullptr);
4779  // result::event_message;
4780  }
4781  else if (millisec == 0)
4782  {
4783  res = (reinterpret_cast<message_queue&> (*queue_id)).try_receive (
4784  (char*) &event.value.v, sizeof(uint32_t), nullptr);
4785  // result::event_message when message;
4786  // result::ok when no meessage
4787  }
4788  else
4789  {
4791  {
4792  event.status = osErrorParameter;
4793  return event;
4794  }
4795  res = (reinterpret_cast<message_queue&> (*queue_id)).timed_receive (
4796  (char*) &event.value.v, sizeof(uint32_t),
4797  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)), nullptr);
4798  // result::event_message when message;
4799  // result::event_timeout when timeout;
4800  }
4801 
4802  if (res == result::ok)
4803  {
4804  // Message received, value.p contains the pointer to message.
4805  event.status = osEventMessage;
4806  }
4807  else if (res == ETIMEDOUT)
4808  {
4809  // No message has arrived during the given timeout period.
4810  event.status = osEventTimeout;
4811  }
4812  else if (res == EINVAL || res == EMSGSIZE)
4813  {
4814  // A parameter is invalid or outside of a permitted range.
4815  event.status = osErrorParameter;
4816  }
4817  else if (res == EWOULDBLOCK)
4818  {
4819  // No message is available in the queue and no timeout was specified.
4820  event.status = osOK;
4821  }
4822  else
4823  {
4824  event.status = osErrorOS;
4825  }
4826 
4827  return event;
4828 }
4829 
4830 #pragma GCC diagnostic pop
4831 
4832 #endif /* Message Queues available */
4833 
4834 // ----------------------------------------------------------------------------
4835 // ==== Mail Queue Management Functions ====
4836 
4837 #if (defined (osFeature_MailQ) && (osFeature_MailQ != 0))
4838 
4845 osMailQId
4846 osMailCreate (const osMailQDef_t* mail_def,
4847  osThreadId thread_id __attribute__((unused)))
4848 {
4850  {
4851  return nullptr;
4852  }
4853 
4854  if (mail_def == nullptr)
4855  {
4856  return nullptr;
4857  }
4858 
4859  memory_pool::attributes pool_attr;
4860  pool_attr.mp_pool_address = mail_def->pool;
4861  pool_attr.mp_pool_size_bytes = mail_def->pool_sz;
4862  new ((void*) &mail_def->data->pool) memory_pool (
4863  mail_def->name, (std::size_t) mail_def->items,
4864  (std::size_t) mail_def->pool_item_sz, pool_attr);
4865 
4866  message_queue::attributes queue_attr;
4867  queue_attr.mq_queue_address = mail_def->queue;
4868  queue_attr.mq_queue_size_bytes = mail_def->queue_sz;
4869  new ((void*) &mail_def->data->queue) message_queue (
4870  mail_def->name, (std::size_t) mail_def->items,
4871  (std::size_t) mail_def->queue_item_sz, queue_attr);
4872 
4873  return (osMailQId) (mail_def->data);
4874 }
4875 
4899 void*
4900 osMailAlloc (osMailQId mail_id, uint32_t millisec)
4901 {
4902  if (mail_id == nullptr)
4903  {
4904  return nullptr;
4905  }
4906 
4907  void* ret = nullptr;
4908 
4909 #pragma GCC diagnostic push
4910 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
4911  if (millisec == osWaitForever)
4912  {
4914  {
4915  return nullptr;
4916  }
4917  ret = (reinterpret_cast<memory_pool&> (mail_id->pool)).alloc ();
4918  }
4919  else if (millisec == 0)
4920  {
4921  ret = (reinterpret_cast<memory_pool&> (mail_id->pool)).try_alloc ();
4922  }
4923  else
4924  {
4926  {
4927  return nullptr;
4928  }
4929  ret = (reinterpret_cast<memory_pool&> (mail_id->pool)).timed_alloc (
4930  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)));
4931  }
4932 #pragma GCC diagnostic pop
4933  return ret;
4934 }
4935 
4959 void*
4960 osMailCAlloc (osMailQId mail_id, uint32_t millisec)
4961 {
4962  void* ret = osMailAlloc (mail_id, millisec);
4963  if (ret != nullptr)
4964  {
4965 #pragma GCC diagnostic push
4966 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
4967  memset (ret, 0,
4968  (reinterpret_cast<memory_pool&> (mail_id->pool)).block_size ());
4969 #pragma GCC diagnostic pop
4970  }
4971  return ret;
4972 }
4973 
4981 osStatus
4982 osMailPut (osMailQId mail_id, void* mail)
4983 {
4984  if (mail_id == nullptr)
4985  {
4986  return osErrorParameter;
4987  }
4988  if (mail == nullptr)
4989  {
4990  return osErrorValue;
4991  }
4992 
4993  // Validate pointer.
4994  memory_pool* pool = reinterpret_cast<memory_pool*> (&mail_id->pool);
4995  if (((char*) mail < (char*) (pool->pool ()))
4996  || (((char*) mail)
4997  >= ((char*) (pool->pool ()) + pool->capacity () * pool->block_size ())))
4998  {
4999  return osErrorValue;
5000  }
5001 
5002  result_t res;
5003 #pragma GCC diagnostic push
5004 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
5005  res = (reinterpret_cast<message_queue&> (mail_id->queue)).try_send (
5006  (const char*) &mail, sizeof(void*), 0);
5007 #pragma GCC diagnostic pop
5008  if (res == result::ok)
5009  {
5010  return osOK;
5011  }
5012  else
5013  {
5014  return osErrorOS;
5015  }
5016 }
5017 
5018 #pragma GCC diagnostic push
5019 #pragma GCC diagnostic ignored "-Waggregate-return"
5020 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
5021 
5040 osEvent
5041 osMailGet (osMailQId mail_id, uint32_t millisec)
5042 {
5043  osEvent event;
5044  result_t res;
5045 
5046  if (mail_id == nullptr)
5047  {
5048  event.status = osErrorParameter;
5049  return event;
5050  }
5051 
5052  if (millisec == osWaitForever)
5053  {
5055  {
5056  event.status = osErrorParameter;
5057  return event;
5058  }
5059  res = (reinterpret_cast<message_queue&> ((mail_id->queue))).receive (
5060  (char*) &event.value.p, sizeof(void*), nullptr);
5061  // osEventMail for ok,
5062  }
5063  else if (millisec == 0)
5064  {
5065  res = (reinterpret_cast<message_queue&> (mail_id->queue)).try_receive (
5066  (char*) &event.value.p, sizeof(void*), nullptr);
5067  // osEventMail for ok,
5068  }
5069  else
5070  {
5072  {
5073  event.status = osErrorParameter;
5074  return event;
5075  }
5076  res = (reinterpret_cast<message_queue&> (mail_id->queue)).timed_receive (
5077  (char*) &event.value.p, sizeof(void*),
5078  clock_systick::ticks_cast ((uint64_t) (millisec * 1000u)), nullptr);
5079  // osEventMail for ok, osEventTimeout
5080  }
5081 
5082  if (res == result::ok)
5083  {
5084  // Mail received, value.p contains the pointer to mail content.
5085  event.status = osEventMail;
5086  }
5087  else if (res == EWOULDBLOCK)
5088  {
5089  // No mail is available in the queue and no timeout was specified.
5090  event.status = osOK;
5091  }
5092  else if (res == EINVAL || res == EMSGSIZE)
5093  {
5094  // A parameter is invalid or outside of a permitted range.
5095  event.status = osErrorParameter;
5096  }
5097  else if (res == ETIMEDOUT)
5098  {
5099  // No mail has arrived during the given timeout period.
5100  event.status = osEventTimeout;
5101  }
5102  else
5103  {
5104  event.status = osErrorOS;
5105  }
5106 
5107  return event;
5108 }
5109 
5110 #pragma GCC diagnostic pop
5111 
5118 osStatus
5119 osMailFree (osMailQId mail_id, void* mail)
5120 {
5121  if (mail_id == nullptr)
5122  {
5123  return osErrorParameter;
5124  }
5125  if (mail == nullptr)
5126  {
5127  return osErrorValue;
5128  }
5129 
5130  return osPoolFree (&(mail_id->pool), mail);
5131 }
5132 
5133 #endif /* Mail Queues available */
5134 
5135 // ----------------------------------------------------------------------------
5136 
5137 #pragma GCC diagnostic pop
Memory resource manager (abstract class).
Definition: os-memory.h:153
message_queue::size_t index_t
Type of list index storage.
Definition: os-mqueue.h:100
uint32_t queue_sz
Definition: cmsis_os.h:365
os_result_t os_evflags_clear(os_evflags_t *evflags, os_flags_mask_t mask, os_flags_mask_t *oflags)
Clear event flags.
osEvent osSignalWait(int32_t signals, uint32_t millisec)
Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
uint8_t type_t
Type of of variables holding timer run types.
Definition: os-timer.h:73
osStatus osTimerStop(osTimerId timer_id)
Stop the timer.
size_t os_thread_stack_set_min_size(size_t size_bytes)
Set the min stack size.
os_sched_state_t os_sched_set_locked(os_sched_state_t state)
Lock/unlock the scheduler.
Thread attributes.
Definition: os-thread.h:770
static const attributes initializer
Default thread initialiser.
Definition: os-thread.h:968
os_sched_state_t os_sched_unlock(void)
Unlock the scheduler.
Terminated and resources (like stack) released.
Definition: os-thread.h:378
os_result_t os_mqueue_try_send(os_mqueue_t *mqueue, const void *msg, size_t nbytes, os_mqueue_prio_t mprio)
Try to send a message to the queue.
type_t tm_type
Timer type attribute.
Definition: os-timer.h:192
osStatus osDelay(uint32_t millisec)
Time Delay.
port::scheduler::state_t unlock(void)
Unlock the scheduler.
Definition: os-sched.h:882
Check mutex behaviour.
Definition: os-c-decls.h:873
static const attributes initializer
Default condition variable initialiser.
Definition: os-condvar.h:116
static state_t enter(void)
Enter interrupts uncritical section.
Definition: os-sched.h:1179
const os_timer_attr_t * os_timer_attr_get_periodic(void)
Get a periodic timer attributes object instance.
port::clock::timestamp_t timestamp_t
Type of variables holding clock time stamps.
Definition: os-clocks.h:81
Timer definition structure contains timer parameters.
Definition: cmsis_os.h:294
void * os_mempool_timed_alloc(os_mempool_t *mempool, os_clock_duration_t timeout)
Allocate a memory block with timeout.
uint8_t os_mutex_protocol_t
Definition: os-c-decls.h:799
resource not available: a specified resource was not available.
Definition: cmsis_os.h:166
static std::size_t default_size(void)
Get the default stack size.
Definition: os-thread.h:2190
Thread definition structure contains startup information of a thread.
Definition: cmsis_os.h:277
void os_mqueue_delete(os_mqueue_t *mqueue)
Destruct the message queue object instance and deallocate it.
void os_timer_attr_init(os_timer_attr_t *attr)
Initialise the single shot timer attributes.
os_result_t os_mqueue_receive(os_mqueue_t *mqueue, void *msg, size_t nbytes, os_mqueue_prio_t *mprio)
Receive a message from the queue.
uint64_t * stack
Definition: cmsis_os.h:285
count_t sm_initial_value
Semaphore initial count value.
Definition: os-semaphore.h:147
class thread::stack * stack(void)
Get the interrupts stack.
Definition: os-core.cpp:562
void os_mutex_construct(os_mutex_t *mutex, const char *name, const os_mutex_attr_t *attr)
Construct a statically allocated mutex object instance.
port::scheduler::state_t lock(void)
Lock the scheduler.
Definition: os-sched.h:869
os_result_t os_thread_join(os_thread_t *thread, void **exit_ptr)
Wait for thread termination.
size_t os_memory_get_free_bytes(os_memory_t *memory)
Get the total size of free chunks.
os_semaphore_t * os_semaphore_counting_new(const char *name, const os_semaphore_count_t max_value, const os_semaphore_count_t initial_value)
Allocate a counting semaphore object instance and construct it.
function completed; signal event occurred.
Definition: cmsis_os.h:161
const char * os_mqueue_get_name(os_mqueue_t *mqueue)
Get the message queue name.
os_result_t os_thread_flags_raise(os_thread_t *thread, os_flags_mask_t mask, os_flags_mask_t *oflags)
Raise thread event flags.
uint8_t os_timer_type_t
Type of variables holding timer types.
Definition: os-c-decls.h:712
Normal mutex behaviour.
Definition: os-mutex.h:166
os_clock_t * os_clock_get_rtclock(void)
Get rtclock (the real-time clock).
void start(void)
Start the RTOS scheduler.
Definition: os-core.cpp:186
os_result_t os_mempool_reset(os_mempool_t *mempool)
Reset the memory pool.
void * func_args_t
Timer call back function arguments.
Definition: os-timer.h:61
void yield(void)
Yield execution to the next ready thread.
Definition: os-thread.cpp:1564
void os_semaphore_delete(os_semaphore_t *semaphore)
Destruct the semaphore object instance.
osStatus osSemaphoreDelete(osSemaphoreId semaphore_id)
Delete the semaphore.
No longer usable, but resources not yet released.
Definition: os-c-decls.h:316
void exit(void *exit_ptr=nullptr)
Terminate the current running thread.
Definition: os-thread.h:2039
uint32_t pool_item_sz
size of a pool item
Definition: cmsis_os.h:360
Thread context.
Definition: os-c-decls.h:427
static const attributes initializer_normal
Default normal mutex initialiser.
Definition: os-mutex.h:302
void os_semaphore_destruct(os_semaphore_t *semaphore)
Destruct the statically allocated semaphore object instance.
std::size_t capacity(void) const
Get memory pool capacity.
Definition: os-mempool.h:908
std::size_t mp_pool_size_bytes
Size of the user defined storage for the memory pool.
Definition: os-mempool.h:140
void os_irq_uncritical_exit(os_irq_state_t state)
Exit the interrupts uncritical section.
ssize_t send(int socket, const void *buffer, size_t length, int flags)
const char * os_mempool_get_name(os_mempool_t *mempool)
Get the memory pool name.
Mutex definition structure contains setup information for a mutex.
Definition: cmsis_os.h:305
os_result_t os_mempool_free(os_mempool_t *mempool, void *block)
Free the memory block.
os_iterator_t os_children_threads_iter_begin(os_thread_t *thread)
Get the beginning of the list of children threads.
os_result_t os_semaphore_try_wait(os_semaphore_t *semaphore)
Try to lock the semaphore.
os_thread_stack_t * os_thread_get_stack(os_thread_t *thread)
Get the thread context stack.
state_t locked(state_t state)
Lock/unlock the scheduler.
Definition: os-sched.h:899
Message queue object storage.
Definition: os-c-decls.h:1307
static const attributes initializer
Default event flags initialiser.
Definition: os-evflags.h:120
uint8_t protocol_t
Type of variables holding mutex protocols.
Definition: os-mutex.h:61
os_condvar_t * os_condvar_new(const char *name, const os_condvar_attr_t *attr)
Allocate a condition variable object instance and construct it.
Event structure contains detailed information about an event.
Definition: cmsis_os.h:374
osStatus osThreadYield(void)
Yield control.
void * os_thread_func_args_t
Type of thread function arguments.
Definition: os-c-decls.h:331
value of a parameter is out of range.
Definition: cmsis_os.h:172
os_mutex_type_t os_mutex_get_type(os_mutex_t *mutex)
Get the mutex type.
os_result_t os_clock_sleep_for(os_clock_t *clock, os_clock_duration_t duration)
Sleep for a relative duration.
os_result_t os_mqueue_timed_receive(os_mqueue_t *mqueue, void *msg, size_t nbytes, os_clock_duration_t timeout, os_mqueue_prio_t *mprio)
Receive a message from the queue with timeout.
void os_timer_construct(os_timer_t *timer, const char *name, os_timer_func_t function, os_timer_func_args_t args, const os_timer_attr_t *attr)
Construct a statically allocated timer object instance.
void os_timer_delete(os_timer_t *timer)
Destruct the timer object instance and deallocate it.
POSIX compliant semaphore.
Definition: os-semaphore.h:53
Thread attributes.
Definition: os-c-decls.h:489
Run periodically.
Definition: os-timer.h:92
size_t os_memory_get_allocated_bytes(os_memory_t *memory)
Get the total size of allocated chunks.
void os_evflags_delete(os_evflags_t *evflags)
Destruct the event flags object instance and deallocate it.
size_t os_mempool_get_count(os_mempool_t *mempool)
Get blocks count.
os_mutex_t * os_mutex_new(const char *name, const os_mutex_attr_t *attr)
Allocate a mutex object instance and construct it.
static void exit(state_t state)
Exit interrupts uncritical section.
Definition: os-sched.h:1191
function completed; no error or event occurred.
Definition: cmsis_os.h:160
bool os_memory_coalesce(os_memory_t *memory)
Coalesce free blocks.
bool os_thread_stack_check_top_magic(os_thread_stack_t *stack)
Check if top magic word is still there.
POSIX compliant mutex.
Definition: os-mutex.h:53
bool os_mqueue_is_empty(os_mqueue_t *mqueue)
Check if the queue is empty.
size_t os_memory_get_total_bytes(os_memory_t *memory)
Get the total size of managed memory.
uint16_t os_mqueue_msg_size_t
Definition: os-c-decls.h:1244
uint8_t type_t
Type of variables holding mutex behaviours.
Definition: os-mutex.h:148
Event flags attributes.
Definition: os-c-decls.h:1375
void os_mutex_attr_init(os_mutex_attr_t *attr)
Initialise the mutex attributes.
Mutex attributes.
Definition: os-c-decls.h:899
port::clock::offset_t offset_t
Type of variables holding clock offsets.
Definition: os-clocks.h:91
os_result_t os_this_thread_flags_try_wait(os_flags_mask_t mask, os_flags_mask_t *oflags, os_flags_mode_t mode)
Try to wait for thread event flags.
uint32_t item_sz
size of an item
Definition: cmsis_os.h:332
uint32_t pool_sz
Definition: cmsis_os.h:363
uint8_t os_mutex_type_t
Definition: os-c-decls.h:798
bool os_sched_is_started(void)
Check if the scheduler was started.
function completed; message event occurred.
Definition: cmsis_os.h:162
bool os_mempool_is_full(os_mempool_t *mempool)
Check if the memory pool is full.
Ask for flags to be cleared after read.
Definition: os-decls.h:303
os_result_t os_mutex_try_lock(os_mutex_t *mutex)
Try to lock/acquire the mutex.
void os_mempool_construct(os_mempool_t *mempool, const char *name, size_t blocks, size_t block_size_bytes, const os_mempool_attr_t *attr)
Construct a statically allocated memory pool object instance.
void os_timer_destruct(os_timer_t *timer)
Destruct the statically allocated timer object instance.
uint8_t state_t
Type of variables holding thread states.
Definition: os-thread.h:337
void os_this_thread_exit(void *exit_ptr)
Terminate the current running thread.
os_statistics_duration_t os_thread_stat_get_cpu_cycles(os_thread_t *thread)
Get the thread execution time.
bool os_thread_stack_check_bottom_magic(os_thread_stack_t *stack)
Check if bottom magic word is still there.
void(*)(func_args_t args) func_t
Entry point of a timer call back function.
Definition: os-timer.h:67
os_thread_prio_t os_thread_get_priority(os_thread_t *thread)
Get the thread current scheduling priority.
osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority)
Change thread priority.
uint8_t os_mqueue_size_t
Definition: os-c-decls.h:1241
constexpr void sleep_for(const std::chrono::duration< Rep_T, Period_T > &rel_time)
Sleep for a given duration.
uint32_t instances
maximum number of instances of that thread function
Definition: cmsis_os.h:282
uint16_t msg_size_t
Type of message size storage.
Definition: os-mqueue.h:88
bool os_sched_is_locked(void)
Check if the scheduler is locked.
os_result_t os_mutex_timed_lock(os_mutex_t *mutex, os_clock_duration_t timeout)
Timed attempt to lock/acquire the mutex.
os_result_t os_mutex_reset(os_mutex_t *mutex)
Reset the mutex.
os_result_t os_clock_sleep_until(os_clock_t *clock, os_clock_timestamp_t timestamp)
Sleep until an absolute timestamp.
osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *args)
Create a thread.
int32_t osSignalSet(osThreadId thread_id, int32_t signals)
Set signal flags.
void os_mutex_attr_recursive_init(os_mutex_attr_t *attr)
Initialise the recursive mutex attributes.
pid_t wait(int *stat_loc)
size_t os_memory_get_allocated_chunks(os_memory_t *memory)
Get the number of allocated chunks.
os_memory_t * os_memory_get_default(void)
Get the application default memory resource (free store).
Used to catch uninitialised threads.
Definition: os-c-decls.h:300
uint8_t robustness_t
Type of variables holding mutex robustness.
Definition: os-mutex.h:107
std::size_t block_size(void) const
Get block size.
Definition: os-mempool.h:919
os_result_t os_mqueue_try_receive(os_mqueue_t *mqueue, void *msg, size_t nbytes, os_mqueue_prio_t *mprio)
Try to receive a message from the queue.
void os_mutex_recursive_construct(os_mutex_t *mutex, const char *name, const os_mutex_attr_t *attr)
Construct a statically allocated recursive mutex object instance.
System namespace.
void os_mqueue_construct(os_mqueue_t *mqueue, const char *name, size_t msgs, size_t msg_size_bytes, const os_mqueue_attr_t *attr)
Construct a statically allocated message queue object instance.
os_result_t os_sysclock_wait_for(os_clock_duration_t timeout)
Timed wait for an event.
os_result_t os_timer_start(os_timer_t *timer, os_clock_duration_t period)
Start or restart the timer.
static const attributes_binary initializer_binary
Default binary semaphore initialiser.
Definition: os-semaphore.h:202
os_semaphore_count_t os_semaphore_get_value(os_semaphore_t *semaphore)
Get the semaphore count value.
void * os_iterator_t
Generic iterator, implemented as a pointer.
Definition: os-c-decls.h:228
uint16_t count_t
Type of variables holding mutex recursion counters.
Definition: os-mutex.h:192
void os_semaphore_counting_construct(os_semaphore_t *semaphore, const char *name, const os_semaphore_count_t max_value, const os_semaphore_count_t initial_value)
Construct a statically allocated counting semaphore object instance.
void free(void *ptr)
Free the allocated memory block.
Definition: malloc.cpp:352
void sleep_until(const std::chrono::time_point< Clock_T, Duration_T > &abs_time)
Sleep until a given time point.
User single-shot or periodic timer.
Definition: os-timer.h:53
result_t flags_try_wait(flags::mask_t mask, flags::mask_t *oflags=nullptr, flags::mode_t mode=flags::mode::all|flags::mode::clear)
Try to wait for thread event flags.
Definition: os-thread.h:1920
os_thread_user_storage_t * os_thread_get_user_storage(os_thread_t *thread)
Get the thread user storage.
#define osFeature_Semaphore
maximum count for osSemaphoreCreate function
Definition: cmsis_os.h:117
uint32_t stacksize
stack size requirements in bytes; 0 is default stack size
Definition: cmsis_os.h:283
unspecified RTOS error: run-time error but no other error message fits.
Definition: cmsis_os.h:173
uint32_t items
number of elements in the queue
Definition: cmsis_os.h:359
osSemaphore * data
Definition: cmsis_os.h:318
Timer object storage.
Definition: os-c-decls.h:764
void os_mempool_attr_init(os_mempool_attr_t *attr)
Initialise the memory pool attributes.
port::interrupts::state_t state_t
Type of variables holding interrupts statu codes.
Definition: os-decls.h:243
void os_mutex_destruct(os_mutex_t *mutex)
Destruct the statically allocated mutex object instance.
clock_highres hrclock
The high resolution clock object instance.
Definition: os-clocks.cpp:717
memory_resource * get_default_resource(void) noexcept
Get the default RTOS system memory manager.
Definition: os-memory.h:1125
const char * name
Definition: cmsis_os.h:330
os_semaphore_t * os_semaphore_binary_new(const char *name, const os_semaphore_count_t initial_value)
Allocate a binary semaphore object instance and construct it.
os_port_irq_state_t os_irq_state_t
Type of variables holding interrupts priority values.
Definition: os-c-decls.h:176
parameter error: a mandatory parameter was missing or specified an incorrect object.
Definition: cmsis_os.h:165
const char * os_semaphore_get_name(os_semaphore_t *semaphore)
Get the semaphore name.
Semaphore attributes.
Definition: os-c-decls.h:1079
uint8_t os_thread_state_t
Type of variables holding thread states.
Definition: os-c-decls.h:350
osMessageQId osMessageCreate(const osMessageQDef_t *queue_def, osThreadId thread_id)
Create a message queue.
Binary semaphore attributes.
Definition: os-semaphore.h:163
const char * os_mutex_get_name(os_mutex_t *mutex)
Get the mutex name.
void * osPoolCAlloc(osPoolId pool_id)
Allocate and clear a memory block.
uint32_t os_result_t
Type of values returned by RTOS functions.
Definition: os-c-decls.h:100
void os_mutex_delete(os_mutex_t *mutex)
Destruct the mutex object instance and deallocate it.
void os_irq_critical_exit(os_irq_state_t state)
Exit the interrupts critical section.
os_mutex_robustness_t os_mutex_get_robustness(os_mutex_t *mutex)
Get the mutex robustness.
size_t os_thread_stack_get_size(os_thread_stack_t *stack)
Get the stack size.
static const attributes initializer
Default memory pool initialiser.
Definition: os-mempool.h:154
void start(void)
Start the RTOS scheduler.
Definition: os-core.cpp:186
bool os_mqueue_is_full(os_mqueue_t *mqueue)
Check if the queue is full.
uint32_t os_flags_mask_t
Type of variables holding flags masks.
Definition: os-c-decls.h:126
uint8_t size_t
Type of a queue size storage.
Definition: os-mqueue.h:75
Default mutex type.
Definition: os-c-decls.h:883
Present in the READY list and competing for CPU.
Definition: os-thread.h:362
os_result_t os_mqueue_reset(os_mqueue_t *mqueue)
Reset the message queue.
const char * name
Definition: cmsis_os.h:358
void os_semaphore_attr_init(os_semaphore_attr_t *attr)
Initialise the counting semaphore attributes.
clock_rtc rtclock
The real time clock object instance.
Definition: os-clocks.cpp:660
static const attributes_periodic periodic_initializer
Default periodic timer initialiser.
Definition: os-timer.h:252
os_result_t os_sched_initialize(void)
Initialise the RTOS scheduler.
const char * os_timer_get_name(os_timer_t *timer)
Get the timer name.
osStatus osKernelStart(void)
os_result_t os_this_thread_flags_wait(os_flags_mask_t mask, os_flags_mask_t *oflags, os_flags_mode_t mode)
Wait for thread event flags.
int32_t signals
signal flags
Definition: cmsis_os.h:381
Condition variable attributes.
Definition: os-c-decls.h:1005
os_statistics_counter_t os_sched_stat_get_context_switches(void)
Get the total number of context switches.
os_semaphore_t * os_semaphore_new(const char *name, const os_semaphore_attr_t *attr)
Allocated a semaphore object instance and construct it.
Thread object storage.
Definition: os-c-decls.h:554
os_clock_timestamp_t os_clock_now(os_clock_t *clock)
Tell the current time, possibly adjusted for epoch.
os_flags_mask_t os_evflags_get(os_evflags_t *evflags, os_flags_mask_t mask, os_flags_mode_t mode)
Get (and possibly clear) event flags.
void * pool(void)
Get the pool storage address.
Definition: os-mempool.h:963
uint32_t queue_sz
Definition: cmsis_os.h:348
Double linked list node, with time stamp and timer.
Definition: os-lists.h:302
int16_t os_semaphore_count_t
Type of variables holding semaphore counts.
Definition: os-c-decls.h:1064
Present in the READY list and competing for CPU.
Definition: os-c-decls.h:304
Inherit priority from highest priority thread.
Definition: os-mutex.h:84
os_statistics_duration_t os_sched_stat_get_cpu_cycles(void)
Get the total duration of all threads.
uint32_t osKernelSysTick(void)
Get the system timer counter.
result_t sleep_for(duration_t duration)
Sleep for a relative duration.
Definition: os-clocks.cpp:201
os_result_t os_semaphore_timed_wait(os_semaphore_t *semaphore, os_clock_duration_t timeout)
Timed wait to lock the semaphore.
uint8_t priority_t
Type of message priority storage.
Definition: os-mqueue.h:116
os_mutex_protocol_t os_mutex_get_protocol(os_mutex_t *mutex)
Get the mutex protocol.
uint8_t priority_t
Type of variables holding thread priorities.
Definition: os-thread.h:252
os_result_t os_condvar_wait(os_condvar_t *condvar, os_mutex_t *mutex)
Wait for a condition variable to be notified.
uint64_t os_statistics_duration_t
Type of variables holding durations in CPU cycles.
Definition: os-c-decls.h:244
Counting semaphore attributes.
Definition: os-semaphore.h:211
os_clock_timestamp_t os_sysclock_now(void)
Tell the current time since startup.
const char * name
Definition: cmsis_os.h:296
Thread statistics.
Definition: os-c-decls.h:457
Enhanced robustness at thread termination.
Definition: os-mutex.h:130
os_thread_stack_element_t * os_thread_stack_get_top(os_thread_stack_t *stack)
Get the top stack address.
os_result_t os_thread_set_priority(os_thread_t *thread, os_thread_prio_t prio)
Set the thread dynamic scheduling priority.
os_result_t os_clock_wait_for(os_clock_t *clock, os_clock_duration_t timeout)
Timed wait for an event.
os_result_t os_mutex_set_prio_ceiling(os_mutex_t *mutex, os_thread_prio_t prio_ceiling, os_thread_prio_t *old_prio_ceiling)
Change the priority ceiling of a mutex.
count_t sm_max_value
Semaphore max count value.
Definition: os-semaphore.h:142
void * osMailAlloc(osMailQId mail_id, uint32_t millisec)
Allocate a memory block from a mail.
POSIX compliant thread, using the default RTOS allocator.
Definition: os-thread.h:230
bool started(void)
Check if the scheduler was started.
Definition: os-sched.h:827
const os_semaphore_attr_t * os_semaphore_attr_get_binary(void)
Get a binary semaphore attributes object instance.
os_clock_t * os_clock_get_sysclock(void)
Get sysclock (the system clock).
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition: os-clocks.h:72
os_mutex_t * os_mutex_recursive_new(const char *name, const os_mutex_attr_t *attr)
Allocated a recursive mutex object instance and construct it.
const char * os_evflags_get_name(os_evflags_t *evflags)
Get the event flags name.
void os_mqueue_destruct(os_mqueue_t *mqueue)
Destruct the statically allocated message queue object instance.
osStatus osMutexRelease(osMutexId mutex_id)
Release the mutex.
os_thread_t * os_children_threads_iter_get(os_iterator_t iterator)
Get the thread from the current iterator position.
os_thread_t * os_mutex_get_owner(os_mutex_t *mutex)
Get the thread that owns the mutex.
void * pool
pointer to memory array for pool
Definition: cmsis_os.h:362
size_t os_thread_stack_set_default_size(size_t size_bytes)
Set the default stack size.
bool os_evflags_are_waiting(os_evflags_t *evflags)
Check if there are threads waiting.
size_t os_mqueue_get_capacity(os_mqueue_t *mqueue)
Get queue capacity.
os_result_t os_mqueue_send(os_mqueue_t *mqueue, const void *msg, size_t nbytes, os_mqueue_prio_t mprio)
Send a message to the queue.
os_port_clock_duration_t os_clock_duration_t
Type of variables holding clock durations.
Definition: os-c-decls.h:202
result_t wait_for(duration_t timeout)
Timed wait for an event.
Definition: os-clocks.cpp:286
Event flags object storage.
Definition: os-c-decls.h:1400
POSIX compliant condition variable.
Definition: os-condvar.h:51
int16_t os_mutex_count_t
Definition: os-c-decls.h:797
os_clock_offset_t os_clock_get_offset(os_clock_t *clock)
Get adjustment offset.
void * os_mempool_try_alloc(os_mempool_t *mempool)
Try to allocate a memory block.
protocol_t mx_protocol
Attribute with the mutex protocol.
Definition: os-mutex.h:273
osTimer * data
Definition: cmsis_os.h:298
size_t os_mqueue_get_length(os_mqueue_t *mqueue)
Get queue length.
os_thread_t * os_this_thread(void)
Get the current running thread.
std::size_t mq_queue_size_bytes
Size of the user defined storage for the message queue.
Definition: os-mqueue.h:193
void os_mempool_destruct(os_mempool_t *mempool)
Destruct the statically allocated memory pool object instance.
POSIX compliant binary semaphore.
Definition: os-semaphore.h:508
os_result_t os_mutex_unlock(os_mutex_t *mutex)
Unlock/release the mutex.
Normal robustness.
Definition: os-c-decls.h:845
Return when all flags are set.
Definition: os-decls.h:293
os_thread_stack_t * os_irq_get_stack(void)
Get the interrupts stack.
port::scheduler::state_t state_t
Type of variables holding scheduler state codes.
Definition: os-decls.h:202
size_t os_mempool_get_capacity(os_mempool_t *mempool)
Get memory pool capacity.
Enhanced robustness at thread termination.
Definition: os-c-decls.h:850
osThread * data
Definition: cmsis_os.h:284
rtos::statistics::counter_t context_switches(void)
Get the total number of context switches.
Definition: os-sched.h:1044
osThreadId osThreadGetId(void)
Get the current thread.
void os_thread_attr_init(os_thread_attr_t *attr)
Initialise the thread attributes.
#define osWaitForever
Timeout value.
Definition: cmsis_os.h:152
state_t state(void) const
Get thread scheduler state.
Definition: os-thread.h:2304
Inherit priority from highest priority thread.
Definition: os-c-decls.h:822
void os_condvar_destruct(os_condvar_t *condvar)
Destruct the statically allocated condition variable object instance.
os_sched_state_t os_sched_lock(void)
Lock the scheduler.
void * osPoolAlloc(osPoolId pool_id)
Allocate a memory block.
osPriority osThreadGetPriority(osThreadId thread_id)
Get thread priority.
bool preemptive(void)
Check if the scheduler is in preemptive mode.
Definition: os-sched.h:839
os_irq_state_t os_irq_critical_enter(void)
Enter an interrupts critical section.
Single file µOS++ RTOS definitions.
clock_systick sysclock
The system clock object instance.
Definition: os-clocks.cpp:551
os_irq_state_t os_irq_uncritical_enter(void)
Enter an interrupts uncritical section.
class thread::statistics & statistics(void)
Definition: os-thread.h:2409
Synchronised memory pool, using the default RTOS allocator.
Definition: os-mempool.h:58
flags::mask_t flags_get(flags::mask_t mask, flags::mode_t mode=flags::mode::all|flags::mode::clear)
Get/clear thread event flags.
Definition: os-thread.h:1985
class thread::stack & stack(void)
Get the thread context stack.
Definition: os-thread.h:2396
Function completed; no errors or events occurred.
Definition: os-decls.h:181
os_statistics_counter_t os_thread_stat_get_context_switches(os_thread_t *thread)
Get the number of thread context switches.
Thread stack.
Definition: os-c-decls.h:402
void os_thread_resume(os_thread_t *thread)
Resume the thread.
os_result_t os_this_thread_flags_timed_wait(os_flags_mask_t mask, os_clock_duration_t timeout, os_flags_mask_t *oflags, os_flags_mode_t mode)
Timed wait for thread event flags.
os_result_t os_semaphore_wait(os_semaphore_t *semaphore)
Lock the semaphore, possibly waiting.
Memory pool attributes.
Definition: os-c-decls.h:1165
const char * name
Definition: cmsis_os.h:307
os_pthread pthread
start address of thread function
Definition: cmsis_os.h:280
Not present in the READY list, waiting for an event.
Definition: os-c-decls.h:312
void * osMailCAlloc(osMailQId mail_id, uint32_t millisec)
Allocate and clear a memory block from a mail.
os_result_t os_mutex_lock(os_mutex_t *mutex)
Lock/acquire the mutex.
osStatus status
status code: event or error information
Definition: cmsis_os.h:376
osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec)
Put a message to a queue.
Execute at the highest priority.
Definition: os-c-decls.h:827
function completed; timeout occurred.
Definition: cmsis_os.h:164
Normal mutex behaviour.
Definition: os-c-decls.h:868
Timer attributes.
Definition: os-c-decls.h:734
static const attributes_recursive initializer_recursive
Default recursive mutex initialiser.
Definition: os-mutex.h:351
os_mempool_t pool
Definition: cmsis_os.h:222
void suspend(void)
Suspend the current running thread to wait for an event.
Definition: os-thread.h:1876
const char * os_thread_get_name(os_thread_t *thread)
Get the thread name.
osMailQId osMailCreate(const osMailQDef_t *mail_def, osThreadId thread_id)
Create a mail queue.
not allowed in ISR context: the function cannot be called from interrupt service routines.
Definition: cmsis_os.h:168
void * os_memory_allocate(os_memory_t *memory, size_t bytes, size_t alignment)
Allocate a block of memory.
Periodic timer attributes.
Definition: os-timer.h:212
osPool * data
Definition: cmsis_os.h:335
Used to catch uninitialised threads.
Definition: os-thread.h:358
void os_this_thread_suspend(void)
Suspend the current running thread to wait for an event.
void * os_mempool_alloc(os_mempool_t *mempool)
Allocate a memory block.
Not present in the READY list, waiting for an event.
Definition: os-thread.h:370
os_thread_stack_element_t * os_thread_stack_get_bottom(os_thread_stack_t *stack)
Get the stack lowest reserved address.
os_semaphore_count_t os_semaphore_get_max_value(os_semaphore_t *semaphore)
Get the semaphore maximum count value.
Semaphore object storage.
Definition: os-c-decls.h:1114
type_t mx_type
Attribute with the mutex type.
Definition: os-mutex.h:283
Has the CPU and runs.
Definition: os-c-decls.h:308
os_iterator_t os_children_threads_iter_next(os_iterator_t iterator)
Advance the iterator to the next position.
os_mqueue_size_t os_mqueue_index_t
Definition: os-c-decls.h:1245
os_flags_mask_t os_this_thread_flags_get(os_flags_mask_t mask, os_flags_mode_t mode)
Get/clear thread event flags.
resource not available within given time: a specified resource was not available within the timeout p...
Definition: cmsis_os.h:167
int16_t count_t
Type of semaphore counter storage.
Definition: os-semaphore.h:64
static const attributes initializer
Default message queue initialiser.
Definition: os-mqueue.h:207
const char * os_condvar_get_name(os_condvar_t *condvar)
Get the condition variable name.
static state_t enter(void)
Enter an interrupts critical section.
Definition: os-sched.h:1128
Run only once.
Definition: os-timer.h:87
uint32_t items
number of items (elements) in the pool
Definition: cmsis_os.h:331
os_thread_t * os_thread_new(const char *name, os_thread_func_t func, const os_thread_func_args_t args, const os_thread_attr_t *attr)
Allocate a thread object instance and construct it.
os_mqueue_t queue
Definition: cmsis_os.h:223
Event flags attributes.
Definition: os-evflags.h:66
size_t os_thread_stack_get_default_size(void)
Get the default stack size.
osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec)
Wait for mutex.
os_thread_prio_t os_mutex_get_prio_ceiling(os_mutex_t *mutex)
Get the priority ceiling of a mutex.
state_t lock(void)
Lock the scheduler.
Definition: os-sched.h:869
void os_thread_destruct(os_thread_t *thread)
Destruct the statically allocated thread object instance.
size_t os_mqueue_get_msg_size(os_mqueue_t *mqueue)
Get message size.
int32_t osKernelRunning(void)
Check if the RTOS scheduler is started.
Semaphore attributes.
Definition: os-semaphore.h:81
union osEvent::@0 value
event value
os_result_t os_evflags_wait(os_evflags_t *evflags, os_flags_mask_t mask, os_flags_mask_t *oflags, os_flags_mode_t mode)
Wait for event flags.
uint8_t os_timer_state_t
Type of variables holding timer states.
Definition: os-c-decls.h:719
Return when at least one flag is set.
Definition: os-decls.h:298
osSemaphoreId osSemaphoreCreate(const osSemaphoreDef_t *semaphore_def, int32_t count)
Create a semaphore.
uint32_t items
number of elements in the queue
Definition: cmsis_os.h:345
Priority and scheduling not affected by mutex ownership.
Definition: os-c-decls.h:817
void os_mempool_delete(os_mempool_t *mempool)
Destruct the memory pool object instance and deallocate it.
static std::size_t min_size(void)
Get the min stack size.
Definition: os-thread.h:2166
os_evflags_t * os_evflags_new(const char *name, const os_evflags_attr_t *attr)
Allocate an event flags object instance and construct it.
uint8_t state_t
Type of of variables holding timer states.
Definition: os-timer.h:100
os_result_t os_semaphore_post(os_semaphore_t *semaphore)
Post (unlock) the semaphore.
Semaphore definition structure contains setup information for a semaphore.
Definition: cmsis_os.h:315
size_t os_memory_get_free_chunks(os_memory_t *memory)
Get the number of free chunks.
Recursive mutex attributes.
Definition: os-mutex.h:311
Synchronised event flags.
Definition: os-evflags.h:55
uint8_t os_mutex_robustness_t
Definition: os-c-decls.h:800
osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec)
Get a message or Wait for a Message from a Queue.
os_mqueue_t * os_mqueue_new(const char *name, size_t msgs, size_t msg_size_bytes, const os_mqueue_attr_t *attr)
Allocate a message queue object instance and construct it.
os_result_t os_timer_stop(os_timer_t *timer)
Stop the timer.
osMutexId osMutexCreate(const osMutexDef_t *mutex_def)
Create a mutex.
os_result_t os_evflags_raise(os_evflags_t *evflags, os_flags_mask_t mask, os_flags_mask_t *oflags)
Raise event flags.
void os_condvar_delete(os_condvar_t *condvar)
Destruct the condition variable object instance and deallocate it.
state_t unlock(void)
Unlock the scheduler.
Definition: os-sched.h:882
virtual result_t sleep_until(timestamp_t timestamp)
Sleep until an absolute timestamp.
Definition: os-clocks.cpp:245
void * p
message or mail as void pointer
Definition: cmsis_os.h:380
void os_mqueue_attr_init(os_mqueue_attr_t *attr)
Initialise the message queue attributes.
static void exit(state_t state)
Exit the interrupts critical section.
Definition: os-sched.h:1140
result_t flags_clear(flags::mask_t mask, flags::mask_t *oflags=nullptr)
Clear thread event flags.
Definition: os-thread.h:1996
os_result_t os_sysclock_sleep_until(os_clock_timestamp_t timestamp)
Sleep until an absolute timestamp.
result_t join(void **exit_ptr=nullptr)
Wait for thread termination.
Definition: os-thread.cpp:859
osStatus osMailFree(osMailQId mail_id, void *mail)
Free a memory block from a mail.
os_result_t os_evflags_timed_wait(os_evflags_t *evflags, os_flags_mask_t mask, os_clock_duration_t timeout, os_flags_mask_t *oflags, os_flags_mode_t mode)
Timed wait for event flags.
const char * name
Definition: cmsis_os.h:317
Timer attributes.
Definition: os-timer.h:131
uint16_t os_mempool_size_t
Definition: os-c-decls.h:1145
const char * os_clock_get_name(os_clock_t *clock)
Get the clock name.
os_thread_state_t os_thread_get_state(os_thread_t *thread)
Get the thread scheduler state.
uint64_t os_statistics_counter_t
Type of variables holding context switches counters.
Definition: os-c-decls.h:237
void * queue
pointer to memory array for queue
Definition: cmsis_os.h:364
bool os_sched_is_preemptive(void)
Check if the scheduler is in preemptive mode.
os_result_t os_evflags_try_wait(os_evflags_t *evflags, os_flags_mask_t mask, os_flags_mask_t *oflags, os_flags_mode_t mode)
Try to wait for event flags.
void os_evflags_attr_init(os_evflags_attr_t *attr)
Initialise the event flags attributes.
const char * name
Definition: cmsis_os.h:279
Memory pool object storage.
Definition: os-c-decls.h:1200
Condition variable attributes.
Definition: os-condvar.h:62
result_t flags_timed_wait(flags::mask_t mask, clock::duration_t timeout, flags::mask_t *oflags=nullptr, flags::mode_t mode=flags::mode::all|flags::mode::clear)
Timed wait for thread event flags.
Definition: os-thread.h:1966
uint64_t counter_t
Type of variables holding context switches counters.
Definition: os-decls.h:217
void os_evflags_destruct(os_evflags_t *evflags)
Destruct the statically allocated event flags object instance.
Definition structure for memory block allocation.
Definition: cmsis_os.h:328
virtual timestamp_t now(void) override
Tell the current time.
Definition: os-clocks.cpp:760
osStatus osThreadTerminate(osThreadId thread_id)
Terminate a thread.
uint32_t item_sz
size of an item
Definition: cmsis_os.h:346
os_port_thread_stack_element_t os_thread_stack_element_t
Type of variables holding stack words.
Definition: os-c-decls.h:377
os_port_clock_offset_t os_clock_offset_t
Type of variables holding clock offsets.
Definition: os-c-decls.h:214
Generic clock.
Definition: os-clocks.h:54
int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec)
Wait until a Semaphore token becomes available.
os_result_t os_condvar_broadcast(os_condvar_t *condvar)
Notify all threads waiting for a condition variable.
void * mp_pool_address
Address of the user defined storage for the memory pool.
Definition: os-mempool.h:135
os_clock_timestamp_t os_clock_steady_now(os_clock_t *clock)
Tell the current time since startup.
void *(*)(func_args_t args) func_t
Type of thread function.
Definition: os-thread.h:397
RTOS namespace.
Definition: os-flags.h:41
thread & thread(void)
Get the current running thread.
Definition: os-thread.cpp:1543
uint32_t mask_t
Type of variables holding flags masks.
Definition: os-decls.h:265
size_t os_thread_stack_get_min_size(void)
Get the min stack size.
const char * name
Definition: cmsis_os.h:344
void os_memory_deallocate(os_memory_t *memory, void *addr, size_t bytes, size_t alignment)
Deallocate the previously allocated block of memory.
void(* os_timer_func_t)(os_timer_func_args_t args)
Type of timer function.
Definition: os-c-decls.h:705
os_mempool_t * os_mempool_new(const char *name, size_t blocks, size_t block_size_bytes, const os_mempool_attr_t *attr)
Allocate a memory pool object instance and construct it.
osStatus osTimerDelete(osTimerId timer_id)
Delete the timer.
uint32_t pool_sz
Definition: cmsis_os.h:334
uint8_t os_thread_prio_t
Type of variables holding thread priorities.
Definition: os-c-decls.h:364
void os_semaphore_binary_construct(os_semaphore_t *semaphore, const char *name, const os_semaphore_count_t initial_value)
Construct a statically allocated binary semaphore object instance.
POSIX compliant recursive mutex.
Definition: os-mutex.h:716
void * os_mempool_get_pool(os_mempool_t *mempool)
Get the pool storage address.
uint32_t os_flags_mode_t
Type of variables holding flags modes.
Definition: os-c-decls.h:113
uint32_t mode_t
Type of variables holding flags modes.
Definition: os-decls.h:275
void os_memory_reset(os_memory_t *memory)
Reset the memory manager to the initial state.
os_iterator_t os_children_threads_iter_end(os_thread_t *thread)
Get the end of the list of children threads.
osTimerId osTimerCreate(const osTimerDef_t *timer_def, os_timer_type type, void *args)
Create a timer.
result_t flags_wait(flags::mask_t mask, flags::mask_t *oflags=nullptr, flags::mode_t mode=flags::mode::all|flags::mode::clear)
Wait for thread event flags.
Definition: os-thread.h:1903
os_clock_t * os_clock_get_hrclock(void)
Get hrclock (the high resolution clock).
POSIX compliant counting semaphore.
Definition: os-semaphore.h:581
os_result_t os_condvar_signal(os_condvar_t *condvar)
Notify one thread waiting for a condition variable.
int32_t osSignalClear(osThreadId thread_id, int32_t signals)
Clear the specified Signal Flags of an active thread.
uint32_t result_t
Type of values returned by RTOS functions.
Definition: os-decls.h:96
os_result_t os_mutex_mark_consistent(os_mutex_t *mutex)
Mark mutex as consistent.
No longer usable, but resources not yet released.
Definition: os-thread.h:374
void os_thread_delete(os_thread_t *thread)
Destruct the thread object instance and deallocate it.
void * pool
pointer to memory for pool
Definition: cmsis_os.h:333
osPriority tpriority
initial thread priority
Definition: cmsis_os.h:281
os_result_t os_semaphore_reset(os_semaphore_t *semaphore)
Reset the semaphore.
const os_mutex_attr_t * os_mutex_attr_get_recursive(void)
Get a recursive mutex attributes object instance.
uint8_t os_mqueue_prio_t
Type of variables holding message queue priorities.
Definition: os-c-decls.h:1257
void os_semaphore_attr_binary_init(os_semaphore_attr_t *attr, const os_semaphore_count_t initial_value)
Initialise the binary semaphore attributes.
result_t initialize(void)
Initialise the RTOS scheduler.
Definition: os-core.cpp:157
void os_condvar_construct(os_condvar_t *condvar, const char *name, const os_condvar_attr_t *attr)
Construct a statically allocated condition variable object instance.
void * mq_queue_address
Address of the user defined storage for the message queue.
Definition: os-mqueue.h:188
Condition variable object storage.
Definition: os-c-decls.h:1031
osEvent osWait(uint32_t millisec)
Wait for Signal, Message, Mail, or Timeout.
Message queue attributes.
Definition: os-mqueue.h:142
void os_sched_start(void)
Start the RTOS scheduler.
osEvent osMailGet(osMailQId mail_id, uint32_t millisec)
Get a mail from a queue.
os_port_clock_timestamp_t os_clock_timestamp_t
Type of variables holding clock time stamps.
Definition: os-c-decls.h:191
os_result_t os_sysclock_sleep_for(os_clock_duration_t duration)
Sleep for a relative duration.
uint16_t size_t
Type of memory pool size storage.
Definition: os-mempool.h:71
void os_semaphore_construct(os_semaphore_t *semaphore, const char *name, const os_semaphore_attr_t *attr)
Construct a statically allocated semaphore object instance.
Recursive mutex behaviour.
Definition: os-mutex.h:174
osStatus osTimerStart(osTimerId timer_id, uint32_t millisec)
Start the timer.
os_semaphore_count_t os_semaphore_get_initial_value(os_semaphore_t *semaphore)
Get the semaphore initial count value.
Has the CPU and runs.
Definition: os-thread.h:366
os_ptimer ptimer
start address of a timer function
Definition: cmsis_os.h:297
osStatus osSemaphoreRelease(osSemaphoreId semaphore_id)
Release the semaphore.
thread::threads_list & children_threads(thread *th)
Get the children threads.
Definition: os-core.cpp:260
os_result_t os_condvar_timed_wait(os_condvar_t *condvar, os_mutex_t *mutex, os_clock_duration_t timeout)
Timed wait for a condition variable to be notified.
Priority and scheduling not affected by mutex ownership.
Definition: os-mutex.h:79
static constexpr clock::duration_t ticks_cast(Rep_T microsec)
Convert microseconds to ticks.
osStatus
Status code values returned by CMSIS-RTOS functions.
Definition: cmsis_os.h:158
os_timer_t * os_timer_new(const char *name, os_timer_func_t function, os_timer_func_args_t args, const os_timer_attr_t *attr)
Allocate a timer object instance and construct it.
Recursive mutex behaviour.
Definition: os-c-decls.h:878
Memory pool attributes.
Definition: os-mempool.h:89
void os_thread_construct(os_thread_t *thread, const char *name, os_thread_func_t func, const os_thread_func_args_t args, const os_thread_attr_t *attr)
Construct a statically allocated thread object instance.
Mutex object storage.
Definition: os-c-decls.h:949
os_result_t os_mqueue_timed_send(os_mqueue_t *mqueue, const void *msg, size_t nbytes, os_clock_duration_t timeout, os_mqueue_prio_t mprio)
Send a message to the queue with timeout.
void * queue
pointer to memory array for messages
Definition: cmsis_os.h:347
void os_condvar_attr_init(os_condvar_attr_t *attr)
Initialise the condition variable attributes.
osMessageQ * data
Definition: cmsis_os.h:349
_func_args_t func_args_t
Type of thread function arguments.
Definition: os-thread.h:389
Execute at the highest priority.
Definition: os-mutex.h:89
void * os_timer_func_args_t
Type of timer function arguments.
Definition: os-c-decls.h:693
void *(* os_thread_func_t)(os_thread_func_args_t args)
Type of thread function.
Definition: os-c-decls.h:343
Mutex attributes.
Definition: os-mutex.h:207
os_port_scheduler_state_t os_sched_state_t
Type of variables holding scheduler state codes.
Definition: os-c-decls.h:162
osStatus osKernelInitialize(void)
Initialize the RTOS.
Check mutex behaviour.
Definition: os-mutex.h:170
size_t os_mempool_get_block_size(os_mempool_t *mempool)
Get block size.
virtual timestamp_t now(void)
Tell the current time, possibly adjusted for epoch.
Definition: os-clocks.cpp:169
void os_timer_attr_periodic_init(os_timer_attr_t *attr)
Initialise the periodic timer attributes.
function completed; mail event occurred.
Definition: cmsis_os.h:163
size_t os_thread_stack_get_available(os_thread_stack_t *stack)
Compute how much available stack remains.
POSIX compliant message queue, using the default RTOS allocator.
Definition: os-mqueue.h:58
Clock object storage.
Definition: os-c-decls.h:631
uint64_t duration_t
Type of variables holding durations in CPU cycles.
Definition: os-decls.h:222
int kill(pid_t pid, int sig)
bool os_mempool_is_empty(os_mempool_t *mempool)
Check if the memory pool is empty.
osMailQ * data
Definition: cmsis_os.h:366
void os_evflags_construct(os_evflags_t *evflags, const char *name, const os_evflags_attr_t *attr)
Construct a statically allocated event flags object instance.
osPoolId osPoolCreate(const osPoolDef_t *pool_def)
Create a memory pool.
Memory resource object storage.
Definition: os-c-decls.h:1481
USB switch to High Speed occurred.
Definition: usb-device.h:142
Terminated and resources (like stack) released.
Definition: os-c-decls.h:320
Message queue attributes.
Definition: os-c-decls.h:1272
osStatus osMailPut(osMailQId mail_id, void *mail)
Put a mail to a queue.
Definition structure for message queue.
Definition: cmsis_os.h:342
Definition structure for mail queue.
Definition: cmsis_os.h:356
void os_semaphore_attr_counting_init(os_semaphore_attr_t *attr, const os_semaphore_count_t max_value, const os_semaphore_count_t initial_value)
Initialise the counting semaphore attributes.
osStatus osMutexDelete(osMutexId mutex_id)
Delete the mutex.
uint32_t queue_item_sz
size of a queue item
Definition: cmsis_os.h:361
os_clock_offset_t os_clock_set_offset(os_clock_t *clock, os_clock_offset_t offset)
Set adjustment offset.
os_result_t os_this_thread_flags_clear(os_flags_mask_t mask, os_flags_mask_t *oflags)
Clear thread event flags.
bool os_sched_set_preemptive(bool state)
Set the scheduler preemptive mode.
osMutex * data
Definition: cmsis_os.h:308
bool in_handler_mode(void)
Check if the CPU is in handler mode.
Definition: os-sched.h:1091
bool os_irq_in_handler_mode(void)
Check if the CPU is in handler mode.
rtos::statistics::duration_t cpu_cycles(void)
Get the total duration of all threads.
Definition: os-sched.h:1069
osPriority
Definition: cmsis_os.h:136
os_timer_type
Timer type value for the timer definition.
Definition: cmsis_os.h:181
osStatus osPoolFree(osPoolId pool_id, void *block)
Free a memory block.
system cannot determine priority or thread has illegal priority
Definition: cmsis_os.h:145