µ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++ 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#ifndef CMSIS_PLUS_RTOS_OS_MUTEX_H_
29#define CMSIS_PLUS_RTOS_OS_MUTEX_H_
30
31// ----------------------------------------------------------------------------
32
33#if defined(__cplusplus)
34
35// ----------------------------------------------------------------------------
36
38
39// ----------------------------------------------------------------------------
40
41#pragma GCC diagnostic push
42
43#if defined(__clang__)
44#pragma clang diagnostic ignored "-Wc++98-compat"
45#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
46#endif
47
48// ----------------------------------------------------------------------------
49
50namespace os
51{
52 namespace rtos
53 {
54 // ========================================================================
55
56#pragma GCC diagnostic push
57#pragma GCC diagnostic ignored "-Wpadded"
58
65 {
66 public:
67
72 using protocol_t = uint8_t;
73
79 struct protocol
80 {
84 enum
86 {
90 none = 0,
91
96
101
106
110 max_ = protect
111 };
112 };
113
118 using robustness_t = uint8_t;
119
126 {
130 enum
132 {
137
142
147
151 max_ = robust
152 };
153 };
154
159 using type_t = uint8_t;
160
166 struct type
167 {
171 enum
172 : type_t
173 {
186
191
196 };
197 };
198
203 using count_t = uint16_t;
204
209 static constexpr count_t max_count = 0xFFFF;
210
211 // ======================================================================
212
219 {
220 public:
221
232 constexpr
233 attributes ();
234
235 protected:
236
241 constexpr
243
248 public:
249
250 // The rule of five.
251 attributes (const attributes&) = default;
252 attributes (attributes&&) = default;
254 operator= (const attributes&) = default;
256 operator= (attributes&&) = default;
257
261 ~attributes () = default;
262
267 public:
268
274 // Public members; no accessors and mutators required.
275 // Warning: must match the type & order of the C file header.
280
285
290
295
300
301 // Add more attributes here.
302
307 }; /* class attributes */
308
314
315 // ======================================================================
316
323 {
324 public:
325
336 constexpr
338
339 // The rule of five.
346
351
356 }; /* class attributes_recursive */
357
363
373 mutex (const attributes& attr = initializer_normal);
374
380 mutex (const char* name, const attributes& attr = initializer_normal);
381
386 // The rule of five.
387 mutex (const mutex&) = delete;
388 mutex (mutex&&) = delete;
389 mutex&
390 operator= (const mutex&) = delete;
391 mutex&
392 operator= (mutex&&) = delete;
393
401 ~mutex ();
402
417 bool
418 operator== (const mutex& rhs) const;
419
424 public:
425
453 lock (void);
454
479 try_lock (void);
480
504
518 unlock (void);
519
527 prio_ceiling (void) const;
528
553 thread::priority_t* old_prio_ceiling = nullptr);
554
565 consistent (void);
566
573 thread*
574 owner (void);
575
580 type_t
581 type (void);
582
588 protocol (void);
589
595 robustness (void);
596
604 reset (void);
605
610 protected:
611
612 friend class thread;
613
628 void
629 internal_init_ (void);
630
637 internal_try_lock_ (thread* th);
638
645 internal_unlock_ (thread* th);
646
647 void
648 internal_mark_owner_dead_ (void);
649
658 protected:
659
669 // Can be updated in different thread contexts.
670 thread* volatile owner_ = nullptr;
671
672#if !defined(OS_USE_RTOS_PORT_MUTEX)
674 clock* clock_ = nullptr;
675#endif
676
677 public:
678
679 // Intrusive node used to link this mutex to the owning thread.
680 // This is used for priority inheritance and robustness.
681 utils::double_list_links owner_links_;
682
683 protected:
684
685#if defined(OS_USE_RTOS_PORT_MUTEX)
686 friend class port::mutex;
687 os_mutex_port_data_t port_;
688#endif
689
690 // Can be updated in different thread contexts.
691 volatile count_t count_ = 0;
692
693 // Can be updated in different thread contexts.
694 volatile thread::priority_t initial_prio_ceiling_ =
696 volatile thread::priority_t prio_ceiling_ = thread::priority::highest;
697 volatile thread::priority_t boosted_prio_ = thread::priority::none;
698
699 bool owner_dead_ = false;
700 bool consistent_ = true;
701 bool recoverable_ = true;
702
703 // Constants set during construction.
704 const type_t type_; // normal, errorcheck, recursive
705 const protocol_t protocol_; // none, inherit, protect
706 const robustness_t robustness_; // stalled, robust
707 const count_t max_count_;
708
709 // Add more internal data.
710
719 };
720
726 class mutex_recursive : public mutex
727 {
728 public:
729
739
743 mutex_recursive (const char* name, const attributes& attr =
745
750 // The rule of five.
751 mutex_recursive (const mutex_recursive&) = delete;
752 mutex_recursive (mutex_recursive&&) = delete;
754 operator= (const mutex_recursive&) = delete;
756 operator= (mutex_recursive&&) = delete;
757
766
781 bool
782 operator== (const mutex_recursive& rhs) const;
783
788 };
789
790#pragma GCC diagnostic pop
791
792 // ==========================================================================
793
794 } /* namespace rtos */
795} /* namespace os */
796
797// ===== Inline & template implementations ====================================
798
799namespace os
800{
801 namespace rtos
802 {
803
804 // ========================================================================
805
806 constexpr
808 {
809 ;
810 }
811
816 constexpr
818 mx_type (type)
819 {
820 ;
821 }
822
827 // ========================================================================
828 constexpr
831 { type::recursive } // Use the protected constructor.
832 {
833 ;
834 }
835
836 // ========================================================================
837
842 inline bool
843 mutex::operator== (const mutex& rhs) const
844 {
845 return this == &rhs;
846 }
847
851 inline thread*
853 {
854 return owner_;
855 }
856
860 inline mutex::type_t
862 {
863 return type_;
864 }
865
869 inline mutex::protocol_t
871 {
872 return protocol_;
873 }
874
880 {
881 return robustness_;
882 }
883
884 // ========================================================================
885
886 inline
888 mutex
889 { attr }
890 {
891 ;
892 }
893
894 inline
895 mutex_recursive::mutex_recursive (const char* name, const attributes& attr) :
896 mutex
897 { name, attr }
898 {
899 ;
900 }
901
902 inline
904 {
905 ;
906 }
907
912 inline bool
914 {
915 return this == &rhs;
916 }
917
918 } /* namespace rtos */
919} /* namespace os */
920
921#pragma GCC diagnostic pop
922
923// ----------------------------------------------------------------------------
924
925#endif /* __cplusplus */
926
927// ----------------------------------------------------------------------------
928
929#endif /* CMSIS_PLUS_RTOS_OS_MUTEX_H_ */
Generic clock.
Definition os-clocks.h:66
Base class for attributes.
Definition os-decls.h:577
Base class for named system objects.
Definition os-decls.h:459
const char * name(void) const
Get object name.
Definition os-decls.h:774
Priority ordered list of threads.
Definition os-lists.h:550
Recursive mutex attributes.
Definition os-mutex.h:323
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:829
Mutex attributes.
Definition os-mutex.h:219
count_t mx_max_count
Attribute with the mutex maximum recursive count.
Definition os-mutex.h:299
constexpr attributes()
Construct a mutex attributes object instance.
Definition os-mutex.h:807
type_t mx_type
Attribute with the mutex type.
Definition os-mutex.h:294
robustness_t mx_robustness
Attribute with the mutex robustness.
Definition os-mutex.h:289
~attributes()=default
Destruct the mutex attributes object instance.
thread::priority_t mx_priority_ceiling
Attribute with the mutex priority ceiling.
Definition os-mutex.h:279
attributes & operator=(const attributes &)=default
protocol_t mx_protocol
Attribute with the mutex protocol.
Definition os-mutex.h:284
attributes(attributes &&)=default
attributes(const attributes &)=default
POSIX compliant recursive mutex.
Definition os-mutex.h:727
mutex_recursive(const attributes &attr=initializer_recursive)
Construct a recursive mutex object instance.
Definition os-mutex.h:887
bool operator==(const mutex_recursive &rhs) const
Compare mutexes.
Definition os-mutex.h:913
~mutex_recursive()
Destruct the recursive mutex object instance.
Definition os-mutex.h:903
POSIX compliant mutex.
Definition os-mutex.h:65
result_t reset(void)
Reset the mutex.
result_t lock(void)
Lock/acquire the mutex.
Definition os-mutex.cpp:953
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:843
~mutex()
Destruct the mutex object instance.
Definition os-mutex.cpp:538
result_t consistent(void)
Mark mutex as consistent.
thread * owner(void)
Get the thread that owns the mutex.
Definition os-mutex.h:852
result_t unlock(void)
Unlock/release the mutex.
POSIX compliant thread, using the default RTOS allocator.
Definition os-thread.h:247
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition os-clocks.h:83
uint8_t type_t
Type of variables holding mutex behaviours.
Definition os-mutex.h:159
uint16_t count_t
Type of variables holding mutex recursion counters.
Definition os-mutex.h:203
static const attributes_recursive initializer_recursive
Default recursive mutex initialiser.
Definition os-mutex.h:362
uint8_t protocol_t
Type of variables holding mutex protocols.
Definition os-mutex.h:72
uint8_t robustness_t
Type of variables holding mutex robustness.
Definition os-mutex.h:118
static constexpr count_t max_count
Constant with the maximum value for the recursion counter.
Definition os-mutex.h:209
static const attributes initializer_normal
Default normal mutex initialiser.
Definition os-mutex.h:313
uint8_t priority_t
Type of variables holding thread priorities.
Definition os-thread.h:268
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:110
System namespace.
Mutex protocols.
Definition os-mutex.h:80
@ max_
Maximum value, for validation purposes.
Definition os-mutex.h:110
@ default_
Default value. Differs from POSIX, which uses none.
Definition os-mutex.h:105
@ none
Priority and scheduling not affected by mutex ownership.
Definition os-mutex.h:90
@ inherit
Inherit priority from highest priority thread.
Definition os-mutex.h:95
@ protect
Execute at the highest priority.
Definition os-mutex.h:100
Mutex robustness.
Definition os-mutex.h:126
@ max_
Maximum value, for validation purposes.
Definition os-mutex.h:151
@ stalled
Normal robustness.
Definition os-mutex.h:136
@ default_
Default value.
Definition os-mutex.h:146
@ robust
Enhanced robustness at thread termination.
Definition os-mutex.h:141
@ max_
Maximum value, for validation purposes.
Definition os-mutex.h:195
@ normal
Normal mutex behaviour.
Definition os-mutex.h:177
@ default_
Default value.
Definition os-mutex.h:190
@ errorcheck
Check mutex behaviour.
Definition os-mutex.h:181
@ recursive
Recursive mutex behaviour.
Definition os-mutex.h:185