libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
ThreadManager.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 THREADS_H_
32 #define THREADS_H_
33 
34 #include "../Exception.h"
35 #include "../Output/Output.h"
36 #include <mutex>
37 #include <thread>
38 
39 namespace BaseLib
40 {
41 
42 class SharedObjects;
43 
45 {
46 public:
47  ThreadManager();
48  virtual ~ThreadManager();
49  void init(BaseLib::SharedObjects* baseLib, bool testMaxThreadCount);
50 
51  static int32_t getThreadPolicyFromString(std::string policy);
52  static int32_t parseThreadPriority(int32_t priority, int32_t policy);
53  void setThreadPriority(pthread_t thread, int32_t priority, int32_t policy = SCHED_FIFO);
54 
55  template<typename Function, typename... Args>
56  bool start(std::thread& thread, bool highPriority, Function&& function, Args&&... args)
57  {
58  if(!checkThreadCount(highPriority)) return false;
59  join(thread);
60  thread = std::thread(function, args...);
62  return true;
63  }
64 
65  template<typename Function, typename... Args>
66  bool start(std::thread& thread, bool highPriority, int32_t priority, int32_t policy, Function&& function, Args&&... args)
67  {
68  if(!checkThreadCount(highPriority)) return false;
69  join(thread);
70  thread = std::thread(function, args...);
71  setThreadPriority(thread.native_handle(), priority, policy);
73  return true;
74  }
75 
76  void join(std::thread& thread);
77 
78  void registerThread();
79  void unregisterThread();
80  void setMaxThreadCount(uint32_t value);
81  uint32_t getMaxThreadCount();
82  int32_t getCurrentThreadCount();
83  uint32_t getMaxRegisteredThreadCount();
84  void testMaxThreadCount();
85 protected:
86  SharedObjects* _bl = nullptr;
87  std::mutex _threadCountMutex;
89  uint32_t _maxThreadCount = 0;
90  volatile int32_t _currentThreadCount = 0;
91 
92  bool checkThreadCount(bool highPriority);
93 private:
94  ThreadManager(const ThreadManager&) = delete;
95  ThreadManager& operator=(const ThreadManager&) = delete;
96 };
97 
98 }
99 #endif
bool checkThreadCount(bool highPriority)
Definition: ThreadManager.cpp:107
uint32_t getMaxRegisteredThreadCount()
Definition: ThreadManager.cpp:85
Definition: ThreadManager.h:44
This is the base library main class.
Definition: BaseLib.h:95
void registerThread()
Definition: ThreadManager.cpp:193
uint32_t getMaxThreadCount()
Definition: ThreadManager.cpp:95
SharedObjects * _bl
Definition: ThreadManager.h:86
Definition: BaseLib.cpp:34
bool start(std::thread &thread, bool highPriority, int32_t priority, int32_t policy, Function &&function, Args &&... args)
Definition: ThreadManager.h:66
ThreadManager()
Definition: ThreadManager.cpp:48
uint32_t _maxThreadCount
Definition: ThreadManager.h:89
static int32_t getThreadPolicyFromString(std::string policy)
Definition: ThreadManager.cpp:117
void testMaxThreadCount()
Definition: ThreadManager.cpp:62
void unregisterThread()
Definition: ThreadManager.cpp:200
void init(BaseLib::SharedObjects *baseLib, bool testMaxThreadCount)
Definition: ThreadManager.cpp:56
void join(std::thread &thread)
Definition: ThreadManager.cpp:184
PVariable value
Definition: UiElements.h:217
volatile int32_t _currentThreadCount
Definition: ThreadManager.h:90
void setMaxThreadCount(uint32_t value)
Definition: ThreadManager.cpp:90
int32_t getCurrentThreadCount()
Definition: ThreadManager.cpp:100
bool start(std::thread &thread, bool highPriority, Function &&function, Args &&... args)
Definition: ThreadManager.h:56
std::mutex _threadCountMutex
Definition: ThreadManager.h:87
static int32_t parseThreadPriority(int32_t priority, int32_t policy)
Definition: ThreadManager.cpp:132
virtual ~ThreadManager()
Definition: ThreadManager.cpp:52
uint32_t _maxRegisteredThreadCount
Definition: ThreadManager.h:88
void setThreadPriority(pthread_t thread, int32_t priority, int32_t policy=SCHED_FIFO)
Definition: ThreadManager.cpp:143