µ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-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#ifndef CMSIS_PLUS_RTOS_OS_SEMAPHORE_H_
14#define CMSIS_PLUS_RTOS_OS_SEMAPHORE_H_
15
16// ----------------------------------------------------------------------------
17
18#if defined(__cplusplus)
19
20// ----------------------------------------------------------------------------
21
22#if defined(OS_USE_OS_APP_CONFIG_H)
23#include <cmsis-plus/os-app-config.h>
24#endif
25
27
28// ----------------------------------------------------------------------------
29
30#pragma GCC diagnostic push
31#if defined(__clang__)
32#pragma clang diagnostic ignored "-Wc++98-compat"
33#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
34#endif
35
36// ----------------------------------------------------------------------------
37
38namespace os
39{
40 namespace rtos
41 {
42 // ========================================================================
43
44#pragma GCC diagnostic push
45#if defined(__clang__)
46#pragma clang diagnostic ignored "-Wpadded"
47#elif defined(__GNUC__)
48#pragma GCC diagnostic ignored "-Wpadded"
49#endif
50
57 {
58 public:
59
67 using count_t = int16_t;
68
75 static constexpr count_t max_count_value = 0x7FFF;
76
77 // ======================================================================
78
85 {
86 public:
87
98 constexpr
99 attributes ();
100
101 protected:
102
107 constexpr
109
114 public:
115
116 // The rule of five.
117 attributes (const attributes&) = default;
118 attributes (attributes&&) = default;
120 operator= (const attributes&) = default;
122 operator= (attributes&&) = default;
123
127 ~attributes () = default;
128
133 public:
134
140 // Public members; no accessors and mutators required.
141 // Warning: must match the type & order of the C file header.
146
151
152 // Add more attributes here.
153
158 }; /* class attributes */
159
160 // ======================================================================
167 {
168 public:
169
179 constexpr
181
182 // The rule of five.
186 operator= (const attributes_binary&) = default;
189
193 ~attributes_binary () = default;
194
199 }; /* class attributes_binary */
200
206
207 // ======================================================================
208
215 {
216 public:
217
228 constexpr
230
231 // The rule of five.
238
243
248 }; /* class attributes_counting */
249
250 // ======================================================================
261
267 semaphore (const char* name, const attributes& attr = initializer_binary);
268
269 protected:
270
275 semaphore (const char* name, const count_t max_value,
276 const count_t initial_value, const attributes& attr =
278
283 public:
284
289 // The rule of five.
290 semaphore (const semaphore&) = delete;
291 semaphore (semaphore&&) = delete;
292 semaphore&
293 operator= (const semaphore&) = delete;
294 semaphore&
295 operator= (semaphore&&) = delete;
296
304 ~semaphore ();
305
320 bool
321 operator== (const semaphore& rhs) const;
322
327 public:
328
344 post (void);
345
358 wait (void);
359
373 try_wait (void);
374
391
398 count_t
399 value (void) const;
400
409 reset (void);
410
417 count_t
418 initial_value (void) const;
419
426 count_t
427 max_value (void) const;
428
433 protected:
434
449 void
450 internal_init_ (void);
451
452 bool
453 internal_try_wait_ (void);
454
463 protected:
464
474#if !defined(OS_USE_RTOS_PORT_SEMAPHORE)
476 clock* clock_ = nullptr;
477#endif
478
479#if defined(OS_USE_RTOS_PORT_SEMAPHORE)
480 friend class port::semaphore;
481 os_semaphore_port_data_t port_;
482#endif
483
484 // Constant set during construction.
485 const count_t max_value_ = max_count_value;
486
487 const count_t initial_value_ = 0;
488
489 // Can be updated in different contexts (interrupts or threads)
490 volatile count_t count_ = 0;
491
492 // Add more internal data.
493
502 };
503
504 // ========================================================================
505
512 {
513 public:
514
525
531 semaphore_binary (const char* name, const count_t initial_value);
532
537 // The rule of five.
538 semaphore_binary (const semaphore_binary&) = delete;
541 operator= (const semaphore_binary&) = delete;
543 operator= (semaphore_binary&&) = delete;
544
553
568 bool
569 operator== (const semaphore_binary& rhs) const;
570
575 };
576
577 // ========================================================================
578
585 {
586 public:
587
599
606 semaphore_counting (const char* name, const count_t max_value,
607 const count_t initial_value);
608
613 // The rule of five.
614 semaphore_counting (const semaphore_counting&) = delete;
617 operator= (const semaphore_counting&) = delete;
619 operator= (semaphore_counting&&) = delete;
620
629
644 bool
646
651 };
652
653#pragma GCC diagnostic pop
654
655 // ==========================================================================
656
657 } /* namespace rtos */
658} /* namespace os */
659
660// ===== Inline & template implementations ====================================
661
662namespace os
663{
664 namespace rtos
665 {
666 // ========================================================================
667
668 constexpr
670 {
671 }
672
677 constexpr
679 sm_max_value (max_value), //
680 sm_initial_value (initial_value)
681 {
682 }
683
688 // ========================================================================
689 constexpr
692 { 1, initial_value } // Use the protected constructor.
693 {
694 }
695
696 // ========================================================================
697
698 constexpr
702 { max_value, initial_value } // Use the protected constructor.
703 {
704 }
705
706 // ========================================================================
707
735 inline
738 { nullptr, attr }
739 {
740 }
741
746 inline bool
748 {
749 return this == &rhs;
750 }
751
758 inline semaphore::count_t
760 {
761 return initial_value_;
762 }
763
770 inline semaphore::count_t
772 {
773 return max_value_;
774 }
775
776 // ========================================================================
777
796 inline
799 { nullptr, 1, initial_value, initializer_binary }
800 {
801 }
802
821 inline
823 const count_t initial_value) :
825 { name, 1, initial_value }
826 {
827 }
828
847 inline
849 {
850 }
851
852 // ========================================================================
853
872 inline
874 const count_t initial_value) :
876 { nullptr, max_value, initial_value }
877 {
878 }
879
898 inline
900 const count_t max_value,
901 const count_t initial_value) :
904 {
905 }
906
925 inline
927 {
928 }
929
930 // ========================================================================
931
932 } /* namespace rtos */
933} /* namespace os */
934
935
936#pragma GCC diagnostic pop
937
938// ----------------------------------------------------------------------------
939
940#endif /* __cplusplus */
941
942// ----------------------------------------------------------------------------
943
944#endif /* CMSIS_PLUS_RTOS_OS_SEMAPHORE_H_ */
Generic clock.
Definition os-clocks.h:59
Base class for attributes.
Definition os-decls.h:563
Base class for named system objects.
Definition os-decls.h:445
const char * name(void) const
Get object name.
Definition os-decls.h:759
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:76
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:96
System namespace.