1 /*
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 */
11 //
12 // Multi-threaded worker
13 //
14 // Original source:
15 // https://chromium.googlesource.com/webm/libwebp
16
17 #ifndef AOM_AOM_UTIL_AOM_THREAD_H_
18 #define AOM_AOM_UTIL_AOM_THREAD_H_
19
20 #include "config/aom_config.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 // 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 #define MAX_NUM_THREADS 64
30
31 #if CONFIG_MULTITHREAD
32
33 #if defined(_WIN32) && !HAVE_PTHREAD_H
34 #include <errno.h> // NOLINT
35 #include <process.h> // NOLINT
36 #include <windows.h> // NOLINT
37 typedef HANDLE pthread_t;
38 typedef CRITICAL_SECTION pthread_mutex_t;
39
40 #if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater
41 #define USE_WINDOWS_CONDITION_VARIABLE
42 typedef CONDITION_VARIABLE pthread_cond_t;
43 #else
44 typedef struct {
45 HANDLE waiting_sem_;
46 HANDLE received_sem_;
47 HANDLE signal_event_;
48 } pthread_cond_t;
49 #endif // _WIN32_WINNT >= 0x600
50
51 #ifndef WINAPI_FAMILY_PARTITION
52 #define WINAPI_PARTITION_DESKTOP 1
53 #define WINAPI_FAMILY_PARTITION(x) x
54 #endif
55
56 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
57 #define USE_CREATE_THREAD
58 #endif
59
60 //------------------------------------------------------------------------------
61 // simplistic pthread emulation layer
62
63 // _beginthreadex requires __stdcall
64 #define THREADFN unsigned int __stdcall
65 #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
66
67 #if _WIN32_WINNT >= 0x0501 // Windows XP or greater
68 #define WaitForSingleObject(obj, timeout) \
69 WaitForSingleObjectEx(obj, timeout, FALSE /*bAlertable*/)
70 #endif
71
pthread_create(pthread_t * const thread,const void * attr,unsigned int (__stdcall * start)(void *),void * arg)72 static INLINE int pthread_create(pthread_t *const thread, const void *attr,
73 unsigned int(__stdcall *start)(void *),
74 void *arg) {
75 (void)attr;
76 #ifdef USE_CREATE_THREAD
77 *thread = CreateThread(NULL, /* lpThreadAttributes */
78 0, /* dwStackSize */
79 start, arg, 0, /* dwStackSize */
80 NULL); /* lpThreadId */
81 #else
82 *thread = (pthread_t)_beginthreadex(NULL, /* void *security */
83 0, /* unsigned stack_size */
84 start, arg, 0, /* unsigned initflag */
85 NULL); /* unsigned *thrdaddr */
86 #endif
87 if (*thread == NULL) return 1;
88 SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
89 return 0;
90 }
91
pthread_join(pthread_t thread,void ** value_ptr)92 static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
93 (void)value_ptr;
94 return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 ||
95 CloseHandle(thread) == 0);
96 }
97
98 // Mutex
pthread_mutex_init(pthread_mutex_t * const mutex,void * mutexattr)99 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
100 void *mutexattr) {
101 (void)mutexattr;
102 #if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater
103 InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
104 #else
105 InitializeCriticalSection(mutex);
106 #endif
107 return 0;
108 }
109
pthread_mutex_trylock(pthread_mutex_t * const mutex)110 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
111 return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
112 }
113
pthread_mutex_lock(pthread_mutex_t * const mutex)114 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
115 EnterCriticalSection(mutex);
116 return 0;
117 }
118
pthread_mutex_unlock(pthread_mutex_t * const mutex)119 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
120 LeaveCriticalSection(mutex);
121 return 0;
122 }
123
pthread_mutex_destroy(pthread_mutex_t * const mutex)124 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
125 DeleteCriticalSection(mutex);
126 return 0;
127 }
128
129 // Condition
pthread_cond_destroy(pthread_cond_t * const condition)130 static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
131 int ok = 1;
132 #ifdef USE_WINDOWS_CONDITION_VARIABLE
133 (void)condition;
134 #else
135 ok &= (CloseHandle(condition->waiting_sem_) != 0);
136 ok &= (CloseHandle(condition->received_sem_) != 0);
137 ok &= (CloseHandle(condition->signal_event_) != 0);
138 #endif
139 return !ok;
140 }
141
pthread_cond_init(pthread_cond_t * const condition,void * cond_attr)142 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
143 void *cond_attr) {
144 (void)cond_attr;
145 #ifdef USE_WINDOWS_CONDITION_VARIABLE
146 InitializeConditionVariable(condition);
147 #else
148 condition->waiting_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
149 condition->received_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
150 condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
151 if (condition->waiting_sem_ == NULL || condition->received_sem_ == NULL ||
152 condition->signal_event_ == NULL) {
153 pthread_cond_destroy(condition);
154 return 1;
155 }
156 #endif
157 return 0;
158 }
159
pthread_cond_signal(pthread_cond_t * const condition)160 static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
161 int ok = 1;
162 #ifdef USE_WINDOWS_CONDITION_VARIABLE
163 WakeConditionVariable(condition);
164 #else
165 if (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
166 // a thread is waiting in pthread_cond_wait: allow it to be notified
167 ok = SetEvent(condition->signal_event_);
168 // wait until the event is consumed so the signaler cannot consume
169 // the event via its own pthread_cond_wait.
170 ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
171 WAIT_OBJECT_0);
172 }
173 #endif
174 return !ok;
175 }
176
pthread_cond_broadcast(pthread_cond_t * const condition)177 static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
178 int ok = 1;
179 #ifdef USE_WINDOWS_CONDITION_VARIABLE
180 WakeAllConditionVariable(condition);
181 #else
182 while (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
183 // a thread is waiting in pthread_cond_wait: allow it to be notified
184 ok &= SetEvent(condition->signal_event_);
185 // wait until the event is consumed so the signaler cannot consume
186 // the event via its own pthread_cond_wait.
187 ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
188 WAIT_OBJECT_0);
189 }
190 #endif
191 return !ok;
192 }
193
pthread_cond_wait(pthread_cond_t * const condition,pthread_mutex_t * const mutex)194 static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
195 pthread_mutex_t *const mutex) {
196 int ok;
197 #ifdef USE_WINDOWS_CONDITION_VARIABLE
198 ok = SleepConditionVariableCS(condition, mutex, INFINITE);
199 #else
200 // note that there is a consumer available so the signal isn't dropped in
201 // pthread_cond_signal
202 if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL)) return 1;
203 // now unlock the mutex so pthread_cond_signal may be issued
204 pthread_mutex_unlock(mutex);
205 ok = (WaitForSingleObject(condition->signal_event_, INFINITE) ==
206 WAIT_OBJECT_0);
207 ok &= ReleaseSemaphore(condition->received_sem_, 1, NULL);
208 pthread_mutex_lock(mutex);
209 #endif
210 return !ok;
211 }
212 #elif defined(__OS2__)
213 #define INCL_DOS
214 #include <os2.h> // NOLINT
215
216 #include <errno.h> // NOLINT
217 #include <stdlib.h> // NOLINT
218 #include <sys/builtin.h> // NOLINT
219
220 #define pthread_t TID
221 #define pthread_mutex_t HMTX
222
223 typedef struct {
224 HEV event_sem_;
225 HEV ack_sem_;
226 volatile unsigned wait_count_;
227 } pthread_cond_t;
228
229 //------------------------------------------------------------------------------
230 // simplistic pthread emulation layer
231
232 #define THREADFN void *
233 #define THREAD_RETURN(val) (val)
234
235 typedef struct {
236 void *(*start_)(void *);
237 void *arg_;
238 } thread_arg;
239
240 static void thread_start(void *arg) {
241 thread_arg targ = *(thread_arg *)arg;
242 free(arg);
243
244 targ.start_(targ.arg_);
245 }
246
247 static INLINE int pthread_create(pthread_t *const thread, const void *attr,
248 void *(*start)(void *), void *arg) {
249 int tid;
250 thread_arg *targ = (thread_arg *)malloc(sizeof(*targ));
251 if (targ == NULL) return 1;
252
253 (void)attr;
254
255 targ->start_ = start;
256 targ->arg_ = arg;
257 tid = (pthread_t)_beginthread(thread_start, NULL, 1024 * 1024, targ);
258 if (tid == -1) {
259 free(targ);
260 return 1;
261 }
262
263 *thread = tid;
264 return 0;
265 }
266
267 static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
268 (void)value_ptr;
269 return DosWaitThread(&thread, DCWW_WAIT) != 0;
270 }
271
272 // Mutex
273 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
274 void *mutexattr) {
275 (void)mutexattr;
276 return DosCreateMutexSem(NULL, mutex, 0, FALSE) != 0;
277 }
278
279 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
280 return DosRequestMutexSem(*mutex, SEM_IMMEDIATE_RETURN) == 0 ? 0 : EBUSY;
281 }
282
283 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
284 return DosRequestMutexSem(*mutex, SEM_INDEFINITE_WAIT) != 0;
285 }
286
287 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
288 return DosReleaseMutexSem(*mutex) != 0;
289 }
290
291 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
292 return DosCloseMutexSem(*mutex) != 0;
293 }
294
295 // Condition
296 static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
297 int ok = 1;
298 ok &= DosCloseEventSem(condition->event_sem_) == 0;
299 ok &= DosCloseEventSem(condition->ack_sem_) == 0;
300 return !ok;
301 }
302
303 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
304 void *cond_attr) {
305 int ok = 1;
306 (void)cond_attr;
307
308 ok &=
309 DosCreateEventSem(NULL, &condition->event_sem_, DCE_POSTONE, FALSE) == 0;
310 ok &= DosCreateEventSem(NULL, &condition->ack_sem_, DCE_POSTONE, FALSE) == 0;
311 if (!ok) {
312 pthread_cond_destroy(condition);
313 return 1;
314 }
315 condition->wait_count_ = 0;
316 return 0;
317 }
318
319 static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
320 int ok = 1;
321
322 if (!__atomic_cmpxchg32(&condition->wait_count_, 0, 0)) {
323 ok &= DosPostEventSem(condition->event_sem_) == 0;
324 ok &= DosWaitEventSem(condition->ack_sem_, SEM_INDEFINITE_WAIT) == 0;
325 }
326
327 return !ok;
328 }
329
330 static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
331 int ok = 1;
332
333 while (!__atomic_cmpxchg32(&condition->wait_count_, 0, 0))
334 ok &= pthread_cond_signal(condition) == 0;
335
336 return !ok;
337 }
338
339 static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
340 pthread_mutex_t *const mutex) {
341 int ok = 1;
342
343 __atomic_increment(&condition->wait_count_);
344
345 ok &= pthread_mutex_unlock(mutex) == 0;
346
347 ok &= DosWaitEventSem(condition->event_sem_, SEM_INDEFINITE_WAIT) == 0;
348
349 __atomic_decrement(&condition->wait_count_);
350
351 ok &= DosPostEventSem(condition->ack_sem_) == 0;
352
353 pthread_mutex_lock(mutex);
354
355 return !ok;
356 }
357 #else // _WIN32
358 #include <pthread.h> // NOLINT
359 #define THREADFN void *
360 #define THREAD_RETURN(val) val
361 #endif
362
363 #endif // CONFIG_MULTITHREAD
364
365 // State of the worker thread object
366 typedef enum {
367 NOT_OK = 0, // object is unusable
368 OK, // ready to work
369 WORK // busy finishing the current task
370 } AVxWorkerStatus;
371
372 // Function to be called by the worker thread. Takes two opaque pointers as
373 // arguments (data1 and data2). Should return true on success and return false
374 // in case of error.
375 typedef int (*AVxWorkerHook)(void *, void *);
376
377 // Platform-dependent implementation details for the worker.
378 typedef struct AVxWorkerImpl AVxWorkerImpl;
379
380 // Synchronization object used to launch job in the worker thread
381 typedef struct {
382 AVxWorkerImpl *impl_;
383 AVxWorkerStatus status_;
384 // Thread name for the debugger. If not NULL, must point to a string that
385 // outlives the worker thread. For portability, use a name <= 15 characters
386 // long (not including the terminating NUL character).
387 const char *thread_name;
388 AVxWorkerHook hook; // hook to call
389 void *data1; // first argument passed to 'hook'
390 void *data2; // second argument passed to 'hook'
391 int had_error; // true if a call to 'hook' returned false
392 } AVxWorker;
393
394 // The interface for all thread-worker related functions. All these functions
395 // must be implemented.
396 typedef struct {
397 // Must be called first, before any other method.
398 void (*init)(AVxWorker *const worker);
399 // Must be called to initialize the object and spawn the thread. Re-entrant.
400 // Will potentially launch the thread. Returns false in case of error.
401 int (*reset)(AVxWorker *const worker);
402 // Makes sure the previous work is finished. Returns true if worker->had_error
403 // was not set and no error condition was triggered by the working thread.
404 int (*sync)(AVxWorker *const worker);
405 // Triggers the thread to call hook() with data1 and data2 arguments. These
406 // hook/data1/data2 values can be changed at any time before calling this
407 // function, but not be changed afterward until the next call to Sync().
408 void (*launch)(AVxWorker *const worker);
409 // This function is similar to launch() except that it calls the
410 // hook directly instead of using a thread. Convenient to bypass the thread
411 // mechanism while still using the AVxWorker structs. sync() must
412 // still be called afterward (for error reporting).
413 void (*execute)(AVxWorker *const worker);
414 // Kill the thread and terminate the object. To use the object again, one
415 // must call reset() again.
416 void (*end)(AVxWorker *const worker);
417 } AVxWorkerInterface;
418
419 // Install a new set of threading functions, overriding the defaults. This
420 // should be done before any workers are started, i.e., before any encoding or
421 // decoding takes place. The contents of the interface struct are copied, it
422 // is safe to free the corresponding memory after this call. This function is
423 // not thread-safe. Return false in case of invalid pointer or methods.
424 int aom_set_worker_interface(const AVxWorkerInterface *const winterface);
425
426 // Retrieve the currently set thread worker interface.
427 const AVxWorkerInterface *aom_get_worker_interface(void);
428
429 //------------------------------------------------------------------------------
430
431 #ifdef __cplusplus
432 } // extern "C"
433 #endif
434
435 #endif // AOM_AOM_UTIL_AOM_THREAD_H_
436