vpx_thread.h 13 KB

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