• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
215 #elif defined(__OS2__)
216 #define INCL_DOS
217 #include <os2.h>  // NOLINT
218 
219 #include <errno.h>        // NOLINT
220 #include <stdlib.h>       // NOLINT
221 #include <sys/builtin.h>  // NOLINT
222 
223 #if defined(__STRICT_ANSI__)
224 // _beginthread() is not declared on __STRICT_ANSI__ mode. Declare here.
225 int _beginthread(void (*)(void *), void *, unsigned, void *);
226 #endif
227 
228 #define pthread_t TID
229 #define pthread_mutex_t HMTX
230 
231 typedef struct {
232   HEV event_sem_;
233   HEV ack_sem_;
234   volatile unsigned wait_count_;
235 } pthread_cond_t;
236 
237 //------------------------------------------------------------------------------
238 // simplistic pthread emulation layer
239 
240 #define THREADFN void *
241 #define THREAD_RETURN(val) (val)
242 
243 typedef struct {
244   void *(*start_)(void *);
245   void *arg_;
246 } thread_arg;
247 
248 static void thread_start(void *arg) {
249   thread_arg targ = *(thread_arg *)arg;
250   free(arg);
251 
252   targ.start_(targ.arg_);
253 }
254 
255 static INLINE int pthread_create(pthread_t *const thread, const void *attr,
256                                  void *(*start)(void *), void *arg) {
257   int tid;
258   thread_arg *targ = (thread_arg *)malloc(sizeof(*targ));
259   if (targ == NULL) return 1;
260 
261   (void)attr;
262 
263   targ->start_ = start;
264   targ->arg_ = arg;
265   tid = (pthread_t)_beginthread(thread_start, NULL, 1024 * 1024, targ);
266   if (tid == -1) {
267     free(targ);
268     return 1;
269   }
270 
271   *thread = tid;
272   return 0;
273 }
274 
275 static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
276   (void)value_ptr;
277   return DosWaitThread(&thread, DCWW_WAIT) != 0;
278 }
279 
280 // Mutex
281 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
282                                      void *mutexattr) {
283   (void)mutexattr;
284   return DosCreateMutexSem(NULL, mutex, 0, FALSE) != 0;
285 }
286 
287 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
288   return DosRequestMutexSem(*mutex, SEM_IMMEDIATE_RETURN) == 0 ? 0 : EBUSY;
289 }
290 
291 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
292   return DosRequestMutexSem(*mutex, SEM_INDEFINITE_WAIT) != 0;
293 }
294 
295 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
296   return DosReleaseMutexSem(*mutex) != 0;
297 }
298 
299 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
300   return DosCloseMutexSem(*mutex) != 0;
301 }
302 
303 // Condition
304 static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
305   int ok = 1;
306   ok &= DosCloseEventSem(condition->event_sem_) == 0;
307   ok &= DosCloseEventSem(condition->ack_sem_) == 0;
308   return !ok;
309 }
310 
311 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
312                                     void *cond_attr) {
313   int ok = 1;
314   (void)cond_attr;
315 
316   ok &=
317       DosCreateEventSem(NULL, &condition->event_sem_, DCE_POSTONE, FALSE) == 0;
318   ok &= DosCreateEventSem(NULL, &condition->ack_sem_, DCE_POSTONE, FALSE) == 0;
319   if (!ok) {
320     pthread_cond_destroy(condition);
321     return 1;
322   }
323   condition->wait_count_ = 0;
324   return 0;
325 }
326 
327 static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
328   int ok = 1;
329 
330   if (!__atomic_cmpxchg32(&condition->wait_count_, 0, 0)) {
331     ok &= DosPostEventSem(condition->event_sem_) == 0;
332     ok &= DosWaitEventSem(condition->ack_sem_, SEM_INDEFINITE_WAIT) == 0;
333   }
334 
335   return !ok;
336 }
337 
338 static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
339   int ok = 1;
340 
341   while (!__atomic_cmpxchg32(&condition->wait_count_, 0, 0))
342     ok &= pthread_cond_signal(condition) == 0;
343 
344   return !ok;
345 }
346 
347 static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
348                                     pthread_mutex_t *const mutex) {
349   int ok = 1;
350 
351   __atomic_increment(&condition->wait_count_);
352 
353   ok &= pthread_mutex_unlock(mutex) == 0;
354 
355   ok &= DosWaitEventSem(condition->event_sem_, SEM_INDEFINITE_WAIT) == 0;
356 
357   __atomic_decrement(&condition->wait_count_);
358 
359   ok &= DosPostEventSem(condition->ack_sem_) == 0;
360 
361   pthread_mutex_lock(mutex);
362 
363   return !ok;
364 }
365 #else                 // _WIN32
366 #include <pthread.h>  // NOLINT
367 #define THREADFN void *
368 #define THREAD_RETURN(val) val
369 #endif
370 
371 #endif  // CONFIG_MULTITHREAD
372 
373 // State of the worker thread object
374 typedef enum {
375   NOT_OK = 0,  // object is unusable
376   OK,          // ready to work
377   WORK         // busy finishing the current task
378 } VPxWorkerStatus;
379 
380 // Function to be called by the worker thread. Takes two opaque pointers as
381 // arguments (data1 and data2), and should return false in case of error.
382 typedef int (*VPxWorkerHook)(void *, void *);
383 
384 // Platform-dependent implementation details for the worker.
385 typedef struct VPxWorkerImpl VPxWorkerImpl;
386 
387 // Synchronization object used to launch job in the worker thread
388 typedef struct {
389   VPxWorkerImpl *impl_;
390   VPxWorkerStatus status_;
391   VPxWorkerHook hook;  // hook to call
392   void *data1;         // first argument passed to 'hook'
393   void *data2;         // second argument passed to 'hook'
394   int had_error;       // return value of the last call to 'hook'
395 } VPxWorker;
396 
397 // The interface for all thread-worker related functions. All these functions
398 // must be implemented.
399 typedef struct {
400   // Must be called first, before any other method.
401   void (*init)(VPxWorker *const worker);
402   // Must be called to initialize the object and spawn the thread. Re-entrant.
403   // Will potentially launch the thread. Returns false in case of error.
404   int (*reset)(VPxWorker *const worker);
405   // Makes sure the previous work is finished. Returns true if worker->had_error
406   // was not set and no error condition was triggered by the working thread.
407   int (*sync)(VPxWorker *const worker);
408   // Triggers the thread to call hook() with data1 and data2 arguments. These
409   // hook/data1/data2 values can be changed at any time before calling this
410   // function, but not be changed afterward until the next call to Sync().
411   void (*launch)(VPxWorker *const worker);
412   // This function is similar to launch() except that it calls the
413   // hook directly instead of using a thread. Convenient to bypass the thread
414   // mechanism while still using the VPxWorker structs. sync() must
415   // still be called afterward (for error reporting).
416   void (*execute)(VPxWorker *const worker);
417   // Kill the thread and terminate the object. To use the object again, one
418   // must call reset() again.
419   void (*end)(VPxWorker *const worker);
420 } VPxWorkerInterface;
421 
422 // Install a new set of threading functions, overriding the defaults. This
423 // should be done before any workers are started, i.e., before any encoding or
424 // decoding takes place. The contents of the interface struct are copied, it
425 // is safe to free the corresponding memory after this call. This function is
426 // not thread-safe. Return false in case of invalid pointer or methods.
427 int vpx_set_worker_interface(const VPxWorkerInterface *const winterface);
428 
429 // Retrieve the currently set thread worker interface.
430 const VPxWorkerInterface *vpx_get_worker_interface(void);
431 
432 //------------------------------------------------------------------------------
433 
434 #ifdef __cplusplus
435 }  // extern "C"
436 #endif
437 
438 #endif  // VPX_VPX_UTIL_VPX_THREAD_H_
439