Beamlib  3.0.1
This is the Beam C++ class library.
BQueue.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * BQueue.h Queue Classes
3  * T.Barnaby, BEAM Ltd, 2014-07-23
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  * Simple thread safe queue.
9  */
10 #ifndef BQueue_h
11 #define BQueue_h 1
12 
13 #include <BTypes.h>
14 #include <BError.h>
15 #include <BList.h>
16 #include <BMutex.h>
17 #include <BCondInt.h>
18 
20 template <class T> class BQueue : private BList<T>{
21 public:
24 
25  void clear();
26 
28  BError write(const T& v, BTimeout timeout = BTimeoutForever);
29 
31  BError read(T& v, BTimeout timeout = BTimeoutForever);
32 
33 private:
34  BMutex olock;
35  BUInt osize;
36  BCondInt onumber;
37 };
38 
39 // BQueueInt class
41 
42 // BQueue class implementation
43 template <class T> BQueue<T>::BQueue(BUInt size){
44  osize = size;
45 }
46 
47 template <class T> BQueue<T>::~BQueue(){
48 }
49 
50 template <class T> void BQueue<T>::clear(){
51  olock.lock();
53  olock.unlock();
54 }
55 
56 template <class T> BUInt BQueue<T>::writeAvailable() const{
57  return osize - onumber.value();
58 }
59 
60 template <class T> BUInt BQueue<T>::readAvailable() const{
61  return onumber.value();
62 }
63 
64 template <class T> BError BQueue<T>::write(const T& v, BTimeout timeout){
65  BError err;
66 
67  if(!onumber.waitLessThanOrEqual(osize - 1, 0, timeout))
68  return err.set(ErrorTimeout, "Timeout");
69 
70  olock.lock();
72  olock.unlock();
73  onumber.increment();
74 
75  return err;
76 }
77 
78 template <class T> BError BQueue<T>::read(T& v, BTimeout timeout){
79  BError err;
80 
81  if(!onumber.waitMoreThanOrEqual(1, 1, timeout))
82  return err.set(ErrorTimeout, "Timeout");
83 
84  olock.lock();
85  v = BList<T>::queueGet();
86  olock.unlock();
87 
88  return err;
89 }
90 
91 #endif
@ ErrorTimeout
Definition: BError.h:23
BQueue< BInt32 > BQueueInt
Definition: BQueue.h:40
const BTimeout BTimeoutForever
Definition: BTypes.h:46
BUInt32 BUInt
Definition: BTypes.h:33
BUInt32 BTimeout
Definition: BTypes.h:45
Thread conditional value.
Definition: BCondInt.h:16
Error return class. This class is used to return the error status from a function....
Definition: BError.h:31
BError & set(int errNo, BString errStr="")
Set error number and message.
Definition: BError.cpp:24
Template based list class.
Definition: BList.h:31
unsigned int size() const
Number of items in list.
Definition: BList_func.h:99
virtual void clear()
Clear the list.
Definition: BList_func.h:115
void queueAdd(const T &i)
Add item to end of list.
Definition: BList_func.h:195
T queueGet()
Get item from front of list deleteing item.
Definition: BList_func.h:199
Mutex class. Note these are recursive Mutexes and so you need to make sure the number of unlocks equa...
Definition: BMutex.h:14
Provides a thread save queue of objects that can be used to communicate between threads.
Definition: BQueue.h:20
BQueue(BUInt size)
Definition: BQueue.h:43
~BQueue()
Definition: BQueue.h:47
BError read(T &v, BTimeout timeout=BTimeoutForever)
Get an item from the queue.
Definition: BQueue.h:78
BUInt writeAvailable() const
Definition: BQueue.h:56
void clear()
Clear the queue.
Definition: BQueue.h:50
BUInt readAvailable() const
Definition: BQueue.h:60
BError write(const T &v, BTimeout timeout=BTimeoutForever)
Append an item onto the queue.
Definition: BQueue.h:64