vpx_thread.h 14 KB

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