Resource Tuner
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2// SPDX-License-Identifier: BSD-3-Clause-Clear
3
4#ifndef THREAD_POOL_H
5#define THREAD_POOL_H
6
37#include <thread>
38#include <vector>
39#include <functional>
40#include <mutex>
41#include <condition_variable>
42#include <exception>
43
44#include "Utils.h"
45#include "Logger.h"
46#include "MemoryPool.h"
47#include "SafeOps.h"
48
49class TaskNode {
50public:
51 std::function<void(void*)>* taskCallback;
52 void* args;
53 TaskNode* next;
54
55 TaskNode();
56 ~TaskNode();
57};
58
59struct ThreadNode {
60 std::thread* th;
61 ThreadNode* next;
62};
63
64class TaskQueue {
65private:
66 int32_t size;
67 TaskNode* head;
68 TaskNode* tail;
69
70public:
71 TaskQueue();
72
73 void add(TaskNode* taskNode);
74 TaskNode* poll();
75 int8_t isEmpty();
76 int32_t getSize();
77
78 ~TaskQueue();
79};
80
81static const int32_t maxLoadPerThread = 3;
82
88private:
91
92 int32_t mCurrentThreadsCount;
93 int32_t mTotalTasksCount;
94 int8_t mTerminatePool;
95
96 TaskQueue* mCurrentTasks;
97 ThreadNode* mThreadQueueHead;
98 ThreadNode* mThreadQueueTail;
99
100 std::mutex mThreadPoolMutex;
101 std::condition_variable mThreadPoolCond;
102
103 TaskNode* createTaskNode(std::function<void(void*)> taskCallback, void* args);
104 int8_t addNewThread(int8_t isCoreThread);
105 int8_t threadRoutineHelper(int8_t isCoreThread);
106
107public:
108 ThreadPool(int32_t desiredCapacity, int32_t maxCapacity);
109 ~ThreadPool();
110
119 int8_t enqueueTask(std::function<void(void*)> callBack, void* arg);
120};
121
122#endif
123
ThreadPool.
Definition ThreadPool.h:87
int32_t mMaxPoolCapacity
Max Capacity upto which the Thread Pool can scale up.
Definition ThreadPool.h:90
int8_t enqueueTask(std::function< void(void *)> callBack, void *arg)
Enqueue a task for processing by one of ThreadPool's thread.
int32_t mDesiredPoolCapacity
Desired or Base Thread Pool Capacity.
Definition ThreadPool.h:89