JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
ThreadManager.h
Go to the documentation of this file.
1 //
2 // Created by Nico on 06/12/2018.
3 //
4 
5 #ifndef JODA_THREADMANAGER_H
6 #define JODA_THREADMANAGER_H
7 
8 #include <glob.h>
9 #include <glog/logging.h>
10 #include <memory>
11 #include <mutex>
12 #include <thread>
13 #include <unordered_map>
14 #include <vector>
15 #include "IThreadUser.h"
16 
17 #define JODA_THREAD_POLLING_TIME 500ms
18 
20  public:
21  ThreadManager(size_t maxThreads);
23  size_t getMaxThreads() const;
24  void setMaxThreads(size_t maxThreads);
25  size_t registerThreadUser(IThreadUser *user);
26  void unregisterThreadUser(size_t id);
27  void unregisterThreadUser(std::vector<size_t> &ids);
28  template <class queue_t>
29  void balanceQueue(std::vector<size_t> &writerIds,
30  std::vector<size_t> &readerIds, queue_t &queue,
31  bool &stop) {
32  std::pair<size_t, size_t> prevStats;
33  while (!stop) {
34  std::pair<size_t, size_t> stats = queue->getStatistics();
35  std::pair<size_t, size_t> currStats = stats;
36  currStats.first -= prevStats.first;
37  currStats.second -= prevStats.second;
38  prevStats = stats;
39  size_t i = 0;
40  for (const auto &id : writerIds) {
41  i += users[id]->getUsedThreads();
42  }
43  size_t o = 0;
44  for (const auto &id : readerIds) {
45  o += users[id]->getUsedThreads();
46  }
47  DLOG(INFO) << "QUEUE I/O: " << currStats.first << "/" << currStats.second
48  << " | "
49  << "Threads I/O: " << i << "/" << o;
50  using namespace std::chrono_literals;
51  std::this_thread::sleep_for(JODA_THREAD_POLLING_TIME);
52  }
53  }
54 
55  private:
56  std::mutex mut;
57  void recalculateThreads();
58  size_t maxThreads = 1;
59  size_t usedThreads() const;
60  size_t reservedThreads() const;
61  size_t availableThreads() const;
62  std::unordered_map<size_t, IThreadUser *> users;
63  size_t currID = 0;
64 };
65 
67 
68 #endif // JODA_THREADMANAGER_H
#define JODA_THREAD_POLLING_TIME
Definition: ThreadManager.h:17
ThreadManager g_ThreadManagerInstance
Definition: ThreadManager.cpp:8
Definition: IThreadUser.h:13
Definition: ThreadManager.h:19
size_t registerThreadUser(IThreadUser *user)
Definition: ThreadManager.cpp:21
void balanceQueue(std::vector< size_t > &writerIds, std::vector< size_t > &readerIds, queue_t &queue, bool &stop)
Definition: ThreadManager.h:29
void unregisterThreadUser(size_t id)
Definition: ThreadManager.cpp:30
void setMaxThreads(size_t maxThreads)
Definition: ThreadManager.cpp:12
size_t getMaxThreads() const
Definition: ThreadManager.cpp:10