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 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 17 | #ifndef AOM_AOM_UTIL_AOM_THREAD_H_ |
| 18 | #define AOM_AOM_UTIL_AOM_THREAD_H_ |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 19 | |
Tom Finegan | 60e653d | 2018-05-22 11:34:58 -0700 | [diff] [blame] | 20 | #include "config/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 |
Ravi Chaudhary | 3117948 | 2018-10-08 11:12:43 +0530 | [diff] [blame] | 29 | #define MAX_NUM_THREADS 64 |
hkuang | dd88f48 | 2015-02-05 13:59:15 -0800 | [diff] [blame] | 30 | |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 31 | #if CONFIG_MULTITHREAD |
| 32 | |
James Zern | d167a1a | 2015-02-10 12:47:14 -0800 | [diff] [blame] | 33 | #if defined(_WIN32) && !HAVE_PTHREAD_H |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 34 | #include <errno.h> // NOLINT |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 35 | #include <process.h> // NOLINT |
Jim Bankoski | c52d854 | 2013-10-01 15:19:39 -0700 | [diff] [blame] | 36 | #include <windows.h> // NOLINT |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 37 | typedef HANDLE pthread_t; |
| 38 | typedef CRITICAL_SECTION pthread_mutex_t; |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 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 |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 44 | typedef struct { |
| 45 | HANDLE waiting_sem_; |
| 46 | HANDLE received_sem_; |
| 47 | HANDLE signal_event_; |
| 48 | } pthread_cond_t; |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 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 |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 59 | |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 60 | //------------------------------------------------------------------------------ |
| 61 | // simplistic pthread emulation layer |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 62 | |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 63 | // _beginthreadex requires __stdcall |
| 64 | #define THREADFN unsigned int __stdcall |
| 65 | #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val) |
| 66 | |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 67 | #if _WIN32_WINNT >= 0x0501 // Windows XP or greater |
| 68 | #define WaitForSingleObject(obj, timeout) \ |
| 69 | WaitForSingleObjectEx(obj, timeout, FALSE /*bAlertable*/) |
| 70 | #endif |
| 71 | |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 72 | static INLINE int pthread_create(pthread_t *const thread, const void *attr, |
| 73 | unsigned int(__stdcall *start)(void *), |
| 74 | void *arg) { |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 75 | (void)attr; |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 76 | #ifdef USE_CREATE_THREAD |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 77 | *thread = CreateThread(NULL, /* lpThreadAttributes */ |
| 78 | 0, /* dwStackSize */ |
| 79 | start, arg, 0, /* dwStackSize */ |
| 80 | NULL); /* lpThreadId */ |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 81 | #else |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 82 | *thread = (pthread_t)_beginthreadex(NULL, /* void *security */ |
| 83 | 0, /* unsigned stack_size */ |
| 84 | start, arg, 0, /* unsigned initflag */ |
| 85 | NULL); /* unsigned *thrdaddr */ |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 86 | #endif |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 87 | if (*thread == NULL) return 1; |
| 88 | SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL); |
| 89 | return 0; |
| 90 | } |
| 91 | |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 92 | static INLINE int pthread_join(pthread_t thread, void **value_ptr) { |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 93 | (void)value_ptr; |
| 94 | return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 || |
| 95 | CloseHandle(thread) == 0); |
| 96 | } |
| 97 | |
| 98 | // Mutex |
| 99 | static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex, |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 100 | void *mutexattr) { |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 101 | (void)mutexattr; |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 102 | #if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater |
| 103 | InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/); |
| 104 | #else |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 105 | InitializeCriticalSection(mutex); |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 106 | #endif |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) { |
| 111 | return TryEnterCriticalSection(mutex) ? 0 : EBUSY; |
| 112 | } |
| 113 | |
| 114 | static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) { |
| 115 | EnterCriticalSection(mutex); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) { |
| 120 | LeaveCriticalSection(mutex); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) { |
| 125 | DeleteCriticalSection(mutex); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | // Condition |
| 130 | static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) { |
| 131 | int ok = 1; |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 132 | #ifdef USE_WINDOWS_CONDITION_VARIABLE |
| 133 | (void)condition; |
| 134 | #else |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 135 | ok &= (CloseHandle(condition->waiting_sem_) != 0); |
| 136 | ok &= (CloseHandle(condition->received_sem_) != 0); |
| 137 | ok &= (CloseHandle(condition->signal_event_) != 0); |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 138 | #endif |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 139 | return !ok; |
| 140 | } |
| 141 | |
| 142 | static INLINE int pthread_cond_init(pthread_cond_t *const condition, |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 143 | void *cond_attr) { |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 144 | (void)cond_attr; |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 145 | #ifdef USE_WINDOWS_CONDITION_VARIABLE |
| 146 | InitializeConditionVariable(condition); |
| 147 | #else |
hkuang | dd88f48 | 2015-02-05 13:59:15 -0800 | [diff] [blame] | 148 | condition->waiting_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL); |
| 149 | condition->received_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL); |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 150 | condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL); |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 151 | if (condition->waiting_sem_ == NULL || condition->received_sem_ == NULL || |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 152 | condition->signal_event_ == NULL) { |
| 153 | pthread_cond_destroy(condition); |
| 154 | return 1; |
| 155 | } |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 156 | #endif |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static INLINE int pthread_cond_signal(pthread_cond_t *const condition) { |
| 161 | int ok = 1; |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 162 | #ifdef USE_WINDOWS_CONDITION_VARIABLE |
| 163 | WakeConditionVariable(condition); |
| 164 | #else |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 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 | } |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 173 | #endif |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 174 | return !ok; |
| 175 | } |
| 176 | |
Deepa K G | 964e72e | 2018-05-16 16:56:01 +0530 | [diff] [blame] | 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 | |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 194 | static INLINE int pthread_cond_wait(pthread_cond_t *const condition, |
| 195 | pthread_mutex_t *const mutex) { |
| 196 | int ok; |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 197 | #ifdef USE_WINDOWS_CONDITION_VARIABLE |
| 198 | ok = SleepConditionVariableCS(condition, mutex, INFINITE); |
| 199 | #else |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 200 | // note that there is a consumer available so the signal isn't dropped in |
| 201 | // pthread_cond_signal |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 202 | if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL)) return 1; |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 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); |
Johann | 2967bf3 | 2016-06-22 16:08:10 -0700 | [diff] [blame] | 209 | #endif |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 210 | return !ok; |
| 211 | } |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 212 | #elif defined(__OS2__) |
| 213 | #define INCL_DOS |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 214 | #include <os2.h> // NOLINT |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 215 | |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 216 | #include <errno.h> // NOLINT |
| 217 | #include <stdlib.h> // NOLINT |
| 218 | #include <sys/builtin.h> // NOLINT |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 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 { |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 236 | void *(*start_)(void *); |
| 237 | void *arg_; |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 238 | } thread_arg; |
| 239 | |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 240 | static void thread_start(void *arg) { |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 241 | thread_arg targ = *(thread_arg *)arg; |
| 242 | free(arg); |
| 243 | |
| 244 | targ.start_(targ.arg_); |
| 245 | } |
| 246 | |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 247 | static INLINE int pthread_create(pthread_t *const thread, const void *attr, |
| 248 | void *(*start)(void *), void *arg) { |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 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 | |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 267 | static INLINE int pthread_join(pthread_t thread, void **value_ptr) { |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 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, |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 274 | void *mutexattr) { |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 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, |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 304 | void *cond_attr) { |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 305 | int ok = 1; |
| 306 | (void)cond_attr; |
| 307 | |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 308 | ok &= |
| 309 | DosCreateEventSem(NULL, &condition->event_sem_, DCE_POSTONE, FALSE) == 0; |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 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)) |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 334 | ok &= pthread_cond_signal(condition) == 0; |
KO Myung-Hun | 14e8ade | 2016-05-22 13:57:07 +0900 | [diff] [blame] | 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 | } |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 357 | #else // _WIN32 |
| 358 | #include <pthread.h> // NOLINT |
| 359 | #define THREADFN void * |
| 360 | #define THREAD_RETURN(val) val |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 361 | #endif |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 362 | |
Yunqing Wang | 903801f | 2013-12-27 15:25:54 -0800 | [diff] [blame] | 363 | #endif // CONFIG_MULTITHREAD |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 364 | |
| 365 | // State of the worker thread object |
Satish Kumar Suman | 9b1252e | 2018-12-25 11:21:18 +0530 | [diff] [blame] | 366 | typedef enum { |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 367 | NOT_OK = 0, // object is unusable |
| 368 | OK, // ready to work |
| 369 | WORK // busy finishing the current task |
Satish Kumar Suman | 9b1252e | 2018-12-25 11:21:18 +0530 | [diff] [blame] | 370 | } AVxWorkerStatus; |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 371 | |
| 372 | // Function to be called by the worker thread. Takes two opaque pointers as |
Wan-Teh Chang | 3f0cbf1 | 2018-07-03 14:59:18 -0700 | [diff] [blame] | 373 | // arguments (data1 and data2). Should return true on success and return false |
| 374 | // in case of error. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 375 | typedef int (*AVxWorkerHook)(void *, void *); |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 376 | |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 377 | // Platform-dependent implementation details for the worker. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 378 | typedef struct AVxWorkerImpl AVxWorkerImpl; |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 379 | |
| 380 | // Synchronization object used to launch job in the worker thread |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 381 | typedef struct { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 382 | AVxWorkerImpl *impl_; |
| 383 | AVxWorkerStatus status_; |
Wan-Teh Chang | 4d29ee8 | 2018-09-20 10:07:52 -0700 | [diff] [blame] | 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; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 388 | AVxWorkerHook hook; // hook to call |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 389 | void *data1; // first argument passed to 'hook' |
| 390 | void *data2; // second argument passed to 'hook' |
Wan-Teh Chang | 3f0cbf1 | 2018-07-03 14:59:18 -0700 | [diff] [blame] | 391 | int had_error; // true if a call to 'hook' returned false |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 392 | } AVxWorker; |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 393 | |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 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. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 398 | void (*init)(AVxWorker *const worker); |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 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. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 401 | int (*reset)(AVxWorker *const worker); |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 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. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 404 | int (*sync)(AVxWorker *const worker); |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 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(). |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 408 | void (*launch)(AVxWorker *const worker); |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 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 |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 411 | // mechanism while still using the AVxWorker structs. sync() must |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 412 | // still be called afterward (for error reporting). |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 413 | void (*execute)(AVxWorker *const worker); |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 414 | // Kill the thread and terminate the object. To use the object again, one |
| 415 | // must call reset() again. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 416 | void (*end)(AVxWorker *const worker); |
| 417 | } AVxWorkerInterface; |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 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. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 424 | int aom_set_worker_interface(const AVxWorkerInterface *const winterface); |
James Zern | e656f44 | 2014-06-19 21:14:51 -0700 | [diff] [blame] | 425 | |
| 426 | // Retrieve the currently set thread worker interface. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 427 | const AVxWorkerInterface *aom_get_worker_interface(void); |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 428 | |
| 429 | //------------------------------------------------------------------------------ |
| 430 | |
James Zern | 40aa910 | 2014-01-18 12:16:11 -0800 | [diff] [blame] | 431 | #ifdef __cplusplus |
clang-format | 3a992f8 | 2016-08-10 17:02:06 -0700 | [diff] [blame] | 432 | } // extern "C" |
James Zern | 183b77d | 2013-07-30 22:46:58 -0700 | [diff] [blame] | 433 | #endif |
| 434 | |
James Zern | e1cbb13 | 2018-08-22 14:10:36 -0700 | [diff] [blame] | 435 | #endif // AOM_AOM_UTIL_AOM_THREAD_H_ |