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