RDK Documentation (Open Sourced RDK Components)
EventQueue.hpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /*****************************************************************************/
21 /*!
22 \file
23 \brief This file defines interface of EventQueue class.
24 
25 */
26 /*****************************************************************************/
27 
28 
29 
30 /**
31 * @defgroup hdmicec
32 * @{
33 * @defgroup osal
34 * @{
35 **/
36 
37 
38 #ifndef HDMI_CCEC_OSAL_EVENTQUEUE_HPP_
39 #define HDMI_CCEC_OSAL_EVENTQUEUE_HPP_
40 
41 #include <stdlib.h>
42 #include <deque>
43 
44 #include "OSAL.hpp"
45 #include "Mutex.hpp"
46 #include "ConditionVariable.hpp"
47 
48 CCEC_OSAL_BEGIN_NAMESPACE
49 
50 /***************************************************************************/
51 /*!
52 
53 This is a collection class which provides the event queue functionality.
54 consumer threads could wait on the queue and will be signalled when
55 queue is populated.
56 
57 \param E - type of elements held in this collection.
58 */
59 /**************************************************************************/
60 
61 template <class E>
62 class EventQueue {
63 public:
64 
65 /***************************************************************************/
66 /*!
67 \brief Constructor.
68 Creates an EventQueue with the provided capacity.
69 
70 \param cap - Number of elements that could be held in the queue.
71 */
72 /**************************************************************************/
73 
74  EventQueue(size_t cap = 32) {
75  this->cap = cap;
76  events = new std::deque<E>(0);
77  }
78 /***************************************************************************/
79 /*!
80 
81 \brief Destructor
82 Destructor - Destroys the EventQueue object.
83 */
84 /**************************************************************************/
85 
87  {AutoLock lock_(mutex);
88  if (!events->empty()) {
89  printf("WARNING: There are [%d] elements left in queue\r\n", events->size());
90  }
91  delete events;
92  }
93 /***************************************************************************/
94 /*!
95 \brief method for polling events. This will block if queue is empty.
96 
97 Consumer will be returned with the object from front of the queue if queue is
98 not empty. If queue is empty, consumer threads will wait until an event is
99 posted to the queue.
100 
101 If waiting thread is awakened by there is no element on queue, a default
102 value is returned. This allows the waiting thread to close out the queue.
103 
104 \return event from the front of the queue.
105 */
106 /**************************************************************************/
107 
108  E poll(void) {
109  typedef E ET;
110  E front;
111  do {
112  cond.wait();
113  {AutoLock lock_(mutex);
114  if (!cond.isSet() || events->empty()) {
115  return ET();
116  }
117 
118  if (cond.isSet()) {
119  if (!events->empty()) {
120  front = events->front();
121  events->pop_front();
122  if (events->empty()) {
123  cond.reset();
124  }
125  }
126  else {
127  cond.reset();
128  }
129  } else {
130 
131  }
132  }
133 
134  } while (0 && front == NULL);
135 
136  return front;
137  }
138 
139 /***************************************************************************/
140 /*!
141 \brief returns size of queue.
142 
143 Retrieves number of events currently available in the queue
144 
145 \return number of events in queue.
146 */
147 /**************************************************************************/
148 
149  size_t size(void) {
150  AutoLock lock_(mutex);
151  return events->size();
152  }
153 
154 /***************************************************************************/
155 /*!
156 \brief send an event to the queue.
157 
158 Post a event to the queue and signals threads waiting on the queue.
159 On receiving the signal (event), if there is any consumer thread waiting
160 on the queue will come out of wait state and will consume the event.
161 
162 \exception - if queue is full, method will throw an exception.
163 \param E - Object that is to be posted to the queue.
164 */
165 /**************************************************************************/
166 
167  void offer(E element) {
168  AutoLock lock_(mutex);
169 
170  if (events->size() == cap) {
171  /* @TODO Throw Exception */
172  }
173  else {
174  events->push_back(element);
175  cond.set();
176  cond.notifyAll();
177  }
178  }
179 
180 private:
181  std::deque<E> *events;
182  size_t cap;
183  Mutex mutex;
184  ConditionVariable cond;
185 };
186 
187 CCEC_OSAL_END_NAMESPACE
188 
189 #endif
190 
191 
192 /** @} */
193 /** @} */
CCEC_OSAL::AutoLock
Definition: Mutex.hpp:121
CCEC_OSAL::EventQueue::poll
E poll(void)
method for polling events. This will block if queue is empty.
Definition: EventQueue.hpp:108
CCEC_OSAL::Mutex
Definition: Mutex.hpp:53
CCEC_OSAL::EventQueue::~EventQueue
~EventQueue(void)
Destructor Destructor - Destroys the EventQueue object.
Definition: EventQueue.hpp:86
CCEC_OSAL::EventQueue
Definition: EventQueue.hpp:62
ConditionVariable.hpp
This file defines interface of ConditionVariable class.
CCEC_OSAL::ConditionVariable
Definition: ConditionVariable.hpp:65
CCEC_OSAL::EventQueue::offer
void offer(E element)
send an event to the queue.
Definition: EventQueue.hpp:167
CCEC_OSAL::EventQueue::size
size_t size(void)
returns size of queue.
Definition: EventQueue.hpp:149
CCEC_OSAL::EventQueue::EventQueue
EventQueue(size_t cap=32)
Constructor. Creates an EventQueue with the provided capacity.
Definition: EventQueue.hpp:74
Mutex.hpp
This file defines interface of Mutex class.