-
Notifications
You must be signed in to change notification settings - Fork 2
/
threads.cc
314 lines (234 loc) · 7.77 KB
/
threads.cc
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* H.265 video codec.
* Copyright (c) 2013-2014 struktur AG, Dirk Farin <[email protected]>
*
* This file is part of libde265.
*
* libde265 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libde265 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libde265. If not, see <http://www.gnu.org/licenses/>.
*/
#include "threads.h"
#include <assert.h>
#include <string.h>
#if defined(_MSC_VER) || defined(__MINGW32__)
# include <malloc.h>
#elif defined(HAVE_ALLOCA_H)
# include <alloca.h>
#endif
#ifndef _WIN32
// #include <intrin.h>
#define THREAD_RESULT_TYPE void*
#define THREAD_PARAM_TYPE void*
#define THREAD_CALLING_CONVENTION
#include <stdio.h>
int de265_thread_create(de265_thread* t, void *(*start_routine) (void *), void *arg) { return pthread_create(t,NULL,start_routine,arg); }
void de265_thread_join(de265_thread t) { pthread_join(t,NULL); }
void de265_thread_destroy(de265_thread* t) { }
void de265_mutex_init(de265_mutex* m) { pthread_mutex_init(m,NULL); }
void de265_mutex_destroy(de265_mutex* m) { pthread_mutex_destroy(m); }
void de265_mutex_lock(de265_mutex* m) { pthread_mutex_lock(m); }
void de265_mutex_unlock(de265_mutex* m) { pthread_mutex_unlock(m); }
void de265_cond_init(de265_cond* c) { pthread_cond_init(c,NULL); }
void de265_cond_destroy(de265_cond* c) { pthread_cond_destroy(c); }
void de265_cond_broadcast(de265_cond* c,de265_mutex* m) { pthread_cond_broadcast(c); }
void de265_cond_wait(de265_cond* c,de265_mutex* m) { pthread_cond_wait(c,m); }
void de265_cond_signal(de265_cond* c) { pthread_cond_signal(c); }
#else // _WIN32
#define THREAD_RESULT_TYPE DWORD
#define THREAD_CALLING_CONVENTION WINAPI
#define THREAD_PARAM_TYPE LPVOID
int de265_thread_create(de265_thread* t, LPTHREAD_START_ROUTINE start_routine, void *arg) {
HANDLE handle = CreateThread(NULL, 0, start_routine, arg, 0, NULL);
if (handle == NULL) {
return -1;
}
*t = handle;
return 0;
}
void de265_thread_join(de265_thread t) { WaitForSingleObject(t, INFINITE); }
void de265_thread_destroy(de265_thread* t) { CloseHandle(*t); *t = NULL; }
void de265_mutex_init(de265_mutex* m) { *m = CreateMutex(NULL, FALSE, NULL); }
void de265_mutex_destroy(de265_mutex* m) { CloseHandle(*m); }
void de265_mutex_lock(de265_mutex* m) { WaitForSingleObject(*m, INFINITE); }
void de265_mutex_unlock(de265_mutex* m) { ReleaseMutex(*m); }
void de265_cond_init(de265_cond* c) { win32_cond_init(c); }
void de265_cond_destroy(de265_cond* c) { win32_cond_destroy(c); }
void de265_cond_broadcast(de265_cond* c,de265_mutex* m)
{
de265_mutex_lock(m);
win32_cond_broadcast(c);
de265_mutex_unlock(m);
}
void de265_cond_wait(de265_cond* c,de265_mutex* m) { win32_cond_wait(c,m); }
void de265_cond_signal(de265_cond* c) { win32_cond_signal(c); }
#endif // _WIN32
de265_progress_lock::de265_progress_lock()
{
mProgress = 0;
de265_mutex_init(&mutex);
de265_cond_init(&cond);
}
de265_progress_lock::~de265_progress_lock()
{
de265_mutex_destroy(&mutex);
de265_cond_destroy(&cond);
}
void de265_progress_lock::wait_for_progress(int progress)
{
if (mProgress >= progress) {
return;
}
de265_mutex_lock(&mutex);
while (mProgress < progress) {
de265_cond_wait(&cond, &mutex);
}
de265_mutex_unlock(&mutex);
}
void de265_progress_lock::set_progress(int progress)
{
de265_mutex_lock(&mutex);
if (progress>mProgress) {
mProgress = progress;
de265_cond_broadcast(&cond, &mutex);
}
de265_mutex_unlock(&mutex);
}
void de265_progress_lock::increase_progress(int progress)
{
de265_mutex_lock(&mutex);
mProgress += progress;
de265_cond_broadcast(&cond, &mutex);
de265_mutex_unlock(&mutex);
}
int de265_progress_lock::get_progress() const
{
return mProgress;
}
#include "libde265/decctx.h"
#if 0
const char* line="--------------------------------------------------";
void printblks(const thread_pool* pool)
{
int w = pool->tasks[0].data.task_ctb.ctx->current_sps->PicWidthInCtbsY;
int h = pool->tasks[0].data.task_ctb.ctx->current_sps->PicHeightInCtbsY;
printf("active threads: %d queue len: %d\n",pool->num_threads_working,pool->num_tasks);
char *const p = (char *)alloca(w * h * sizeof(char));
assert(p != NULL);
memset(p,' ',w*h);
for (int i=0;i<pool->num_tasks;i++) {
int b = 0; //pool->tasks[i].num_blockers;
int x = pool->tasks[i].data.task_ctb.ctb_x;
int y = pool->tasks[i].data.task_ctb.ctb_y;
p[y*w+x] = b+'0';
}
for (int i=0;i<pool->num_threads_working;i++) {
int x = pool->ctbx[i];
int y = pool->ctby[i];
p[y*w+x] = '*';
}
printf("+%s+\n",line+50-w);
for (int y=0;y<h;y++)
{
printf("|");
for (int x=0;x<w;x++)
{
printf("%c",p[x+y*w]);
}
printf("|\n");
}
printf("+%s+\n",line+50-w);
}
#endif
static THREAD_RESULT_TYPE THREAD_CALLING_CONVENTION worker_thread(THREAD_PARAM_TYPE pool_ptr)
{
thread_pool* pool = (thread_pool*)pool_ptr;
de265_mutex_lock(&pool->mutex);
while(true) {
// wait until we can pick a task or until the pool has been stopped
for (;;) {
// end waiting if thread-pool has been stopped or we have a task to execute
if (pool->stopped || pool->tasks.size()>0) {
break;
}
//printf("going idle\n");
de265_cond_wait(&pool->cond_var, &pool->mutex);
}
// if the pool was shut down, end the execution
if (pool->stopped) {
de265_mutex_unlock(&pool->mutex);
return (THREAD_RESULT_TYPE)0;
}
// get a task
thread_task* task = pool->tasks.front();
pool->tasks.pop_front();
pool->num_threads_working++;
//printblks(pool);
de265_mutex_unlock(&pool->mutex);
// execute the task
task->work();
// end processing and check if this was the last task to be processed
de265_mutex_lock(&pool->mutex);
pool->num_threads_working--;
}
de265_mutex_unlock(&pool->mutex);
return (THREAD_RESULT_TYPE)0;
}
de265_error start_thread_pool(thread_pool* pool, int num_threads)
{
de265_error err = DE265_OK;
// limit number of threads to maximum
if (num_threads > MAX_THREADS) {
num_threads = MAX_THREADS;
err = DE265_WARNING_NUMBER_OF_THREADS_LIMITED_TO_MAXIMUM;
}
pool->num_threads = 0; // will be increased below
de265_mutex_init(&pool->mutex);
de265_cond_init(&pool->cond_var);
de265_mutex_lock(&pool->mutex);
pool->num_threads_working = 0;
pool->stopped = false;
de265_mutex_unlock(&pool->mutex);
// start worker threads
for (int i=0; i<num_threads; i++) {
int ret = de265_thread_create(&pool->thread[i], worker_thread, pool);
if (ret != 0) {
// cerr << "pthread_create() failed: " << ret << endl;
return DE265_ERROR_CANNOT_START_THREADPOOL;
}
pool->num_threads++;
}
return err;
}
void stop_thread_pool(thread_pool* pool)
{
de265_mutex_lock(&pool->mutex);
pool->stopped = true;
de265_mutex_unlock(&pool->mutex);
de265_cond_broadcast(&pool->cond_var, &pool->mutex);
for (int i=0;i<pool->num_threads;i++) {
de265_thread_join(pool->thread[i]);
de265_thread_destroy(&pool->thread[i]);
}
de265_mutex_destroy(&pool->mutex);
de265_cond_destroy(&pool->cond_var);
}
void add_task(thread_pool* pool, thread_task* task)
{
de265_mutex_lock(&pool->mutex);
if (!pool->stopped) {
pool->tasks.push_back(task);
// wake up one thread
de265_cond_signal(&pool->cond_var);
}
de265_mutex_unlock(&pool->mutex);
}