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
30 #if CONFIG_MULTITHREAD
31
32 #if defined(_WIN32) && !HAVE_PTHREAD_H
33 #include <errno.h> // NOLINT
34 #include <process.h> // NOLINT
35 #include <windows.h> // NOLINT
36 typedef HANDLE pthread_t;
37 typedef CRITICAL_SECTION pthread_mutex_t;
38
39 #if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater
40 #define USE_WINDOWS_CONDITION_VARIABLE
41 typedef CONDITION_VARIABLE pthread_cond_t;
42 #else
43 typedef struct {
44 HANDLE waiting_sem_;
45 HANDLE received_sem_;
46 HANDLE signal_event_;
47 } pthread_cond_t;
48 #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
58
59 //------------------------------------------------------------------------------
60 // simplistic pthread emulation layer
61
62 // _beginthreadex requires __stdcall
63 #define THREADFN unsigned int __stdcall
64 #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
65
66 #if _WIN32_WINNT >= 0x0501 // Windows XP or greater
67 #define WaitForSingleObject(obj, timeout) \
68 WaitForSingleObjectEx(obj, timeout, FALSE /*bAlertable*/)
69 #endif
70
pthread_create(pthread_t * const thread,const void * attr,unsigned int (__stdcall * start)(void *),void * arg)71 static INLINE int pthread_create(pthread_t *const thread, const void *attr,
72 unsigned int(__stdcall *start)(void *),
73 void *arg) {
74 (void)attr;
75 #ifdef USE_CREATE_THREAD
76 *thread = CreateThread(NULL, /* lpThreadAttributes */
77 0, /* dwStackSize */
78 start, arg, 0, /* dwStackSize */
79 NULL); /* lpThreadId */
80 #else
81 *thread = (pthread_t)_beginthreadex(NULL, /* void *security */
82 0, /* unsigned stack_size */
83 start, arg, 0, /* unsigned initflag */
84 NULL); /* unsigned *thrdaddr */
85 #endif
86 if (*thread == NULL) return 1;
87 SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
88 return 0;
89 }
90
pthread_join(pthread_t thread,void ** value_ptr)91 static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
92 (void)value_ptr;
93 return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 ||
94 CloseHandle(thread) == 0);
95 }
96
97 // Mutex
pthread_mutex_init(pthread_mutex_t * const mutex,void * mutexattr)98 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
99 void *mutexattr) {
100 (void)mutexattr;
101 #if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater
102 InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
103 #else
104 InitializeCriticalSection(mutex);
105 #endif
106 return 0;
107 }
108
pthread_mutex_trylock(pthread_mutex_t * const mutex)109 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
110 return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
111 }
112
pthread_mutex_lock(pthread_mutex_t * const mutex)113 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
114 EnterCriticalSection(mutex);
115 return 0;
116 }
117
pthread_mutex_unlock(pthread_mutex_t * const mutex)118 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
119 LeaveCriticalSection(mutex);
120 return 0;
121 }
122
pthread_mutex_destroy(pthread_mutex_t * const mutex)123 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
124 DeleteCriticalSection(mutex);
125 return 0;
126 }
127
128 // Condition
pthread_cond_destroy(pthread_cond_t * const condition)129 static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
130 int ok = 1;
131 #ifdef USE_WINDOWS_CONDITION_VARIABLE
132 (void)condition;
133 #else
134 ok &= (CloseHandle(condition->waiting_sem_) != 0);
135 ok &= (CloseHandle(condition->received_sem_) != 0);
136 ok &= (CloseHandle(condition->signal_event_) != 0);
137 #endif
138 return !ok;
139 }
140
pthread_cond_init(pthread_cond_t * const condition,void * cond_attr)141 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
142 void *cond_attr) {
143 (void)cond_attr;
144 #ifdef USE_WINDOWS_CONDITION_VARIABLE
145 InitializeConditionVariable(condition);
146 #else
147 condition->waiting_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
148 condition->received_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
149 condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
150 if (condition->waiting_sem_ == NULL || condition->received_sem_ == NULL ||
151 condition->signal_event_ == NULL) {
152 pthread_cond_destroy(condition);
153 return 1;
154 }
155 #endif
156 return 0;
157 }
158
pthread_cond_signal(pthread_cond_t * const condition)159 static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
160 int ok = 1;
161 #ifdef USE_WINDOWS_CONDITION_VARIABLE
162 WakeConditionVariable(condition);
163 #else
164 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 }
172 #endif
173 return !ok;
174 }
175
pthread_cond_broadcast(pthread_cond_t * const condition)176 static 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
pthread_cond_wait(pthread_cond_t * const condition,pthread_mutex_t * const mutex)193 static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
194 pthread_mutex_t *const mutex) {
195 int ok;
196 #ifdef USE_WINDOWS_CONDITION_VARIABLE
197 ok = SleepConditionVariableCS(condition, mutex, INFINITE);
198 #else
199 // note that there is a consumer available so the signal isn't dropped in
200 // pthread_cond_signal
201 if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL)) return 1;
202 // 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);
208 #endif
209 return !ok;
210 }
211 #elif defined(__OS2__)
212 #define INCL_DOS
213 #include <os2.h> // NOLINT
214
215 #include <errno.h> // NOLINT
216 #include <stdlib.h> // NOLINT
217 #include <sys/builtin.h> // NOLINT
218
219 #define pthread_t TID
220 #define pthread_mutex_t HMTX
221
222 typedef 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
234 typedef struct {
235 void *(*start_)(void *);
236 void *arg_;
237 } thread_arg;
238
239 static void thread_start(void *arg) {
240 thread_arg targ = *(thread_arg *)arg;
241 free(arg);
242
243 targ.start_(targ.arg_);
244 }
245
246 static INLINE int pthread_create(pthread_t *const thread, const void *attr,
247 void *(*start)(void *), void *arg) {
248 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
266 static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
267 (void)value_ptr;
268 return DosWaitThread(&thread, DCWW_WAIT) != 0;
269 }
270
271 // Mutex
272 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
273 void *mutexattr) {
274 (void)mutexattr;
275 return DosCreateMutexSem(NULL, mutex, 0, FALSE) != 0;
276 }
277
278 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
279 return DosRequestMutexSem(*mutex, SEM_IMMEDIATE_RETURN) == 0 ? 0 : EBUSY;
280 }
281
282 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
283 return DosRequestMutexSem(*mutex, SEM_INDEFINITE_WAIT) != 0;
284 }
285
286 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
287 return DosReleaseMutexSem(*mutex) != 0;
288 }
289
290 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
291 return DosCloseMutexSem(*mutex) != 0;
292 }
293
294 // Condition
295 static 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
302 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
303 void *cond_attr) {
304 int ok = 1;
305 (void)cond_attr;
306
307 ok &=
308 DosCreateEventSem(NULL, &condition->event_sem_, DCE_POSTONE, FALSE) == 0;
309 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
318 static 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
329 static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
330 int ok = 1;
331
332 while (!__atomic_cmpxchg32(&condition->wait_count_, 0, 0))
333 ok &= pthread_cond_signal(condition) == 0;
334
335 return !ok;
336 }
337
338 static 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 }
356 #else // _WIN32
357 #include <pthread.h> // NOLINT
358 #define THREADFN void *
359 #define THREAD_RETURN(val) val
360 #endif
361
362 #endif // CONFIG_MULTITHREAD
363
364 // State of the worker thread object
365 typedef enum {
366 NOT_OK = 0, // object is unusable
367 OK, // ready to work
368 WORK // busy finishing the current task
369 } AVxWorkerStatus;
370
371 // Function to be called by the worker thread. Takes two opaque pointers as
372 // arguments (data1 and data2). Should return true on success and return false
373 // in case of error.
374 typedef int (*AVxWorkerHook)(void *, void *);
375
376 // Platform-dependent implementation details for the worker.
377 typedef struct AVxWorkerImpl AVxWorkerImpl;
378
379 // Synchronization object used to launch job in the worker thread
380 typedef struct {
381 AVxWorkerImpl *impl_;
382 AVxWorkerStatus status_;
383 AVxWorkerHook hook; // hook to call
384 void *data1; // first argument passed to 'hook'
385 void *data2; // second argument passed to 'hook'
386 int had_error; // true if a call to 'hook' returned false
387 } AVxWorker;
388
389 // The interface for all thread-worker related functions. All these functions
390 // must be implemented.
391 typedef struct {
392 // Must be called first, before any other method.
393 void (*init)(AVxWorker *const worker);
394 // Must be called to initialize the object and spawn the thread. Re-entrant.
395 // Will potentially launch the thread. Returns false in case of error.
396 int (*reset)(AVxWorker *const worker);
397 // Makes sure the previous work is finished. Returns true if worker->had_error
398 // was not set and no error condition was triggered by the working thread.
399 int (*sync)(AVxWorker *const worker);
400 // Triggers the thread to call hook() with data1 and data2 arguments. These
401 // hook/data1/data2 values can be changed at any time before calling this
402 // function, but not be changed afterward until the next call to Sync().
403 void (*launch)(AVxWorker *const worker);
404 // This function is similar to launch() except that it calls the
405 // hook directly instead of using a thread. Convenient to bypass the thread
406 // mechanism while still using the AVxWorker structs. sync() must
407 // still be called afterward (for error reporting).
408 void (*execute)(AVxWorker *const worker);
409 // Kill the thread and terminate the object. To use the object again, one
410 // must call reset() again.
411 void (*end)(AVxWorker *const worker);
412 } AVxWorkerInterface;
413
414 // Install a new set of threading functions, overriding the defaults. This
415 // should be done before any workers are started, i.e., before any encoding or
416 // decoding takes place. The contents of the interface struct are copied, it
417 // is safe to free the corresponding memory after this call. This function is
418 // not thread-safe. Return false in case of invalid pointer or methods.
419 int aom_set_worker_interface(const AVxWorkerInterface *const winterface);
420
421 // Retrieve the currently set thread worker interface.
422 const AVxWorkerInterface *aom_get_worker_interface(void);
423
424 //------------------------------------------------------------------------------
425
426 #ifdef __cplusplus
427 } // extern "C"
428 #endif
429
430 #endif // AOM_AOM_UTIL_AOM_THREAD_H_
431