blob: e22c4cca2789b3bc56b5fc544933ce440877a9bc [file] [log] [blame]
Yaowu Xu9c01aa12016-09-01 14:32:49 -07001/*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
James Zern183b77d2013-07-30 22:46:58 -070011//
12// Multi-threaded worker
13//
14// Original source:
Johann2967bf32016-06-22 16:08:10 -070015// https://chromium.googlesource.com/webm/libwebp
James Zern183b77d2013-07-30 22:46:58 -070016
Yaowu Xuf883b422016-08-30 14:01:10 -070017#ifndef AOM_THREAD_H_
18#define AOM_THREAD_H_
James Zern183b77d2013-07-30 22:46:58 -070019
Yaowu Xuf883b422016-08-30 14:01:10 -070020#include "./aom_config.h"
James Zern183b77d2013-07-30 22:46:58 -070021
James Zern40aa9102014-01-18 12:16:11 -080022#ifdef __cplusplus
James Zern183b77d2013-07-30 22:46:58 -070023extern "C" {
24#endif
25
hkuangdd88f482015-02-05 13:59:15 -080026// Set maximum decode threads to be 8 due to the limit of frame buffers
27// and not enough semaphores in the emulation layer on windows.
28#define MAX_DECODE_THREADS 8
29
James Zern183b77d2013-07-30 22:46:58 -070030#if CONFIG_MULTITHREAD
31
James Zernd167a1a2015-02-10 12:47:14 -080032#if defined(_WIN32) && !HAVE_PTHREAD_H
clang-format3a992f82016-08-10 17:02:06 -070033#include <errno.h> // NOLINT
Yunqing Wang903801f2013-12-27 15:25:54 -080034#include <process.h> // NOLINT
Jim Bankoskic52d8542013-10-01 15:19:39 -070035#include <windows.h> // NOLINT
James Zern183b77d2013-07-30 22:46:58 -070036typedef HANDLE pthread_t;
37typedef CRITICAL_SECTION pthread_mutex_t;
Johann2967bf32016-06-22 16:08:10 -070038
39#if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater
40#define USE_WINDOWS_CONDITION_VARIABLE
41typedef CONDITION_VARIABLE pthread_cond_t;
42#else
James Zern183b77d2013-07-30 22:46:58 -070043typedef struct {
44 HANDLE waiting_sem_;
45 HANDLE received_sem_;
46 HANDLE signal_event_;
47} pthread_cond_t;
Johann2967bf32016-06-22 16:08:10 -070048#endif // _WIN32_WINNT >= 0x600
49
50#ifndef WINAPI_FAMILY_PARTITION
51#define WINAPI_PARTITION_DESKTOP 1
52#define WINAPI_FAMILY_PARTITION(x) x
53#endif
54
55#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
56#define USE_CREATE_THREAD
57#endif
James Zern183b77d2013-07-30 22:46:58 -070058
Yunqing Wang903801f2013-12-27 15:25:54 -080059//------------------------------------------------------------------------------
60// simplistic pthread emulation layer
James Zern183b77d2013-07-30 22:46:58 -070061
Yunqing Wang903801f2013-12-27 15:25:54 -080062// _beginthreadex requires __stdcall
63#define THREADFN unsigned int __stdcall
64#define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
65
Johann2967bf32016-06-22 16:08:10 -070066#if _WIN32_WINNT >= 0x0501 // Windows XP or greater
67#define WaitForSingleObject(obj, timeout) \
68 WaitForSingleObjectEx(obj, timeout, FALSE /*bAlertable*/)
69#endif
70
clang-format3a992f82016-08-10 17:02:06 -070071static INLINE int pthread_create(pthread_t *const thread, const void *attr,
72 unsigned int(__stdcall *start)(void *),
73 void *arg) {
Yunqing Wang903801f2013-12-27 15:25:54 -080074 (void)attr;
Johann2967bf32016-06-22 16:08:10 -070075#ifdef USE_CREATE_THREAD
clang-format3a992f82016-08-10 17:02:06 -070076 *thread = CreateThread(NULL, /* lpThreadAttributes */
77 0, /* dwStackSize */
78 start, arg, 0, /* dwStackSize */
79 NULL); /* lpThreadId */
Johann2967bf32016-06-22 16:08:10 -070080#else
clang-format3a992f82016-08-10 17:02:06 -070081 *thread = (pthread_t)_beginthreadex(NULL, /* void *security */
82 0, /* unsigned stack_size */
83 start, arg, 0, /* unsigned initflag */
84 NULL); /* unsigned *thrdaddr */
Johann2967bf32016-06-22 16:08:10 -070085#endif
Yunqing Wang903801f2013-12-27 15:25:54 -080086 if (*thread == NULL) return 1;
87 SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
88 return 0;
89}
90
clang-format3a992f82016-08-10 17:02:06 -070091static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
Yunqing Wang903801f2013-12-27 15:25:54 -080092 (void)value_ptr;
93 return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 ||
94 CloseHandle(thread) == 0);
95}
96
97// Mutex
98static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
clang-format3a992f82016-08-10 17:02:06 -070099 void *mutexattr) {
Yunqing Wang903801f2013-12-27 15:25:54 -0800100 (void)mutexattr;
Johann2967bf32016-06-22 16:08:10 -0700101#if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater
102 InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
103#else
Yunqing Wang903801f2013-12-27 15:25:54 -0800104 InitializeCriticalSection(mutex);
Johann2967bf32016-06-22 16:08:10 -0700105#endif
Yunqing Wang903801f2013-12-27 15:25:54 -0800106 return 0;
107}
108
109static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
110 return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
111}
112
113static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
114 EnterCriticalSection(mutex);
115 return 0;
116}
117
118static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
119 LeaveCriticalSection(mutex);
120 return 0;
121}
122
123static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
124 DeleteCriticalSection(mutex);
125 return 0;
126}
127
128// Condition
129static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
130 int ok = 1;
Johann2967bf32016-06-22 16:08:10 -0700131#ifdef USE_WINDOWS_CONDITION_VARIABLE
132 (void)condition;
133#else
Yunqing Wang903801f2013-12-27 15:25:54 -0800134 ok &= (CloseHandle(condition->waiting_sem_) != 0);
135 ok &= (CloseHandle(condition->received_sem_) != 0);
136 ok &= (CloseHandle(condition->signal_event_) != 0);
Johann2967bf32016-06-22 16:08:10 -0700137#endif
Yunqing Wang903801f2013-12-27 15:25:54 -0800138 return !ok;
139}
140
141static INLINE int pthread_cond_init(pthread_cond_t *const condition,
clang-format3a992f82016-08-10 17:02:06 -0700142 void *cond_attr) {
Yunqing Wang903801f2013-12-27 15:25:54 -0800143 (void)cond_attr;
Johann2967bf32016-06-22 16:08:10 -0700144#ifdef USE_WINDOWS_CONDITION_VARIABLE
145 InitializeConditionVariable(condition);
146#else
hkuangdd88f482015-02-05 13:59:15 -0800147 condition->waiting_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
148 condition->received_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
Yunqing Wang903801f2013-12-27 15:25:54 -0800149 condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
clang-format3a992f82016-08-10 17:02:06 -0700150 if (condition->waiting_sem_ == NULL || condition->received_sem_ == NULL ||
Yunqing Wang903801f2013-12-27 15:25:54 -0800151 condition->signal_event_ == NULL) {
152 pthread_cond_destroy(condition);
153 return 1;
154 }
Johann2967bf32016-06-22 16:08:10 -0700155#endif
Yunqing Wang903801f2013-12-27 15:25:54 -0800156 return 0;
157}
158
159static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
160 int ok = 1;
Johann2967bf32016-06-22 16:08:10 -0700161#ifdef USE_WINDOWS_CONDITION_VARIABLE
162 WakeConditionVariable(condition);
163#else
Yunqing Wang903801f2013-12-27 15:25:54 -0800164 if (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
165 // a thread is waiting in pthread_cond_wait: allow it to be notified
166 ok = SetEvent(condition->signal_event_);
167 // wait until the event is consumed so the signaler cannot consume
168 // the event via its own pthread_cond_wait.
169 ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
170 WAIT_OBJECT_0);
171 }
Johann2967bf32016-06-22 16:08:10 -0700172#endif
Yunqing Wang903801f2013-12-27 15:25:54 -0800173 return !ok;
174}
175
Deepa K G964e72e2018-05-16 16:56:01 +0530176static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
177 int ok = 1;
178#ifdef USE_WINDOWS_CONDITION_VARIABLE
179 WakeAllConditionVariable(condition);
180#else
181 while (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
182 // a thread is waiting in pthread_cond_wait: allow it to be notified
183 ok &= SetEvent(condition->signal_event_);
184 // wait until the event is consumed so the signaler cannot consume
185 // the event via its own pthread_cond_wait.
186 ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
187 WAIT_OBJECT_0);
188 }
189#endif
190 return !ok;
191}
192
Yunqing Wang903801f2013-12-27 15:25:54 -0800193static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
194 pthread_mutex_t *const mutex) {
195 int ok;
Johann2967bf32016-06-22 16:08:10 -0700196#ifdef USE_WINDOWS_CONDITION_VARIABLE
197 ok = SleepConditionVariableCS(condition, mutex, INFINITE);
198#else
Yunqing Wang903801f2013-12-27 15:25:54 -0800199 // note that there is a consumer available so the signal isn't dropped in
200 // pthread_cond_signal
clang-format3a992f82016-08-10 17:02:06 -0700201 if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL)) return 1;
Yunqing Wang903801f2013-12-27 15:25:54 -0800202 // now unlock the mutex so pthread_cond_signal may be issued
203 pthread_mutex_unlock(mutex);
204 ok = (WaitForSingleObject(condition->signal_event_, INFINITE) ==
205 WAIT_OBJECT_0);
206 ok &= ReleaseSemaphore(condition->received_sem_, 1, NULL);
207 pthread_mutex_lock(mutex);
Johann2967bf32016-06-22 16:08:10 -0700208#endif
Yunqing Wang903801f2013-12-27 15:25:54 -0800209 return !ok;
210}
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900211#elif defined(__OS2__)
212#define INCL_DOS
clang-format3a992f82016-08-10 17:02:06 -0700213#include <os2.h> // NOLINT
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900214
clang-format3a992f82016-08-10 17:02:06 -0700215#include <errno.h> // NOLINT
216#include <stdlib.h> // NOLINT
217#include <sys/builtin.h> // NOLINT
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900218
219#define pthread_t TID
220#define pthread_mutex_t HMTX
221
222typedef struct {
223 HEV event_sem_;
224 HEV ack_sem_;
225 volatile unsigned wait_count_;
226} pthread_cond_t;
227
228//------------------------------------------------------------------------------
229// simplistic pthread emulation layer
230
231#define THREADFN void *
232#define THREAD_RETURN(val) (val)
233
234typedef struct {
clang-format3a992f82016-08-10 17:02:06 -0700235 void *(*start_)(void *);
236 void *arg_;
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900237} thread_arg;
238
clang-format3a992f82016-08-10 17:02:06 -0700239static void thread_start(void *arg) {
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900240 thread_arg targ = *(thread_arg *)arg;
241 free(arg);
242
243 targ.start_(targ.arg_);
244}
245
clang-format3a992f82016-08-10 17:02:06 -0700246static INLINE int pthread_create(pthread_t *const thread, const void *attr,
247 void *(*start)(void *), void *arg) {
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900248 int tid;
249 thread_arg *targ = (thread_arg *)malloc(sizeof(*targ));
250 if (targ == NULL) return 1;
251
252 (void)attr;
253
254 targ->start_ = start;
255 targ->arg_ = arg;
256 tid = (pthread_t)_beginthread(thread_start, NULL, 1024 * 1024, targ);
257 if (tid == -1) {
258 free(targ);
259 return 1;
260 }
261
262 *thread = tid;
263 return 0;
264}
265
clang-format3a992f82016-08-10 17:02:06 -0700266static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900267 (void)value_ptr;
268 return DosWaitThread(&thread, DCWW_WAIT) != 0;
269}
270
271// Mutex
272static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
clang-format3a992f82016-08-10 17:02:06 -0700273 void *mutexattr) {
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900274 (void)mutexattr;
275 return DosCreateMutexSem(NULL, mutex, 0, FALSE) != 0;
276}
277
278static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
279 return DosRequestMutexSem(*mutex, SEM_IMMEDIATE_RETURN) == 0 ? 0 : EBUSY;
280}
281
282static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
283 return DosRequestMutexSem(*mutex, SEM_INDEFINITE_WAIT) != 0;
284}
285
286static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
287 return DosReleaseMutexSem(*mutex) != 0;
288}
289
290static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
291 return DosCloseMutexSem(*mutex) != 0;
292}
293
294// Condition
295static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
296 int ok = 1;
297 ok &= DosCloseEventSem(condition->event_sem_) == 0;
298 ok &= DosCloseEventSem(condition->ack_sem_) == 0;
299 return !ok;
300}
301
302static INLINE int pthread_cond_init(pthread_cond_t *const condition,
clang-format3a992f82016-08-10 17:02:06 -0700303 void *cond_attr) {
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900304 int ok = 1;
305 (void)cond_attr;
306
clang-format3a992f82016-08-10 17:02:06 -0700307 ok &=
308 DosCreateEventSem(NULL, &condition->event_sem_, DCE_POSTONE, FALSE) == 0;
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900309 ok &= DosCreateEventSem(NULL, &condition->ack_sem_, DCE_POSTONE, FALSE) == 0;
310 if (!ok) {
311 pthread_cond_destroy(condition);
312 return 1;
313 }
314 condition->wait_count_ = 0;
315 return 0;
316}
317
318static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
319 int ok = 1;
320
321 if (!__atomic_cmpxchg32(&condition->wait_count_, 0, 0)) {
322 ok &= DosPostEventSem(condition->event_sem_) == 0;
323 ok &= DosWaitEventSem(condition->ack_sem_, SEM_INDEFINITE_WAIT) == 0;
324 }
325
326 return !ok;
327}
328
329static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
330 int ok = 1;
331
332 while (!__atomic_cmpxchg32(&condition->wait_count_, 0, 0))
clang-format3a992f82016-08-10 17:02:06 -0700333 ok &= pthread_cond_signal(condition) == 0;
KO Myung-Hun14e8ade2016-05-22 13:57:07 +0900334
335 return !ok;
336}
337
338static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
339 pthread_mutex_t *const mutex) {
340 int ok = 1;
341
342 __atomic_increment(&condition->wait_count_);
343
344 ok &= pthread_mutex_unlock(mutex) == 0;
345
346 ok &= DosWaitEventSem(condition->event_sem_, SEM_INDEFINITE_WAIT) == 0;
347
348 __atomic_decrement(&condition->wait_count_);
349
350 ok &= DosPostEventSem(condition->ack_sem_) == 0;
351
352 pthread_mutex_lock(mutex);
353
354 return !ok;
355}
clang-format3a992f82016-08-10 17:02:06 -0700356#else // _WIN32
357#include <pthread.h> // NOLINT
358#define THREADFN void *
359#define THREAD_RETURN(val) val
Yunqing Wang903801f2013-12-27 15:25:54 -0800360#endif
James Zern183b77d2013-07-30 22:46:58 -0700361
Yunqing Wang903801f2013-12-27 15:25:54 -0800362#endif // CONFIG_MULTITHREAD
James Zern183b77d2013-07-30 22:46:58 -0700363
364// State of the worker thread object
365typedef enum {
clang-format3a992f82016-08-10 17:02:06 -0700366 NOT_OK = 0, // object is unusable
367 OK, // ready to work
368 WORK // busy finishing the current task
Yaowu Xuf883b422016-08-30 14:01:10 -0700369} AVxWorkerStatus;
James Zern183b77d2013-07-30 22:46:58 -0700370
371// Function to be called by the worker thread. Takes two opaque pointers as
372// arguments (data1 and data2), and should return false in case of error.
Yaowu Xuf883b422016-08-30 14:01:10 -0700373typedef int (*AVxWorkerHook)(void *, void *);
James Zern183b77d2013-07-30 22:46:58 -0700374
James Zerne656f442014-06-19 21:14:51 -0700375// Platform-dependent implementation details for the worker.
Yaowu Xuf883b422016-08-30 14:01:10 -0700376typedef struct AVxWorkerImpl AVxWorkerImpl;
James Zerne656f442014-06-19 21:14:51 -0700377
378// Synchronization object used to launch job in the worker thread
James Zern183b77d2013-07-30 22:46:58 -0700379typedef struct {
Yaowu Xuf883b422016-08-30 14:01:10 -0700380 AVxWorkerImpl *impl_;
381 AVxWorkerStatus status_;
382 AVxWorkerHook hook; // hook to call
clang-format3a992f82016-08-10 17:02:06 -0700383 void *data1; // first argument passed to 'hook'
384 void *data2; // second argument passed to 'hook'
385 int had_error; // return value of the last call to 'hook'
Yaowu Xuf883b422016-08-30 14:01:10 -0700386} AVxWorker;
James Zern183b77d2013-07-30 22:46:58 -0700387
James Zerne656f442014-06-19 21:14:51 -0700388// The interface for all thread-worker related functions. All these functions
389// must be implemented.
390typedef struct {
391 // Must be called first, before any other method.
Yaowu Xuf883b422016-08-30 14:01:10 -0700392 void (*init)(AVxWorker *const worker);
James Zerne656f442014-06-19 21:14:51 -0700393 // Must be called to initialize the object and spawn the thread. Re-entrant.
394 // Will potentially launch the thread. Returns false in case of error.
Yaowu Xuf883b422016-08-30 14:01:10 -0700395 int (*reset)(AVxWorker *const worker);
James Zerne656f442014-06-19 21:14:51 -0700396 // Makes sure the previous work is finished. Returns true if worker->had_error
397 // was not set and no error condition was triggered by the working thread.
Yaowu Xuf883b422016-08-30 14:01:10 -0700398 int (*sync)(AVxWorker *const worker);
James Zerne656f442014-06-19 21:14:51 -0700399 // Triggers the thread to call hook() with data1 and data2 arguments. These
400 // hook/data1/data2 values can be changed at any time before calling this
401 // function, but not be changed afterward until the next call to Sync().
Yaowu Xuf883b422016-08-30 14:01:10 -0700402 void (*launch)(AVxWorker *const worker);
James Zerne656f442014-06-19 21:14:51 -0700403 // This function is similar to launch() except that it calls the
404 // hook directly instead of using a thread. Convenient to bypass the thread
Yaowu Xuf883b422016-08-30 14:01:10 -0700405 // mechanism while still using the AVxWorker structs. sync() must
James Zerne656f442014-06-19 21:14:51 -0700406 // still be called afterward (for error reporting).
Yaowu Xuf883b422016-08-30 14:01:10 -0700407 void (*execute)(AVxWorker *const worker);
James Zerne656f442014-06-19 21:14:51 -0700408 // Kill the thread and terminate the object. To use the object again, one
409 // must call reset() again.
Yaowu Xuf883b422016-08-30 14:01:10 -0700410 void (*end)(AVxWorker *const worker);
411} AVxWorkerInterface;
James Zerne656f442014-06-19 21:14:51 -0700412
413// Install a new set of threading functions, overriding the defaults. This
414// should be done before any workers are started, i.e., before any encoding or
415// decoding takes place. The contents of the interface struct are copied, it
416// is safe to free the corresponding memory after this call. This function is
417// not thread-safe. Return false in case of invalid pointer or methods.
Yaowu Xuf883b422016-08-30 14:01:10 -0700418int aom_set_worker_interface(const AVxWorkerInterface *const winterface);
James Zerne656f442014-06-19 21:14:51 -0700419
420// Retrieve the currently set thread worker interface.
Yaowu Xuf883b422016-08-30 14:01:10 -0700421const AVxWorkerInterface *aom_get_worker_interface(void);
James Zern183b77d2013-07-30 22:46:58 -0700422
423//------------------------------------------------------------------------------
424
James Zern40aa9102014-01-18 12:16:11 -0800425#ifdef __cplusplus
clang-format3a992f82016-08-10 17:02:06 -0700426} // extern "C"
James Zern183b77d2013-07-30 22:46:58 -0700427#endif
428
Yaowu Xuf883b422016-08-30 14:01:10 -0700429#endif // AOM_THREAD_H_