µ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++ 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_SEMAPHORE_H_
13#define CMSIS_PLUS_RTOS_OS_SEMAPHORE_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(__cplusplus)
18
19// ----------------------------------------------------------------------------
20
21#if defined(OS_USE_OS_APP_CONFIG_H)
22#include <cmsis-plus/os-app-config.h>
23#endif
24
26
27// ----------------------------------------------------------------------------
28
29#pragma GCC diagnostic push
30#if defined(__clang__)
31#pragma clang diagnostic ignored "-Wc++98-compat"
32#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
33#endif
34
35// ----------------------------------------------------------------------------
36
37namespace os
38{
39 namespace rtos
40 {
41 // ========================================================================
42
43#pragma GCC diagnostic push
44#if defined(__clang__)
45#pragma clang diagnostic ignored "-Wpadded"
46#elif defined(__GNUC__)
47#pragma GCC diagnostic ignored "-Wpadded"
48#endif
49
56 {
57 public:
66 using count_t = int16_t;
67
75 static constexpr count_t max_count_value = 0x7FFF;
76
77 // ======================================================================
78
85 {
86 public:
97 constexpr attributes ();
98
99 protected:
105
110 public:
111 // The rule of five.
112 attributes (const attributes&) = default;
113 attributes (attributes&&) = default;
116 = default;
119 = default;
120
124 ~attributes () = default;
125
130 public:
136 // Public members; no accessors and mutators required.
137 // Warning: must match the type & order of the C file header.
142
147
148 // Add more attributes here.
149
154 }; /* class attributes */
155
156 // ======================================================================
173 {
174 public:
185
186 // The rule of five.
191 = default;
194 = default;
195
199 ~attributes_binary () = default;
200
205 }; /* class attributes_binary */
206
212
213 // ======================================================================
214
221 {
222 public:
235
236 // The rule of five.
241 = default;
244 = default;
245
250
255 }; /* class attributes_counting */
256
257 // ======================================================================
268
274 semaphore (const char* name,
275 const attributes& attr = initializer_binary);
276
277 protected:
282 semaphore (const char* name, const count_t max_value,
284 const attributes& attr = initializer_binary);
285
290 public:
295 // The rule of five.
296 semaphore (const semaphore&) = delete;
297 semaphore (semaphore&&) = delete;
298 semaphore&
299 operator= (const semaphore&)
300 = delete;
301 semaphore&
302 operator= (semaphore&&)
303 = delete;
304
312 ~semaphore ();
313
328 bool
329 operator== (const semaphore& rhs) const;
330
335 public:
351 post (void);
352
365 wait (void);
366
380 try_wait (void);
381
398
405 count_t
406 value (void) const;
407
416 reset (void);
417
424 count_t
425 initial_value (void) const;
426
433 count_t
434 max_value (void) const;
435
440 protected:
455 void
456 internal_init_ (void);
457
458 bool
459 internal_try_wait_ (void);
460
469 protected:
479#if !defined(OS_USE_RTOS_PORT_SEMAPHORE)
481 clock* clock_ = nullptr;
482#endif
483
484#if defined(OS_USE_RTOS_PORT_SEMAPHORE)
485 friend class port::semaphore;
486 os_semaphore_port_data_t port_;
487#endif
488
489 // Constant set during construction.
490 const count_t max_value_ = max_count_value;
491
492 const count_t initial_value_ = 0;
493
494 // Can be updated in different contexts (interrupts or threads)
495 volatile count_t count_ = 0;
496
497 // Add more internal data.
498
506 };
507
508 // ========================================================================
509
516 {
517 public:
528
534 semaphore_binary (const char* name, const count_t initial_value);
535
540 // The rule of five.
541 semaphore_binary (const semaphore_binary&) = delete;
544 operator= (const semaphore_binary&)
545 = delete;
547 operator= (semaphore_binary&&)
548 = delete;
549
558
573 bool
574 operator== (const semaphore_binary& rhs) const;
575
579 };
580
581 // ========================================================================
582
589 {
590 public:
602 const count_t initial_value);
603
610 semaphore_counting (const char* name, const count_t max_value,
611 const count_t initial_value);
612
617 // The rule of five.
618 semaphore_counting (const semaphore_counting&) = delete;
621 operator= (const semaphore_counting&)
622 = delete;
624 operator= (semaphore_counting&&)
625 = delete;
626
635
650 bool
652
656 };
657
658#pragma GCC diagnostic pop
659
660 // ========================================================================
661
662 } /* namespace rtos */
663} /* namespace os */
664
665// ===== Inline & template implementations ====================================
666
667namespace os
668{
669 namespace rtos
670 {
671 // ========================================================================
672
674 {
675 }
676
683 : sm_max_value (max_value), //
684 sm_initial_value (initial_value)
685 {
686 }
687
692 // ========================================================================
695 : attributes{ 1, initial_value } // Use the protected constructor.
696 {
697 }
698
699 // ========================================================================
700
704 // Use the protected constructor.
705 {
706 }
707
708 // ========================================================================
709
740 inline semaphore::semaphore (const attributes& attr)
741 : semaphore{ nullptr, attr }
742 {
743 }
744
749 inline bool
751 {
752 return this == &rhs;
753 }
754
761 inline semaphore::count_t
763 {
764 return initial_value_;
765 }
766
773 inline semaphore::count_t
775 {
776 return max_value_;
777 }
778
779 // ========================================================================
780
802 inline semaphore_binary::semaphore_binary (const count_t initial_value)
803 : semaphore{ nullptr, 1, initial_value, initializer_binary }
804 {
805 }
806
828 inline semaphore_binary::semaphore_binary (const char* name,
829 const count_t initial_value)
830 : semaphore{ name, 1, initial_value }
831 {
832 }
833
856 {
857 }
858
859 // ========================================================================
860
883 const count_t initial_value)
884 : semaphore{ nullptr, max_value, initial_value }
885 {
886 }
887
910 const count_t max_value,
911 const count_t initial_value)
912 : semaphore{ name, max_value, initial_value }
913 {
914 }
915
938 {
939 }
940
941 // ========================================================================
942
943 } /* namespace rtos */
944} /* namespace os */
945
946#pragma GCC diagnostic pop
947
948// ----------------------------------------------------------------------------
949
950#endif /* __cplusplus */
951
952// ----------------------------------------------------------------------------
953
954#endif /* CMSIS_PLUS_RTOS_OS_SEMAPHORE_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
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:78
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:95
System namespace.