µ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-semaphore.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_SEMAPHORE_H_
29#define CMSIS_PLUS_RTOS_OS_SEMAPHORE_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
75 using count_t = int16_t;
76
83 static constexpr count_t max_count_value = 0x7FFF;
84
85 // ======================================================================
86
93 {
94 public:
95
106 constexpr
107 attributes ();
108
109 protected:
110
115 constexpr
117
122 public:
123
124 // The rule of five.
125 attributes (const attributes&) = default;
126 attributes (attributes&&) = default;
128 operator= (const attributes&) = default;
130 operator= (attributes&&) = default;
131
135 ~attributes () = default;
136
141 public:
142
148 // Public members; no accessors and mutators required.
149 // Warning: must match the type & order of the C file header.
154
159
160 // Add more attributes here.
161
166 }; /* class attributes */
167
168 // ======================================================================
175 {
176 public:
177
187 constexpr
189
190 // The rule of five.
194 operator= (const attributes_binary&) = default;
197
201 ~attributes_binary () = default;
202
207 }; /* class attributes_binary */
208
214
215 // ======================================================================
216
223 {
224 public:
225
236 constexpr
238
239 // The rule of five.
246
251
256 }; /* class attributes_counting */
257
258 // ======================================================================
269
275 semaphore (const char* name, const attributes& attr = initializer_binary);
276
277 protected:
278
283 semaphore (const char* name, const count_t max_value,
284 const count_t initial_value, const attributes& attr =
286
291 public:
292
297 // The rule of five.
298 semaphore (const semaphore&) = delete;
299 semaphore (semaphore&&) = delete;
300 semaphore&
301 operator= (const semaphore&) = delete;
302 semaphore&
303 operator= (semaphore&&) = delete;
304
312 ~semaphore ();
313
328 bool
329 operator== (const semaphore& rhs) const;
330
335 public:
336
352 post (void);
353
366 wait (void);
367
381 try_wait (void);
382
399
406 count_t
407 value (void) const;
408
417 reset (void);
418
425 count_t
426 initial_value (void) const;
427
434 count_t
435 max_value (void) const;
436
441 protected:
442
457 void
458 internal_init_ (void);
459
460 bool
461 internal_try_wait_ (void);
462
471 protected:
472
482#if !defined(OS_USE_RTOS_PORT_SEMAPHORE)
484 clock* clock_ = nullptr;
485#endif
486
487#if defined(OS_USE_RTOS_PORT_SEMAPHORE)
488 friend class port::semaphore;
489 os_semaphore_port_data_t port_;
490#endif
491
492 // Constant set during construction.
493 const count_t max_value_ = max_count_value;
494
495 const count_t initial_value_ = 0;
496
497 // Can be updated in different contexts (interrupts or threads)
498 volatile count_t count_ = 0;
499
500 // Add more internal data.
501
510 };
511
512 // ========================================================================
513
520 {
521 public:
522
533
539 semaphore_binary (const char* name, const count_t initial_value);
540
545 // The rule of five.
546 semaphore_binary (const semaphore_binary&) = delete;
549 operator= (const semaphore_binary&) = delete;
551 operator= (semaphore_binary&&) = delete;
552
561
576 bool
577 operator== (const semaphore_binary& rhs) const;
578
583 };
584
585 // ========================================================================
586
593 {
594 public:
595
607
614 semaphore_counting (const char* name, const count_t max_value,
615 const count_t initial_value);
616
621 // The rule of five.
622 semaphore_counting (const semaphore_counting&) = delete;
625 operator= (const semaphore_counting&) = delete;
627 operator= (semaphore_counting&&) = delete;
628
637
652 bool
654
659 };
660
661#pragma GCC diagnostic pop
662
663 // ==========================================================================
664
665 } /* namespace rtos */
666} /* namespace os */
667
668// ===== Inline & template implementations ====================================
669
670namespace os
671{
672 namespace rtos
673 {
674 // ========================================================================
675
676 constexpr
678 {
679 ;
680 }
681
686 constexpr
688 sm_max_value (max_value), //
689 sm_initial_value (initial_value)
690 {
691 ;
692 }
693
698 // ========================================================================
699 constexpr
702 { 1, initial_value } // Use the protected constructor.
703 {
704 ;
705 }
706
707 // ========================================================================
708
709 constexpr
713 { max_value, initial_value } // Use the protected constructor.
714 {
715 ;
716 }
717
718 // ========================================================================
719
747 inline
750 { nullptr, attr }
751 {
752 ;
753 }
754
759 inline bool
761 {
762 return this == &rhs;
763 }
764
771 inline semaphore::count_t
773 {
774 return initial_value_;
775 }
776
783 inline semaphore::count_t
785 {
786 return max_value_;
787 }
788
789 // ========================================================================
790
809 inline
812 { nullptr, 1, initial_value, initializer_binary }
813 {
814 ;
815 }
816
835 inline
837 const count_t initial_value) :
839 { name, 1, initial_value }
840 {
841 ;
842 }
843
862 inline
864 {
865 ;
866 }
867
868 // ========================================================================
869
888 inline
890 const count_t initial_value) :
892 { nullptr, max_value, initial_value }
893 {
894 ;
895 }
896
915 inline
917 const count_t max_value,
918 const count_t initial_value) :
921 {
922 ;
923 }
924
943 inline
945 {
946 ;
947 }
948
949 // ========================================================================
950
951 } /* namespace rtos */
952} /* namespace os */
953
954
955#pragma GCC diagnostic pop
956
957// ----------------------------------------------------------------------------
958
959#endif /* __cplusplus */
960
961// ----------------------------------------------------------------------------
962
963#endif /* CMSIS_PLUS_RTOS_OS_SEMAPHORE_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
Binary semaphore attributes.
constexpr attributes_binary(count_t initial_value)
Construct a binary semaphore attributes object instance.
~attributes_binary()=default
Destruct the semaphore attributes object instance.
attributes_binary & operator=(const attributes_binary &)=default
attributes_binary(attributes_binary &&)=default
attributes_binary(const attributes_binary &)=default
Counting semaphore attributes.
attributes_counting & operator=(const attributes_counting &)=default
constexpr attributes_counting(count_t max_value, count_t initial_value)
Construct a counting semaphore attributes object instance.
attributes_counting(attributes_counting &&)=default
attributes_counting(const attributes_counting &)=default
~attributes_counting()=default
Destruct the semaphore attributes object instance.
Semaphore attributes.
count_t sm_max_value
Semaphore max count value.
~attributes()=default
Destruct the semaphore attributes object instance.
attributes(const attributes &)=default
constexpr attributes()
Construct a semaphore attributes object instance.
count_t sm_initial_value
Semaphore initial count value.
attributes & operator=(const attributes &)=default
attributes(attributes &&)=default
POSIX compliant binary semaphore.
~semaphore_binary()
Destruct the semaphore object instance.
bool operator==(const semaphore_binary &rhs) const
Compare semaphores.
semaphore_binary(const count_t initial_value)
Construct a binary semaphore object instance.
POSIX compliant counting semaphore.
~semaphore_counting()
Destruct the semaphore object instance.
bool operator==(const semaphore_counting &rhs) const
Compare semaphores.
semaphore_counting(const count_t max_value, const count_t initial_value)
Construct a binary semaphore object instance.
POSIX compliant semaphore.
result_t timed_wait(clock::duration_t timeout)
Timed wait to lock the semaphore.
result_t wait(void)
Lock the semaphore, possibly waiting.
result_t reset(void)
Reset the semaphore.
result_t post(void)
Post (unlock) the semaphore.
result_t try_wait(void)
Try to lock the semaphore.
~semaphore()
Destruct the semaphore object instance.
semaphore(const attributes &attr=initializer_binary)
Construct a semaphore object instance.
count_t max_value(void) const
Get the semaphore maximum count value.
count_t initial_value(void) const
Get the semaphore initial count value.
count_t value(void) const
Get the semaphore count value.
bool operator==(const semaphore &rhs) const
Compare semaphores.
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition os-clocks.h:83
static constexpr count_t max_count_value
Maximum semaphore value.
static const attributes_binary initializer_binary
Default binary semaphore initialiser.
int16_t count_t
Type of semaphore counter storage.
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:110
System namespace.