µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os-mutex.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2016-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#ifndef CMSIS_PLUS_RTOS_OS_MUTEX_H_
13#define CMSIS_PLUS_RTOS_OS_MUTEX_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(__cplusplus)
18
19// ----------------------------------------------------------------------------
20
22
23// ----------------------------------------------------------------------------
24
25#pragma GCC diagnostic push
26#if defined(__clang__)
27#pragma clang diagnostic ignored "-Wc++98-compat"
28#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
29#endif
30
31// ----------------------------------------------------------------------------
32
33namespace os
34{
35 namespace rtos
36 {
37 // ========================================================================
38
39#pragma GCC diagnostic push
40#if defined(__clang__)
41#pragma clang diagnostic ignored "-Wpadded"
42#elif defined(__GNUC__)
43#pragma GCC diagnostic ignored "-Wpadded"
44#endif
45
52 {
53 public:
58 using protocol_t = uint8_t;
59
65 struct protocol
66 {
70 enum : protocol_t
71 {
75 none = 0,
76
81
86
91
96 };
97 };
98
103 using robustness_t = uint8_t;
104
111 {
115 enum : robustness_t
116 {
121
126
131
135 max_ = robust
136 };
137 };
138
143 using type_t = uint8_t;
144
150 struct type
151 {
155 enum : type_t
156 {
169
174
179 };
180 };
181
186 using count_t = uint16_t;
187
192 static constexpr count_t max_count = 0xFFFF;
193
194 // ======================================================================
195
202 {
203 public:
214 constexpr attributes ();
215
216 protected:
221 constexpr attributes (type_t type);
222
227 public:
228 // The rule of five.
229 attributes (const attributes&) = default;
230 attributes (attributes&&) = default;
233 = default;
236 = default;
237
241 ~attributes () = default;
242
247 public:
253 // Public members; no accessors and mutators required.
254 // Warning: must match the type & order of the C file header.
259
264
269
274
279
280 // Add more attributes here.
281
286 }; /* class attributes */
287
293
294 // ======================================================================
295
302 {
303 public:
314 constexpr attributes_recursive ();
315
316 // The rule of five.
321 = default;
324 = default;
325
330
335 }; /* class attributes_recursive */
336
342
352 mutex (const attributes& attr = initializer_normal);
353
359 mutex (const char* name, const attributes& attr = initializer_normal);
360
365 // The rule of five.
366 mutex (const mutex&) = delete;
367 mutex (mutex&&) = delete;
368 mutex&
369 operator= (const mutex&)
370 = delete;
371 mutex&
372 operator= (mutex&&)
373 = delete;
374
382 ~mutex ();
383
398 bool
399 operator== (const mutex& rhs) const;
400
405 public:
433 lock (void);
434
459 try_lock (void);
460
485
499 unlock (void);
500
508 prio_ceiling (void) const;
509
534 thread::priority_t* old_prio_ceiling = nullptr);
535
546 consistent (void);
547
554 thread*
555 owner (void);
556
561 type_t
562 type (void);
563
569 protocol (void);
570
576 robustness (void);
577
585 reset (void);
586
591 protected:
592 friend class thread;
593
608 void
609 internal_init_ (void);
610
617 internal_try_lock_ (thread* th);
618
625 internal_unlock_ (thread* th);
626
627 void
628 internal_mark_owner_dead_ (void);
629
638 protected:
648 // Can be updated in different thread contexts.
649 thread* volatile owner_ = nullptr;
650
651#if !defined(OS_USE_RTOS_PORT_MUTEX)
653 clock* clock_ = nullptr;
654#endif
655
656 public:
657 // Intrusive node used to link this mutex to the owning thread.
658 // This is used for priority inheritance and robustness.
659 utils::double_list_links owner_links_;
660
661 protected:
662#if defined(OS_USE_RTOS_PORT_MUTEX)
663 friend class port::mutex;
664 os_mutex_port_data_t port_;
665#endif
666
667 // Can be updated in different thread contexts.
668 volatile count_t count_ = 0;
669
670 // Can be updated in different thread contexts.
671 volatile thread::priority_t initial_prio_ceiling_
673 volatile thread::priority_t prio_ceiling_ = thread::priority::highest;
674 volatile thread::priority_t boosted_prio_ = thread::priority::none;
675
676 bool owner_dead_ = false;
677 bool consistent_ = true;
678 bool recoverable_ = true;
679
680 // Constants set during construction.
681 const type_t type_; // normal, errorcheck, recursive
682 const protocol_t protocol_; // none, inherit, protect
683 const robustness_t robustness_; // stalled, robust
684 const count_t max_count_;
685
686 // Add more internal data.
687
695 };
696
702 class mutex_recursive : public mutex
703 {
704 public:
714
718 mutex_recursive (const char* name,
719 const attributes& attr = initializer_recursive);
720
725 // The rule of five.
726 mutex_recursive (const mutex_recursive&) = delete;
727 mutex_recursive (mutex_recursive&&) = delete;
729 operator= (const mutex_recursive&)
730 = delete;
732 operator= (mutex_recursive&&)
733 = delete;
734
743
758 bool
759 operator== (const mutex_recursive& rhs) const;
760
764 };
765
766#pragma GCC diagnostic pop
767
768 // ========================================================================
769
770 } /* namespace rtos */
771} /* namespace os */
772
773// ===== Inline & template implementations ====================================
774
775namespace os
776{
777 namespace rtos
778 {
779
780 // ========================================================================
781
783 {
784 }
785
790 constexpr mutex::attributes::attributes (type_t type) : mx_type (type)
791 {
792 }
793
798 // ========================================================================
800 : attributes{ type::recursive } // Use the protected constructor.
801 {
802 }
803
804 // ========================================================================
805
810 inline bool
811 mutex::operator== (const mutex& rhs) const
812 {
813 return this == &rhs;
814 }
815
819 inline thread*
821 {
822 return owner_;
823 }
824
828 inline mutex::type_t
830 {
831 return type_;
832 }
833
837 inline mutex::protocol_t
839 {
840 return protocol_;
841 }
842
848 {
849 return robustness_;
850 }
851
852 // ========================================================================
853
855 : mutex{ attr }
856 {
857 }
858
859 inline mutex_recursive::mutex_recursive (const char* name,
860 const attributes& attr)
861 : mutex{ name, attr }
862 {
863 }
864
866 {
867 }
868
873 inline bool
875 {
876 return this == &rhs;
877 }
878
879 } /* namespace rtos */
880} /* namespace os */
881
882#pragma GCC diagnostic pop
883
884// ----------------------------------------------------------------------------
885
886#endif /* __cplusplus */
887
888// ----------------------------------------------------------------------------
889
890#endif /* CMSIS_PLUS_RTOS_OS_MUTEX_H_ */
Generic clock.
Definition os-clocks.h:61
Base class for attributes.
Definition os-decls.h:559
Base class for named system objects.
Definition os-decls.h:443
const char * name(void) const
Get object name.
Definition os-decls.h:753
Priority ordered list of threads.
Definition os-lists.h:543
Recursive mutex attributes.
Definition os-mutex.h:302
attributes_recursive & operator=(const attributes_recursive &)=default
attributes_recursive(const attributes_recursive &)=default
attributes_recursive(attributes_recursive &&)=default
~attributes_recursive()=default
Destruct the recursive mutex attributes object instance.
constexpr attributes_recursive()
Construct a recursive mutex attributes object instance.
Definition os-mutex.h:799
Mutex attributes.
Definition os-mutex.h:202
count_t mx_max_count
Attribute with the mutex maximum recursive count.
Definition os-mutex.h:278
constexpr attributes()
Construct a mutex attributes object instance.
Definition os-mutex.h:782
type_t mx_type
Attribute with the mutex type.
Definition os-mutex.h:273
robustness_t mx_robustness
Attribute with the mutex robustness.
Definition os-mutex.h:268
~attributes()=default
Destruct the mutex attributes object instance.
thread::priority_t mx_priority_ceiling
Attribute with the mutex priority ceiling.
Definition os-mutex.h:258
attributes & operator=(const attributes &)=default
protocol_t mx_protocol
Attribute with the mutex protocol.
Definition os-mutex.h:263
attributes(attributes &&)=default
attributes(const attributes &)=default
POSIX compliant recursive mutex.
Definition os-mutex.h:703
mutex_recursive(const attributes &attr=initializer_recursive)
Construct a recursive mutex object instance.
Definition os-mutex.h:854
bool operator==(const mutex_recursive &rhs) const
Compare mutexes.
Definition os-mutex.h:874
~mutex_recursive()
Destruct the recursive mutex object instance.
Definition os-mutex.h:865
POSIX compliant mutex.
Definition os-mutex.h:52
result_t reset(void)
Reset the mutex.
result_t lock(void)
Lock/acquire the mutex.
thread::priority_t prio_ceiling(void) const
Get the priority ceiling of a mutex.
result_t timed_lock(clock::duration_t timeout)
Timed attempt to lock/acquire the mutex.
result_t try_lock(void)
Try to lock/acquire the mutex.
bool operator==(const mutex &rhs) const
Compare mutexes.
Definition os-mutex.h:811
~mutex()
Destruct the mutex object instance.
Definition os-mutex.cpp:570
result_t consistent(void)
Mark mutex as consistent.
thread * owner(void)
Get the thread that owns the mutex.
Definition os-mutex.h:820
result_t unlock(void)
Unlock/release the mutex.
POSIX compliant thread, using the default RTOS allocator.
Definition os-thread.h:251
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition os-clocks.h:78
uint8_t type_t
Type of variables holding mutex behaviours.
Definition os-mutex.h:143
uint16_t count_t
Type of variables holding mutex recursion counters.
Definition os-mutex.h:186
static const attributes_recursive initializer_recursive
Default recursive mutex initialiser.
Definition os-mutex.h:341
uint8_t protocol_t
Type of variables holding mutex protocols.
Definition os-mutex.h:58
uint8_t robustness_t
Type of variables holding mutex robustness.
Definition os-mutex.h:103
static constexpr count_t max_count
Constant with the maximum value for the recursion counter.
Definition os-mutex.h:192
static const attributes initializer_normal
Default normal mutex initialiser.
Definition os-mutex.h:292
uint8_t priority_t
Type of variables holding thread priorities.
Definition os-thread.h:271
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:95
System namespace.
Mutex protocols.
Definition os-mutex.h:66
@ max_
Maximum value, for validation purposes.
Definition os-mutex.h:95
@ default_
Default value. Differs from POSIX, which uses none.
Definition os-mutex.h:90
@ none
Priority and scheduling not affected by mutex ownership.
Definition os-mutex.h:75
@ inherit
Inherit priority from highest priority thread.
Definition os-mutex.h:80
@ protect
Execute at the highest priority.
Definition os-mutex.h:85
Mutex robustness.
Definition os-mutex.h:111
@ max_
Maximum value, for validation purposes.
Definition os-mutex.h:135
@ stalled
Normal robustness.
Definition os-mutex.h:120
@ default_
Default value.
Definition os-mutex.h:130
@ robust
Enhanced robustness at thread termination.
Definition os-mutex.h:125
@ max_
Maximum value, for validation purposes.
Definition os-mutex.h:178
@ normal
Normal mutex behaviour.
Definition os-mutex.h:160
@ default_
Default value.
Definition os-mutex.h:173
@ errorcheck
Check mutex behaviour.
Definition os-mutex.h:164
@ recursive
Recursive mutex behaviour.
Definition os-mutex.h:168