µOS++ IIIe Reference  v6.3.15
“Perfekt ist nicht gut genug”
The third edition of µOS++, a POSIX inspired open source system, written in C++.
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 
36 
37 // ----------------------------------------------------------------------------
38 
39 namespace os
40 {
41  namespace rtos
42  {
43  // ========================================================================
44 
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wpadded"
47 
54  {
55  public:
56 
64  using count_t = int16_t;
65 
72  static constexpr count_t max_count_value = 0x7FFF;
73 
74  // ======================================================================
75 
82  {
83  public:
84 
95  constexpr
96  attributes ();
97 
98  protected:
99 
104  constexpr
106 
111  public:
112 
113  // The rule of five.
114  attributes (const attributes&) = default;
115  attributes (attributes&&) = default;
116  attributes&
117  operator= (const attributes&) = default;
118  attributes&
119  operator= (attributes&&) = default;
120 
124  ~attributes () = default;
125 
130  public:
131 
137  // Public members; no accessors and mutators required.
138  // Warning: must match the type & order of the C file header.
143 
148 
149  // Add more attributes here.
150 
155  }; /* class attributes */
156 
157  // ======================================================================
164  {
165  public:
166 
176  constexpr
178 
179  // The rule of five.
180  attributes_binary (const attributes_binary&) = default;
181  attributes_binary (attributes_binary&&) = default;
182  attributes_binary&
183  operator= (const attributes_binary&) = default;
184  attributes_binary&
185  operator= (attributes_binary&&) = default;
186 
190  ~attributes_binary () = default;
191 
196  }; /* class attributes_binary */
197 
203 
204  // ======================================================================
205 
212  {
213  public:
214 
225  constexpr
227 
228  // The rule of five.
229  attributes_counting (const attributes_counting&) = default;
230  attributes_counting (attributes_counting&&) = default;
231  attributes_counting&
232  operator= (const attributes_counting&) = default;
233  attributes_counting&
234  operator= (attributes_counting&&) = default;
235 
239  ~attributes_counting () = default;
240 
245  }; /* class attributes_counting */
246 
247  // ======================================================================
257  semaphore (const attributes& attr = initializer_binary);
258 
264  semaphore (const char* name, const attributes& attr = initializer_binary);
265 
266  protected:
267 
272  semaphore (const char* name, const count_t max_value,
273  const count_t initial_value, const attributes& attr =
274  initializer_binary);
275 
280  public:
281 
286  // The rule of five.
287  semaphore (const semaphore&) = delete;
288  semaphore (semaphore&&) = delete;
289  semaphore&
290  operator= (const semaphore&) = delete;
291  semaphore&
292  operator= (semaphore&&) = delete;
293 
301  ~semaphore ();
302 
317  bool
318  operator== (const semaphore& rhs) const;
319 
324  public:
325 
340  result_t
341  post (void);
342 
354  result_t
355  wait (void);
356 
369  result_t
370  try_wait (void);
371 
386  result_t
387  timed_wait (clock::duration_t timeout);
388 
395  count_t
396  value (void) const;
397 
405  result_t
406  reset (void);
407 
414  count_t
415  initial_value (void) const;
416 
423  count_t
424  max_value (void) const;
425 
430  protected:
431 
446  void
447  internal_init_ (void);
448 
449  bool
450  internal_try_wait_ (void);
451 
460  protected:
461 
471 #if !defined(OS_USE_RTOS_PORT_SEMAPHORE)
473  clock* clock_ = nullptr;
474 #endif
475 
476 #if defined(OS_USE_RTOS_PORT_SEMAPHORE)
477  friend class port::semaphore;
478  os_semaphore_port_data_t port_;
479 #endif
480 
481  // Constant set during construction.
482  const count_t max_value_ = max_count_value;
483 
484  const count_t initial_value_ = 0;
485 
486  // Can be updated in different contexts (interrupts or threads)
487  volatile count_t count_ = 0;
488 
489  // Add more internal data.
490 
499  };
500 
501  // ========================================================================
502 
509  {
510  public:
511 
522 
528  semaphore_binary (const char* name, const count_t initial_value);
529 
534  // The rule of five.
535  semaphore_binary (const semaphore_binary&) = delete;
536  semaphore_binary (semaphore_binary&&) = delete;
538  operator= (const semaphore_binary&) = delete;
540  operator= (semaphore_binary&&) = delete;
541 
549  ~semaphore_binary ();
550 
565  bool
566  operator== (const semaphore_binary& rhs) const;
567 
572  };
573 
574  // ========================================================================
575 
582  {
583  public:
584 
596 
603  semaphore_counting (const char* name, const count_t max_value,
604  const count_t initial_value);
605 
610  // The rule of five.
611  semaphore_counting (const semaphore_counting&) = delete;
614  operator= (const semaphore_counting&) = delete;
616  operator= (semaphore_counting&&) = delete;
617 
625  ~semaphore_counting ();
626 
641  bool
642  operator== (const semaphore_counting& rhs) const;
643 
648  };
649 
650 #pragma GCC diagnostic pop
651 
652  // ==========================================================================
653 
654  } /* namespace rtos */
655 } /* namespace os */
656 
657 // ===== Inline & template implementations ====================================
658 
659 namespace os
660 {
661  namespace rtos
662  {
663  // ========================================================================
664 
665  constexpr
667  {
668  ;
669  }
670 
675  constexpr
677  sm_max_value (max_value), //
678  sm_initial_value (initial_value)
679  {
680  ;
681  }
682 
687  // ========================================================================
688  constexpr
690  attributes
691  { 1, initial_value } // Use the protected constructor.
692  {
693  ;
694  }
695 
696  // ========================================================================
697 
698  constexpr
700  count_t initial_value) :
701  attributes
702  { max_value, initial_value } // Use the protected constructor.
703  {
704  ;
705  }
706 
707  // ========================================================================
708 
736  inline
738  semaphore
739  { nullptr, attr }
740  {
741  ;
742  }
743 
748  inline bool
750  {
751  return this == &rhs;
752  }
753 
761  inline semaphore::count_t
763  {
764  return initial_value_;
765  }
766 
774  inline semaphore::count_t
775  semaphore::max_value (void) const
776  {
777  return max_value_;
778  }
779 
780  // ========================================================================
781 
800  inline
802  semaphore
803  { nullptr, 1, initial_value, initializer_binary }
804  {
805  ;
806  }
807 
826  inline
828  const count_t initial_value) :
829  semaphore
830  { name, 1, initial_value }
831  {
832  ;
833  }
834 
853  inline
855  {
856  ;
857  }
858 
859  // ========================================================================
860 
879  inline
881  const count_t initial_value) :
882  semaphore
883  { nullptr, max_value, initial_value }
884  {
885  ;
886  }
887 
906  inline
908  const count_t max_value,
909  const count_t initial_value) :
910  semaphore
911  { name, max_value, initial_value }
912  {
913  ;
914  }
915 
934  inline
936  {
937  ;
938  }
939 
940  // ========================================================================
941 
942  } /* namespace rtos */
943 } /* namespace os */
944 
945 // ----------------------------------------------------------------------------
946 
947 #endif /* __cplusplus */
948 
949 #endif /* CMSIS_PLUS_RTOS_OS_SEMAPHORE_H_ */
bool operator==(const semaphore &rhs) const
Compare semaphores.
Definition: os-semaphore.h:749
count_t sm_initial_value
Semaphore initial count value.
Definition: os-semaphore.h:147
~attributes()=default
Destruct the semaphore attributes object instance.
result_t post(void)
Post (unlock) the semaphore.
constexpr attributes()
Construct a semaphore attributes object instance.
Definition: os-semaphore.h:666
constexpr attributes_counting(count_t max_value, count_t initial_value)
Construct a counting semaphore attributes object instance.
Definition: os-semaphore.h:699
result_t reset(void)
Reset the semaphore.
POSIX compliant semaphore.
Definition: os-semaphore.h:53
Priority ordered list of threads.
Definition: os-lists.h:536
semaphore(const attributes &attr=initializer_binary)
Construct a semaphore object instance.
Definition: os-semaphore.h:737
Base class for attributes.
Definition: os-decls.h:562
System namespace.
static const attributes_binary initializer_binary
Default binary semaphore initialiser.
Definition: os-semaphore.h:202
semaphore_counting(const count_t max_value, const count_t initial_value)
Construct a binary semaphore object instance.
Definition: os-semaphore.h:880
Binary semaphore attributes.
Definition: os-semaphore.h:163
static constexpr count_t max_count_value
Maximum semaphore value.
Definition: os-semaphore.h:72
const char * name(void) const
Get object name.
Definition: os-decls.h:760
Base class for named system objects.
Definition: os-decls.h:444
Counting semaphore attributes.
Definition: os-semaphore.h:211
count_t sm_max_value
Semaphore max count value.
Definition: os-semaphore.h:142
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition: os-clocks.h:72
result_t wait(void)
Lock the semaphore, possibly waiting.
POSIX compliant binary semaphore.
Definition: os-semaphore.h:508
attributes & operator=(const attributes &)=default
count_t initial_value(void) const
Get the semaphore initial count value.
Definition: os-semaphore.h:762
int16_t count_t
Type of semaphore counter storage.
Definition: os-semaphore.h:64
~semaphore_binary()
Destruct the semaphore object instance.
Definition: os-semaphore.h:854
constexpr attributes_binary(count_t initial_value)
Construct a binary semaphore attributes object instance.
Definition: os-semaphore.h:689
~semaphore()
Destruct the semaphore object instance.
semaphore_binary(const count_t initial_value)
Construct a binary semaphore object instance.
Definition: os-semaphore.h:801
Semaphore attributes.
Definition: os-semaphore.h:81
result_t try_wait(void)
Try to lock the semaphore.
~semaphore_counting()
Destruct the semaphore object instance.
Definition: os-semaphore.h:935
Generic clock.
Definition: os-clocks.h:54
count_t value(void) const
Get the semaphore count value.
POSIX compliant counting semaphore.
Definition: os-semaphore.h:581
uint32_t result_t
Type of values returned by RTOS functions.
Definition: os-decls.h:96
result_t timed_wait(clock::duration_t timeout)
Timed wait to lock the semaphore.
count_t max_value(void) const
Get the semaphore maximum count value.
Definition: os-semaphore.h:775