BDS Public
Beam-lib  2.16.3
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) 2012 All Right Reserved, Beam Ltd, http://www.beam.ltd.uk
5  *******************************************************************************
6  *
7  * Simple thread safe queue.
8  */
9 #ifndef BQueue_h
10 #define BQueue_h 1
11 
12 #include <BTypes.h>
13 #include <BError.h>
14 #include <BList.h>
15 #include <BMutex.h>
16 #include <BCondInt.h>
17 
19 template <class T> class BQueue : private BList<T>{
20 public:
21  BQueue(BUInt size);
22  ~BQueue();
23 
24  void clear();
25 
26  BUInt writeAvailable() const;
27  BError write(const T& v, BTimeout timeout = BTimeoutForever);
28 
29  BUInt readAvailable() const;
30  BError read(T& v, BTimeout timeout = BTimeoutForever);
31 
32 private:
33  BMutex olock;
34  BUInt osize;
35  BCondInt onumber;
36 };
37 
38 // BQueueInt class
40 
41 // BQueue class implementation
42 template <class T> BQueue<T>::BQueue(BUInt size){
43  osize = size;
44 }
45 
46 template <class T> BQueue<T>::~BQueue(){
47 }
48 
49 template <class T> void BQueue<T>::clear(){
50  olock.lock();
52  olock.unlock();
53 }
54 
55 template <class T> BUInt BQueue<T>::writeAvailable() const{
56  return osize - onumber.value();
57 }
58 
59 template <class T> BUInt BQueue<T>::readAvailable() const{
60  return onumber.value();
61 }
62 
63 template <class T> BError BQueue<T>::write(const T& v, BTimeout timeout){
64  BError err;
65 
66  if(!onumber.waitLessThanOrEqual(osize - 1, 0, timeout))
67  return err.set(ErrorTimeout, "Timeout");
68 
69  olock.lock();
71  olock.unlock();
72  onumber.increment();
73 
74  return err;
75 }
76 
77 template <class T> BError BQueue<T>::read(T& v, BTimeout timeout){
78  BError err;
79 
80  if(!onumber.waitMoreThanOrEqual(1, 1, timeout))
81  return err.set(ErrorTimeout, "Timeout");
82 
83  olock.lock();
84  v = BList<T>::queueGet();
85  olock.unlock();
86 
87  return err;
88 }
89 
90 #endif
BError & set(int errNo, BString errStr="")
Set error number and message.
Definition: BError.cpp:22
void queueAdd(const T &i)
Add item to end of list.
Definition: BList_func.h:184
Definition: BError.h:13
virtual void clear()
Clear the list.
Definition: BList_func.h:104
Queue class.
Definition: BQueue.h:19
Template based list class.
Definition: BList.h:30
BUInt32 BTimeout
Definition: BTypes.h:42
Thread conditional value.
Definition: BCondInt.h:14
BUInt readAvailable() const
Definition: BQueue.h:59
unsigned int size() const
Number of items in list.
Definition: BList_func.h:88
BUInt32 BUInt
Definition: BTypes.h:30
const BTimeout BTimeoutForever
Definition: BTypes.h:43
BQueue< BInt32 > BQueueInt
Definition: BQueue.h:39
Definition: BMutex.h:14
~BQueue()
Definition: BQueue.h:46
BQueue(BUInt size)
Definition: BQueue.h:42
void clear()
Clear the queue.
Definition: BQueue.h:49
T queueGet()
Get item from front of list deleteing item.
Definition: BList_func.h:188
Definition: BError.h:25
BError read(T &v, BTimeout timeout=BTimeoutForever)
Get an item from the queue.
Definition: BQueue.h:77
BUInt writeAvailable() const
Definition: BQueue.h:55
BError write(const T &v, BTimeout timeout=BTimeoutForever)
Append an item onto the queue.
Definition: BQueue.h:63