thread_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Copyright 2011 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. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <assert.h>
  14. #include <string.h> // for memset()
  15. #include "./thread_utils.h"
  16. #include "./utils.h"
  17. #ifdef WEBP_USE_THREAD
  18. #if defined(_WIN32)
  19. #include <windows.h>
  20. typedef HANDLE pthread_t;
  21. typedef CRITICAL_SECTION pthread_mutex_t;
  22. #if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater
  23. #define USE_WINDOWS_CONDITION_VARIABLE
  24. typedef CONDITION_VARIABLE pthread_cond_t;
  25. #else
  26. typedef struct {
  27. HANDLE waiting_sem_;
  28. HANDLE received_sem_;
  29. HANDLE signal_event_;
  30. } pthread_cond_t;
  31. #endif // _WIN32_WINNT >= 0x600
  32. #ifndef WINAPI_FAMILY_PARTITION
  33. #define WINAPI_PARTITION_DESKTOP 1
  34. #define WINAPI_FAMILY_PARTITION(x) x
  35. #endif
  36. #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
  37. #define USE_CREATE_THREAD
  38. #endif
  39. #else // !_WIN32
  40. #include <pthread.h>
  41. #endif // _WIN32
  42. typedef struct {
  43. pthread_mutex_t mutex_;
  44. pthread_cond_t condition_;
  45. pthread_t thread_;
  46. } WebPWorkerImpl;
  47. #if defined(_WIN32)
  48. //------------------------------------------------------------------------------
  49. // simplistic pthread emulation layer
  50. #include <process.h>
  51. // _beginthreadex requires __stdcall
  52. #define THREADFN unsigned int __stdcall
  53. #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
  54. #if _WIN32_WINNT >= 0x0501 // Windows XP or greater
  55. #define WaitForSingleObject(obj, timeout) \
  56. WaitForSingleObjectEx(obj, timeout, FALSE /*bAlertable*/)
  57. #endif
  58. static int pthread_create(pthread_t* const thread, const void* attr,
  59. unsigned int (__stdcall* start)(void*), void* arg) {
  60. (void)attr;
  61. #ifdef USE_CREATE_THREAD
  62. *thread = CreateThread(NULL, /* lpThreadAttributes */
  63. 0, /* dwStackSize */
  64. start,
  65. arg,
  66. 0, /* dwStackSize */
  67. NULL); /* lpThreadId */
  68. #else
  69. *thread = (pthread_t)_beginthreadex(NULL, /* void *security */
  70. 0, /* unsigned stack_size */
  71. start,
  72. arg,
  73. 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 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 int pthread_mutex_init(pthread_mutex_t* const mutex, void* mutexattr) {
  87. (void)mutexattr;
  88. #if _WIN32_WINNT >= 0x0600 // Windows Vista / Server 2008 or greater
  89. InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
  90. #else
  91. InitializeCriticalSection(mutex);
  92. #endif
  93. return 0;
  94. }
  95. static int pthread_mutex_lock(pthread_mutex_t* const mutex) {
  96. EnterCriticalSection(mutex);
  97. return 0;
  98. }
  99. static int pthread_mutex_unlock(pthread_mutex_t* const mutex) {
  100. LeaveCriticalSection(mutex);
  101. return 0;
  102. }
  103. static int pthread_mutex_destroy(pthread_mutex_t* const mutex) {
  104. DeleteCriticalSection(mutex);
  105. return 0;
  106. }
  107. // Condition
  108. static int pthread_cond_destroy(pthread_cond_t* const condition) {
  109. int ok = 1;
  110. #ifdef USE_WINDOWS_CONDITION_VARIABLE
  111. (void)condition;
  112. #else
  113. ok &= (CloseHandle(condition->waiting_sem_) != 0);
  114. ok &= (CloseHandle(condition->received_sem_) != 0);
  115. ok &= (CloseHandle(condition->signal_event_) != 0);
  116. #endif
  117. return !ok;
  118. }
  119. static int pthread_cond_init(pthread_cond_t* const condition, void* cond_attr) {
  120. (void)cond_attr;
  121. #ifdef USE_WINDOWS_CONDITION_VARIABLE
  122. InitializeConditionVariable(condition);
  123. #else
  124. condition->waiting_sem_ = CreateSemaphore(NULL, 0, 1, NULL);
  125. condition->received_sem_ = CreateSemaphore(NULL, 0, 1, NULL);
  126. condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
  127. if (condition->waiting_sem_ == NULL ||
  128. condition->received_sem_ == NULL ||
  129. condition->signal_event_ == NULL) {
  130. pthread_cond_destroy(condition);
  131. return 1;
  132. }
  133. #endif
  134. return 0;
  135. }
  136. static int pthread_cond_signal(pthread_cond_t* const condition) {
  137. int ok = 1;
  138. #ifdef USE_WINDOWS_CONDITION_VARIABLE
  139. WakeConditionVariable(condition);
  140. #else
  141. if (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
  142. // a thread is waiting in pthread_cond_wait: allow it to be notified
  143. ok = SetEvent(condition->signal_event_);
  144. // wait until the event is consumed so the signaler cannot consume
  145. // the event via its own pthread_cond_wait.
  146. ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
  147. WAIT_OBJECT_0);
  148. }
  149. #endif
  150. return !ok;
  151. }
  152. static int pthread_cond_wait(pthread_cond_t* const condition,
  153. pthread_mutex_t* const mutex) {
  154. int ok;
  155. #ifdef USE_WINDOWS_CONDITION_VARIABLE
  156. ok = SleepConditionVariableCS(condition, mutex, INFINITE);
  157. #else
  158. // note that there is a consumer available so the signal isn't dropped in
  159. // pthread_cond_signal
  160. if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL)) return 1;
  161. // now unlock the mutex so pthread_cond_signal may be issued
  162. pthread_mutex_unlock(mutex);
  163. ok = (WaitForSingleObject(condition->signal_event_, INFINITE) ==
  164. WAIT_OBJECT_0);
  165. ok &= ReleaseSemaphore(condition->received_sem_, 1, NULL);
  166. pthread_mutex_lock(mutex);
  167. #endif
  168. return !ok;
  169. }
  170. #else // !_WIN32
  171. # define THREADFN void*
  172. # define THREAD_RETURN(val) val
  173. #endif // _WIN32
  174. //------------------------------------------------------------------------------
  175. static THREADFN ThreadLoop(void* ptr) {
  176. WebPWorker* const worker = (WebPWorker*)ptr;
  177. WebPWorkerImpl* const impl = (WebPWorkerImpl*)worker->impl_;
  178. int done = 0;
  179. while (!done) {
  180. pthread_mutex_lock(&impl->mutex_);
  181. while (worker->status_ == OK) { // wait in idling mode
  182. pthread_cond_wait(&impl->condition_, &impl->mutex_);
  183. }
  184. if (worker->status_ == WORK) {
  185. WebPGetWorkerInterface()->Execute(worker);
  186. worker->status_ = OK;
  187. } else if (worker->status_ == NOT_OK) { // finish the worker
  188. done = 1;
  189. }
  190. // signal to the main thread that we're done (for Sync())
  191. // Note the associated mutex does not need to be held when signaling the
  192. // condition. Unlocking the mutex first may improve performance in some
  193. // implementations, avoiding the case where the waiting thread can't
  194. // reacquire the mutex when woken.
  195. pthread_mutex_unlock(&impl->mutex_);
  196. pthread_cond_signal(&impl->condition_);
  197. }
  198. return THREAD_RETURN(NULL); // Thread is finished
  199. }
  200. // main thread state control
  201. static void ChangeState(WebPWorker* const worker, WebPWorkerStatus new_status) {
  202. // No-op when attempting to change state on a thread that didn't come up.
  203. // Checking status_ without acquiring the lock first would result in a data
  204. // race.
  205. WebPWorkerImpl* const impl = (WebPWorkerImpl*)worker->impl_;
  206. if (impl == NULL) return;
  207. pthread_mutex_lock(&impl->mutex_);
  208. if (worker->status_ >= OK) {
  209. // wait for the worker to finish
  210. while (worker->status_ != OK) {
  211. pthread_cond_wait(&impl->condition_, &impl->mutex_);
  212. }
  213. // assign new status and release the working thread if needed
  214. if (new_status != OK) {
  215. worker->status_ = new_status;
  216. // Note the associated mutex does not need to be held when signaling the
  217. // condition. Unlocking the mutex first may improve performance in some
  218. // implementations, avoiding the case where the waiting thread can't
  219. // reacquire the mutex when woken.
  220. pthread_mutex_unlock(&impl->mutex_);
  221. pthread_cond_signal(&impl->condition_);
  222. return;
  223. }
  224. }
  225. pthread_mutex_unlock(&impl->mutex_);
  226. }
  227. #endif // WEBP_USE_THREAD
  228. //------------------------------------------------------------------------------
  229. static void Init(WebPWorker* const worker) {
  230. memset(worker, 0, sizeof(*worker));
  231. worker->status_ = NOT_OK;
  232. }
  233. static int Sync(WebPWorker* const worker) {
  234. #ifdef WEBP_USE_THREAD
  235. ChangeState(worker, OK);
  236. #endif
  237. assert(worker->status_ <= OK);
  238. return !worker->had_error;
  239. }
  240. static int Reset(WebPWorker* const worker) {
  241. int ok = 1;
  242. worker->had_error = 0;
  243. if (worker->status_ < OK) {
  244. #ifdef WEBP_USE_THREAD
  245. WebPWorkerImpl* const impl =
  246. (WebPWorkerImpl*)WebPSafeCalloc(1, sizeof(WebPWorkerImpl));
  247. worker->impl_ = (void*)impl;
  248. if (worker->impl_ == NULL) {
  249. return 0;
  250. }
  251. if (pthread_mutex_init(&impl->mutex_, NULL)) {
  252. goto Error;
  253. }
  254. if (pthread_cond_init(&impl->condition_, NULL)) {
  255. pthread_mutex_destroy(&impl->mutex_);
  256. goto Error;
  257. }
  258. pthread_mutex_lock(&impl->mutex_);
  259. ok = !pthread_create(&impl->thread_, NULL, ThreadLoop, worker);
  260. if (ok) worker->status_ = OK;
  261. pthread_mutex_unlock(&impl->mutex_);
  262. if (!ok) {
  263. pthread_mutex_destroy(&impl->mutex_);
  264. pthread_cond_destroy(&impl->condition_);
  265. Error:
  266. WebPSafeFree(impl);
  267. worker->impl_ = NULL;
  268. return 0;
  269. }
  270. #else
  271. worker->status_ = OK;
  272. #endif
  273. } else if (worker->status_ > OK) {
  274. ok = Sync(worker);
  275. }
  276. assert(!ok || (worker->status_ == OK));
  277. return ok;
  278. }
  279. static void Execute(WebPWorker* const worker) {
  280. if (worker->hook != NULL) {
  281. worker->had_error |= !worker->hook(worker->data1, worker->data2);
  282. }
  283. }
  284. static void Launch(WebPWorker* const worker) {
  285. #ifdef WEBP_USE_THREAD
  286. ChangeState(worker, WORK);
  287. #else
  288. Execute(worker);
  289. #endif
  290. }
  291. static void End(WebPWorker* const worker) {
  292. #ifdef WEBP_USE_THREAD
  293. if (worker->impl_ != NULL) {
  294. WebPWorkerImpl* const impl = (WebPWorkerImpl*)worker->impl_;
  295. ChangeState(worker, NOT_OK);
  296. pthread_join(impl->thread_, NULL);
  297. pthread_mutex_destroy(&impl->mutex_);
  298. pthread_cond_destroy(&impl->condition_);
  299. WebPSafeFree(impl);
  300. worker->impl_ = NULL;
  301. }
  302. #else
  303. worker->status_ = NOT_OK;
  304. assert(worker->impl_ == NULL);
  305. #endif
  306. assert(worker->status_ == NOT_OK);
  307. }
  308. //------------------------------------------------------------------------------
  309. static WebPWorkerInterface g_worker_interface = {
  310. Init, Reset, Sync, Launch, Execute, End
  311. };
  312. int WebPSetWorkerInterface(const WebPWorkerInterface* const winterface) {
  313. if (winterface == NULL ||
  314. winterface->Init == NULL || winterface->Reset == NULL ||
  315. winterface->Sync == NULL || winterface->Launch == NULL ||
  316. winterface->Execute == NULL || winterface->End == NULL) {
  317. return 0;
  318. }
  319. g_worker_interface = *winterface;
  320. return 1;
  321. }
  322. const WebPWorkerInterface* WebPGetWorkerInterface(void) {
  323. return &g_worker_interface;
  324. }
  325. //------------------------------------------------------------------------------