vp9_thread.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. // http://git.chromium.org/webm/libwebp.git
  14. // 100644 blob 7bd451b124ae3b81596abfbcc823e3cb129d3a38 src/utils/thread.h
  15. #ifndef VP9_DECODER_VP9_THREAD_H_
  16. #define VP9_DECODER_VP9_THREAD_H_
  17. #include "./vpx_config.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. // Set maximum decode threads to be 8 due to the limit of frame buffers
  22. // and not enough semaphores in the emulation layer on windows.
  23. #define MAX_DECODE_THREADS 8
  24. #if CONFIG_MULTITHREAD
  25. #if defined(_WIN32) && !HAVE_PTHREAD_H
  26. #include <errno.h> // NOLINT
  27. #include <process.h> // NOLINT
  28. #include <windows.h> // NOLINT
  29. typedef HANDLE pthread_t;
  30. typedef CRITICAL_SECTION pthread_mutex_t;
  31. typedef struct {
  32. HANDLE waiting_sem_;
  33. HANDLE received_sem_;
  34. HANDLE signal_event_;
  35. } pthread_cond_t;
  36. //------------------------------------------------------------------------------
  37. // simplistic pthread emulation layer
  38. // _beginthreadex requires __stdcall
  39. #define THREADFN unsigned int __stdcall
  40. #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
  41. static INLINE int pthread_create(pthread_t* const thread, const void* attr,
  42. unsigned int (__stdcall *start)(void*),
  43. void* arg) {
  44. (void)attr;
  45. *thread = (pthread_t)_beginthreadex(NULL, /* void *security */
  46. 0, /* unsigned stack_size */
  47. start,
  48. arg,
  49. 0, /* unsigned initflag */
  50. NULL); /* unsigned *thrdaddr */
  51. if (*thread == NULL) return 1;
  52. SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
  53. return 0;
  54. }
  55. static INLINE int pthread_join(pthread_t thread, void** value_ptr) {
  56. (void)value_ptr;
  57. return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 ||
  58. CloseHandle(thread) == 0);
  59. }
  60. // Mutex
  61. static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
  62. void* mutexattr) {
  63. (void)mutexattr;
  64. InitializeCriticalSection(mutex);
  65. return 0;
  66. }
  67. static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
  68. return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
  69. }
  70. static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
  71. EnterCriticalSection(mutex);
  72. return 0;
  73. }
  74. static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
  75. LeaveCriticalSection(mutex);
  76. return 0;
  77. }
  78. static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
  79. DeleteCriticalSection(mutex);
  80. return 0;
  81. }
  82. // Condition
  83. static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
  84. int ok = 1;
  85. ok &= (CloseHandle(condition->waiting_sem_) != 0);
  86. ok &= (CloseHandle(condition->received_sem_) != 0);
  87. ok &= (CloseHandle(condition->signal_event_) != 0);
  88. return !ok;
  89. }
  90. static INLINE int pthread_cond_init(pthread_cond_t *const condition,
  91. void* cond_attr) {
  92. (void)cond_attr;
  93. condition->waiting_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
  94. condition->received_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
  95. condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
  96. if (condition->waiting_sem_ == NULL ||
  97. condition->received_sem_ == NULL ||
  98. condition->signal_event_ == NULL) {
  99. pthread_cond_destroy(condition);
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
  105. int ok = 1;
  106. if (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
  107. // a thread is waiting in pthread_cond_wait: allow it to be notified
  108. ok = SetEvent(condition->signal_event_);
  109. // wait until the event is consumed so the signaler cannot consume
  110. // the event via its own pthread_cond_wait.
  111. ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
  112. WAIT_OBJECT_0);
  113. }
  114. return !ok;
  115. }
  116. static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
  117. pthread_mutex_t *const mutex) {
  118. int ok;
  119. // note that there is a consumer available so the signal isn't dropped in
  120. // pthread_cond_signal
  121. if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL))
  122. return 1;
  123. // now unlock the mutex so pthread_cond_signal may be issued
  124. pthread_mutex_unlock(mutex);
  125. ok = (WaitForSingleObject(condition->signal_event_, INFINITE) ==
  126. WAIT_OBJECT_0);
  127. ok &= ReleaseSemaphore(condition->received_sem_, 1, NULL);
  128. pthread_mutex_lock(mutex);
  129. return !ok;
  130. }
  131. #else // _WIN32
  132. #include <pthread.h> // NOLINT
  133. # define THREADFN void*
  134. # define THREAD_RETURN(val) val
  135. #endif
  136. #endif // CONFIG_MULTITHREAD
  137. // State of the worker thread object
  138. typedef enum {
  139. NOT_OK = 0, // object is unusable
  140. OK, // ready to work
  141. WORK // busy finishing the current task
  142. } VP9WorkerStatus;
  143. // Function to be called by the worker thread. Takes two opaque pointers as
  144. // arguments (data1 and data2), and should return false in case of error.
  145. typedef int (*VP9WorkerHook)(void*, void*);
  146. // Platform-dependent implementation details for the worker.
  147. typedef struct VP9WorkerImpl VP9WorkerImpl;
  148. // Synchronization object used to launch job in the worker thread
  149. typedef struct {
  150. VP9WorkerImpl *impl_;
  151. VP9WorkerStatus status_;
  152. VP9WorkerHook hook; // hook to call
  153. void *data1; // first argument passed to 'hook'
  154. void *data2; // second argument passed to 'hook'
  155. int had_error; // return value of the last call to 'hook'
  156. } VP9Worker;
  157. // The interface for all thread-worker related functions. All these functions
  158. // must be implemented.
  159. typedef struct {
  160. // Must be called first, before any other method.
  161. void (*init)(VP9Worker *const worker);
  162. // Must be called to initialize the object and spawn the thread. Re-entrant.
  163. // Will potentially launch the thread. Returns false in case of error.
  164. int (*reset)(VP9Worker *const worker);
  165. // Makes sure the previous work is finished. Returns true if worker->had_error
  166. // was not set and no error condition was triggered by the working thread.
  167. int (*sync)(VP9Worker *const worker);
  168. // Triggers the thread to call hook() with data1 and data2 arguments. These
  169. // hook/data1/data2 values can be changed at any time before calling this
  170. // function, but not be changed afterward until the next call to Sync().
  171. void (*launch)(VP9Worker *const worker);
  172. // This function is similar to launch() except that it calls the
  173. // hook directly instead of using a thread. Convenient to bypass the thread
  174. // mechanism while still using the VP9Worker structs. sync() must
  175. // still be called afterward (for error reporting).
  176. void (*execute)(VP9Worker *const worker);
  177. // Kill the thread and terminate the object. To use the object again, one
  178. // must call reset() again.
  179. void (*end)(VP9Worker *const worker);
  180. } VP9WorkerInterface;
  181. // Install a new set of threading functions, overriding the defaults. This
  182. // should be done before any workers are started, i.e., before any encoding or
  183. // decoding takes place. The contents of the interface struct are copied, it
  184. // is safe to free the corresponding memory after this call. This function is
  185. // not thread-safe. Return false in case of invalid pointer or methods.
  186. int vp9_set_worker_interface(const VP9WorkerInterface *const winterface);
  187. // Retrieve the currently set thread worker interface.
  188. const VP9WorkerInterface *vp9_get_worker_interface(void);
  189. //------------------------------------------------------------------------------
  190. #ifdef __cplusplus
  191. } // extern "C"
  192. #endif
  193. #endif // VP9_DECODER_VP9_THREAD_H_