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