libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
IQueue.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef IQUEUE_H_
32 #define IQUEUE_H_
33 
34 #include "IQueueBase.h"
35 
36 #include <vector>
37 
38 namespace BaseLib {
39 class SharedObjects;
40 
41 class IQueueEntry {
42  public:
43  IQueueEntry() = default;
44  virtual ~IQueueEntry() = default;
45 
46  int64_t time = 0;
47 };
48 
52 class IQueue : public IQueueBase {
53  public:
61  IQueue(SharedObjects *baseLib, uint32_t queueCount, uint32_t bufferSize);
62  virtual ~IQueue();
63 
73  void startQueue(int32_t index, bool waitWhenFull, uint32_t processingThreadCount, int32_t threadPriority = 0, int32_t threadPolicy = SCHED_OTHER);
74 
83  void startQueue(int32_t index, bool waitWhenFull, uint32_t initialProcessingThreadCount, uint32_t maxProcessingThreadCount);
84 
90  void stopQueue(int32_t index);
91 
95  bool queueIsStarted(int32_t index);
96 
103  bool addThread(int32_t index);
104 
113  bool enqueue(int32_t index, std::shared_ptr<IQueueEntry> &entry, bool waitWhenFull = false);
114 
121  virtual void processQueueEntry(int32_t index, std::shared_ptr<IQueueEntry> &entry) = 0;
122 
129  bool queueEmpty(int32_t index);
130 
136  uint32_t processingThreadCount(int32_t index);
137 
143  uint32_t maxProcessingThreadCount(int32_t index);
144 
151  int32_t queueSize(int32_t index);
152 
153  double threadLoad(int32_t index);
154  double maxThreadLoad(int32_t index);
155  double maxThreadLoad1m(int32_t index);
156  double maxThreadLoad10m(int32_t index);
157  double maxThreadLoad1h(int32_t index);
158  int64_t maxWait(int32_t index);
159  int64_t maxWait1m(int32_t index);
160  int64_t maxWait10m(int32_t index);
161  int64_t maxWait1h(int32_t index);
162  private:
163  std::mutex _addThreadMutex;
164  int32_t _bufferSize = 10000;
165  std::vector<int32_t> _bufferHead;
166  std::vector<int32_t> _bufferTail;
167  std::vector<int32_t> _bufferCount;
168  std::vector<bool> _waitWhenFull;
169  std::vector<std::vector<std::shared_ptr<IQueueEntry>>> _buffer;
170  std::unique_ptr<std::mutex[]> _queueMutex = nullptr;
171  std::vector<std::vector<std::shared_ptr<std::thread>>> _processingThread;
172  std::unique_ptr<std::condition_variable[]> _produceConditionVariable = nullptr;
173  std::unique_ptr<std::condition_variable[]> _processingConditionVariable = nullptr;
174 
175  std::unique_ptr<std::atomic<uint32_t>[]> _threadsInUse;
176 
177  std::unique_ptr<std::atomic<double>[]> _maxThreadLoad1mCurrent;
178  std::unique_ptr<std::atomic<double>[]> _maxThreadLoad1m;
179  std::unique_ptr<std::atomic<int64_t>[]> _maxWait1mCurrent;
180  std::unique_ptr<std::atomic<int64_t>[]> _maxWait1m;
181  std::unique_ptr<std::atomic<int64_t>[]> _last1mCycle;
182 
183  std::unique_ptr<std::atomic<double>[]> _maxThreadLoad10mCurrent;
184  std::unique_ptr<std::atomic<double>[]> _maxThreadLoad10m;
185  std::unique_ptr<std::atomic<int64_t>[]> _maxWait10mCurrent;
186  std::unique_ptr<std::atomic<int64_t>[]> _maxWait10m;
187  std::unique_ptr<std::atomic<int64_t>[]> _last10mCycle;
188 
189  std::unique_ptr<std::atomic<double>[]> _maxThreadLoad1hCurrent;
190  std::unique_ptr<std::atomic<double>[]> _maxThreadLoad1h;
191  std::unique_ptr<std::atomic<int64_t>[]> _maxWait1hCurrent;
192  std::unique_ptr<std::atomic<int64_t>[]> _maxWait1h;
193  std::unique_ptr<std::atomic<int64_t>[]> _last1hCycle;
194 
195  std::unique_ptr<std::atomic<double>[]> _maxThreadLoad;
196  std::unique_ptr<std::atomic<int64_t>[]> _maxWait;
197 
198  void process(int32_t index);
199 };
200 
201 }
202 #endif
This is the base library main class.
Definition: BaseLib.h:95
Definition: IQueue.h:41
Definition: IQueueBase.h:45
Definition: BaseLib.cpp:34
virtual ~IQueueEntry()=default
IQueueEntry()=default
This class implements a queue after the producer-consumer paradigm.
Definition: IQueue.h:52
int64_t time
Definition: IQueue.h:46