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