Beamlib  3.0.1
This is the Beam C++ class library.
BSemaphore.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BSemaphore.h Semaphore Classes
3  * T.Barnaby, BEAM Ltd, 2012-11-17
4  * Copyright (c) 2022 All Right Reserved, Beam Ltd, https://www.beam.ltd.uk
5  * For license see LICENSE.txt at the root of the beamlib source tree.
6  *******************************************************************************
7  */
8 #ifndef BSemaphore_h
9 #define BSemaphore_h 1
10 
11 #include <BTypes.h>
12 #include <BMutex.h>
13 #include <semaphore.h>
14 
16 class BSemaphore {
17 public:
18  BSemaphore();
19  BSemaphore(const BSemaphore& semaphore);
20  ~BSemaphore();
21 
22  Bool wait(BTimeout timeoutUs = BTimeoutForever);
23  void set();
24 
25  int getValue() const;
26  BSemaphore& operator=(const BSemaphore& semaphore);
27 
28 private:
29  sem_t osema;
30 };
31 
34 public:
36  BSemaphoreBool(const BSemaphoreBool& semaphore);
38 
39  void set(Bool on = 1);
40  void clear();
41  Bool wait(Bool v = 1, BTimeout timeoutUs = BTimeoutForever);
42  Bool value();
43 
44  operator int();
45  int operator==(Bool on);
47 
48 private:
49  BSemaphore osema;
50  volatile Bool ovalue;
51 };
52 
55 public:
57  BSemaphoreCount(const BSemaphoreCount& semaphore);
59 
60  void setValue(BUInt v);
61  void add(int v = 1);
62  Bool wait(BUInt v = 1, BTimeout timeoutUs = BTimeoutForever);
63  Bool take(BUInt v = 1, BTimeout timeoutUs = BTimeoutForever);
64 
65  BUInt value();
66 
67  BSemaphoreCount& operator=(const BSemaphoreCount& semaphore);
68 
69 private:
70  BMutex olock;
71  BSemaphore osema;
72  volatile BUInt ovalue;
73 };
74 #endif
const BTimeout BTimeoutForever
Definition: BTypes.h:46
BUInt32 BUInt
Definition: BTypes.h:33
bool Bool
Definition: BTypes.h:18
BUInt32 BTimeout
Definition: BTypes.h:45
Mutex class. Note these are recursive Mutexes and so you need to make sure the number of unlocks equa...
Definition: BMutex.h:14
Boolean semaphore.
Definition: BSemaphore.h:33
int operator==(Bool on)
Definition: BSemaphore.cpp:99
void clear()
Definition: BSemaphore.cpp:78
Bool wait(Bool v=1, BTimeout timeoutUs=BTimeoutForever)
Wait for the semaphore.
Definition: BSemaphore.cpp:83
BSemaphoreBool()
Definition: BSemaphore.cpp:62
~BSemaphoreBool()
Definition: BSemaphore.cpp:70
BSemaphoreBool & operator=(Bool on)
Definition: BSemaphore.cpp:104
void set(Bool on=1)
Definition: BSemaphore.cpp:73
Bool value()
Definition: BSemaphore.cpp:91
Integer counting semaphore.
Definition: BSemaphore.h:54
void setValue(BUInt v)
Definition: BSemaphore.cpp:154
BSemaphoreCount()
Definition: BSemaphore.cpp:112
BUInt value()
Definition: BSemaphore.cpp:161
Bool take(BUInt v=1, BTimeout timeoutUs=BTimeoutForever)
Take for the semaphore.
Definition: BSemaphore.cpp:143
BSemaphoreCount & operator=(const BSemaphoreCount &semaphore)
Definition: BSemaphore.cpp:120
void add(int v=1)
Set the semaphore.
Definition: BSemaphore.cpp:128
~BSemaphoreCount()
Definition: BSemaphore.cpp:125
Bool wait(BUInt v=1, BTimeout timeoutUs=BTimeoutForever)
Wait for the semaphore.
Definition: BSemaphore.cpp:135
Base Semaphore class.
Definition: BSemaphore.h:16
Bool wait(BTimeout timeoutUs=BTimeoutForever)
Wait for the semaphore.
Definition: BSemaphore.cpp:33
~BSemaphore()
Definition: BSemaphore.cpp:25
int getValue() const
Definition: BSemaphore.cpp:53
BSemaphore()
Definition: BSemaphore.cpp:11
BSemaphore & operator=(const BSemaphore &semaphore)
Definition: BSemaphore.cpp:19
void set()
Set the semaphore.
Definition: BSemaphore.cpp:29