summaryrefslogtreecommitdiff
path: root/src/thread_pool.cpp
blob: 3af2273e02e6b66c6b8fcaad1e8b4ee931f29e12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

#include "thread_pool.hpp"

#include <functional>
#include <future>

struct ThreadPool {
	bool isRunning;
	bool isPaused;
	uint8_t maxJobQueueCount;
	std::mutex mutex;
	std::atomic<uint64_t> completedCount;
	std::condition_variable condition;
	DynArray<std::packaged_task<void()> *> *jobQueue;
	DynArray<std::thread> *threads;
	MemBucket *bkt = nullptr;
};

const PkeHandleItemIndex_T MAX_THREADS_PER_BUCKET = 8;
struct ThreadBucket {
	ThreadPool threadPools[MAX_THREADS_PER_BUCKET];
};

BucketContainer<ThreadBucket, ThreadPoolHandle> ThreadPool_BucketContainer{};

void ThreadRun(ThreadPool *tp) {
	std::packaged_task<void()> *j = nullptr;
	while (tp->isRunning && !tp->isPaused) {
		{
			std::unique_lock<std::mutex> lck(tp->mutex);
			tp->condition.wait(lck, [tp] {
				return tp->jobQueue->Count() != 0 || !tp->isRunning || tp->isPaused;
			});
			if (!tp->isRunning || tp->isPaused) {
				return;
			}
			if (tp->jobQueue->Count() == 0) {
				continue;
			}
			j = (*tp->jobQueue)[0];
			tp->jobQueue->Remove(0, 1);
		}
		assert(j != nullptr);
		(*j)();
		Pke_Delete<std::packaged_task<void()>>(j, tp->bkt);
		tp->completedCount = tp->completedCount + 1;
	}
}

void inline PkeThreads_JoinAll_Inner(ThreadPool &tp) {
	tp.condition.notify_all();
	long count = tp.threads->Count();
	for (long l = 0; l < count; ++l) {
		auto &t = (*tp.threads)[l];
		if (t.joinable()) {
			t.join();
		}
	}
}

void inline PkeThreads_Reset_Inner(ThreadPool &tp) {
	tp.maxJobQueueCount = 0;
	tp.completedCount = 0;
	tp.jobQueue->Resize(0);
	tp.threads->Resize(0);
}

bool inline PkeThreads_Enqueue_Inner(ThreadPool &tp, std::packaged_task<void()> *job) {
	tp.mutex.lock();
	if (tp.isRunning == true) {
		if (tp.jobQueue->Count() < tp.maxJobQueueCount) {
			tp.jobQueue->Push(job);
			tp.condition.notify_one();
			tp.mutex.unlock();
			return true;
		}
	}
	tp.mutex.unlock();
	return false;
}

void inline PkeThreads_Pause_Inner(ThreadPool &tp) {
	tp.mutex.lock();
	if (tp.isPaused == true) {
		return; // called more than once
	}
	tp.isPaused = true;
	tp.mutex.unlock();
	PkeThreads_JoinAll_Inner(tp);
}

void inline PkeThreads_Resume_Inner(ThreadPool &tp) {
	tp.mutex.lock();
	tp.isPaused = false;
	long count = tp.threads->Count();
	for (size_t i = 0; i < count; i++) {
		(*tp.threads)[i] = std::thread(std::bind(ThreadRun, &tp));
	}
	tp.mutex.unlock();
}

void inline PkeThreads_Shutdown_Inner(ThreadPool &tp) {
	tp.mutex.lock();
	if (tp.isRunning == false) {
		return;
	}
	tp.isRunning = false;
	tp.isPaused = false;
	tp.jobQueue->Resize(0);
	tp.mutex.unlock();
	PkeThreads_JoinAll_Inner(tp);
}

ThreadPoolHandle PkeThreads_Init(uint8_t threadCount, uint8_t maxQueueCount, MemBucket *bkt) {
	if (!ThreadPool_BucketContainer.buckets) {
		Buckets_Init(ThreadPool_BucketContainer);
	}
	assert(threadCount > 0);
	bool moved;
	ThreadPoolHandle newHandle{Buckets_NewHandle(255, ThreadPool_BucketContainer, moved)};

	auto *tp = &ThreadPool_BucketContainer.buckets[newHandle.bucketIndex].threadPools[newHandle.itemIndex];

	tp->bkt = bkt;
	tp->isRunning = true;
	tp->isPaused = false;
	tp->maxJobQueueCount = maxQueueCount;
	tp->completedCount = 0;
	tp->jobQueue = Pke_New<DynArray<std::packaged_task<void()> *>>(bkt);
	tp->threads = Pke_New<DynArray<std::thread>>(bkt);

	tp->threads->Resize(threadCount);
	for (long l = 0; l < threadCount; ++l) {
		(*tp->threads)[l] = std::thread(std::bind(ThreadRun, tp));
	}

	return newHandle;
}

void PkeThreads_Reset(ThreadPoolHandle handle) {
	assert(handle != ThreadPoolHandle_MAX);
	auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex];
	PkeThreads_Reset_Inner(*tp);
}

bool PkeThreads_Enqueue(ThreadPoolHandle handle, std::packaged_task<void()> job) {
	assert(handle != ThreadPoolHandle_MAX);
	auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex];

	auto jobPtr = Pke_New<std::packaged_task<void()>>(tp->bkt);
	*jobPtr = std::move(job);

	return PkeThreads_Enqueue_Inner(*tp, jobPtr);
}

void PkeThreads_Pause(ThreadPoolHandle handle) {
	assert(handle != ThreadPoolHandle_MAX);
	auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex];

	PkeThreads_Pause_Inner(*tp);
}

void PkeThreads_Resume(ThreadPoolHandle handle) {
	assert(handle != ThreadPoolHandle_MAX);
	auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex];

	PkeThreads_Resume_Inner(*tp);
}

void PkeThreads_Shutdown(ThreadPoolHandle handle) {
	assert(handle != ThreadPoolHandle_MAX);
	auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex];

	PkeThreads_Shutdown_Inner(*tp);
}

void PkeThreads_Teardown(ThreadPoolHandle handle) {
	assert(handle != ThreadPoolHandle_MAX);
	auto *tp = &ThreadPool_BucketContainer.buckets[handle.bucketIndex].threadPools[handle.itemIndex];

	PkeThreads_Shutdown_Inner(*tp);
	PkeThreads_Reset_Inner(*tp);
	Pke_Delete<DynArray<std::packaged_task<void()> *>>(tp->jobQueue, tp->bkt);
	Pke_Delete<DynArray<std::thread>>(tp->threads, tp->bkt);
	tp->jobQueue = CAFE_BABE(DynArray<std::packaged_task<void()> *>);
	tp->threads = CAFE_BABE(DynArray<std::thread>);
	tp->bkt = CAFE_BABE(MemBucket);
}