thread.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.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. typedef struct {
  23. HANDLE waiting_sem_;
  24. HANDLE received_sem_;
  25. HANDLE signal_event_;
  26. } pthread_cond_t;
  27. #else // !_WIN32
  28. #include <pthread.h>
  29. #endif // _WIN32
  30. struct WebPWorkerImpl {
  31. pthread_mutex_t mutex_;
  32. pthread_cond_t condition_;
  33. pthread_t thread_;
  34. };
  35. #if defined(_WIN32)
  36. //------------------------------------------------------------------------------
  37. // simplistic pthread emulation layer
  38. #include <process.h>
  39. // _beginthreadex requires __stdcall
  40. #define THREADFN unsigned int __stdcall
  41. #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
  42. static int pthread_create(pthread_t* const thread, const void* attr,
  43. unsigned int (__stdcall *start)(void*), 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 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 int pthread_mutex_init(pthread_mutex_t* const mutex, void* mutexattr) {
  62. (void)mutexattr;
  63. InitializeCriticalSection(mutex);
  64. return 0;
  65. }
  66. static int pthread_mutex_lock(pthread_mutex_t* const mutex) {
  67. EnterCriticalSection(mutex);
  68. return 0;
  69. }
  70. static int pthread_mutex_unlock(pthread_mutex_t* const mutex) {
  71. LeaveCriticalSection(mutex);
  72. return 0;
  73. }
  74. static int pthread_mutex_destroy(pthread_mutex_t* const mutex) {
  75. DeleteCriticalSection(mutex);
  76. return 0;
  77. }
  78. // Condition
  79. static int pthread_cond_destroy(pthread_cond_t* const condition) {
  80. int ok = 1;
  81. ok &= (CloseHandle(condition->waiting_sem_) != 0);
  82. ok &= (CloseHandle(condition->received_sem_) != 0);
  83. ok &= (CloseHandle(condition->signal_event_) != 0);
  84. return !ok;
  85. }
  86. static int pthread_cond_init(pthread_cond_t* const condition, void* cond_attr) {
  87. (void)cond_attr;
  88. condition->waiting_sem_ = CreateSemaphore(NULL, 0, 1, NULL);
  89. condition->received_sem_ = CreateSemaphore(NULL, 0, 1, NULL);
  90. condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
  91. if (condition->waiting_sem_ == NULL ||
  92. condition->received_sem_ == NULL ||
  93. condition->signal_event_ == NULL) {
  94. pthread_cond_destroy(condition);
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. static int pthread_cond_signal(pthread_cond_t* const condition) {
  100. int ok = 1;
  101. if (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
  102. // a thread is waiting in pthread_cond_wait: allow it to be notified
  103. ok = SetEvent(condition->signal_event_);
  104. // wait until the event is consumed so the signaler cannot consume
  105. // the event via its own pthread_cond_wait.
  106. ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
  107. WAIT_OBJECT_0);
  108. }
  109. return !ok;
  110. }
  111. static int pthread_cond_wait(pthread_cond_t* const condition,
  112. pthread_mutex_t* const mutex) {
  113. int ok;
  114. // note that there is a consumer available so the signal isn't dropped in
  115. // pthread_cond_signal
  116. if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL))
  117. return 1;
  118. // now unlock the mutex so pthread_cond_signal may be issued
  119. pthread_mutex_unlock(mutex);
  120. ok = (WaitForSingleObject(condition->signal_event_, INFINITE) ==
  121. WAIT_OBJECT_0);
  122. ok &= ReleaseSemaphore(condition->received_sem_, 1, NULL);
  123. pthread_mutex_lock(mutex);
  124. return !ok;
  125. }
  126. #else // !_WIN32
  127. # define THREADFN void*
  128. # define THREAD_RETURN(val) val
  129. #endif // _WIN32
  130. //------------------------------------------------------------------------------
  131. static void Execute(WebPWorker* const worker); // Forward declaration.
  132. static THREADFN ThreadLoop(void* ptr) {
  133. WebPWorker* const worker = (WebPWorker*)ptr;
  134. int done = 0;
  135. while (!done) {
  136. pthread_mutex_lock(&worker->impl_->mutex_);
  137. while (worker->status_ == OK) { // wait in idling mode
  138. pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
  139. }
  140. if (worker->status_ == WORK) {
  141. Execute(worker);
  142. worker->status_ = OK;
  143. } else if (worker->status_ == NOT_OK) { // finish the worker
  144. done = 1;
  145. }
  146. // signal to the main thread that we're done (for Sync())
  147. pthread_cond_signal(&worker->impl_->condition_);
  148. pthread_mutex_unlock(&worker->impl_->mutex_);
  149. }
  150. return THREAD_RETURN(NULL); // Thread is finished
  151. }
  152. // main thread state control
  153. static void ChangeState(WebPWorker* const worker,
  154. WebPWorkerStatus new_status) {
  155. // No-op when attempting to change state on a thread that didn't come up.
  156. // Checking status_ without acquiring the lock first would result in a data
  157. // race.
  158. if (worker->impl_ == NULL) return;
  159. pthread_mutex_lock(&worker->impl_->mutex_);
  160. if (worker->status_ >= OK) {
  161. // wait for the worker to finish
  162. while (worker->status_ != OK) {
  163. pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
  164. }
  165. // assign new status and release the working thread if needed
  166. if (new_status != OK) {
  167. worker->status_ = new_status;
  168. pthread_cond_signal(&worker->impl_->condition_);
  169. }
  170. }
  171. pthread_mutex_unlock(&worker->impl_->mutex_);
  172. }
  173. #endif // WEBP_USE_THREAD
  174. //------------------------------------------------------------------------------
  175. static void Init(WebPWorker* const worker) {
  176. memset(worker, 0, sizeof(*worker));
  177. worker->status_ = NOT_OK;
  178. }
  179. static int Sync(WebPWorker* const worker) {
  180. #ifdef WEBP_USE_THREAD
  181. ChangeState(worker, OK);
  182. #endif
  183. assert(worker->status_ <= OK);
  184. return !worker->had_error;
  185. }
  186. static int Reset(WebPWorker* const worker) {
  187. int ok = 1;
  188. worker->had_error = 0;
  189. if (worker->status_ < OK) {
  190. #ifdef WEBP_USE_THREAD
  191. worker->impl_ = (WebPWorkerImpl*)WebPSafeCalloc(1, sizeof(*worker->impl_));
  192. if (worker->impl_ == NULL) {
  193. return 0;
  194. }
  195. if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) {
  196. goto Error;
  197. }
  198. if (pthread_cond_init(&worker->impl_->condition_, NULL)) {
  199. pthread_mutex_destroy(&worker->impl_->mutex_);
  200. goto Error;
  201. }
  202. pthread_mutex_lock(&worker->impl_->mutex_);
  203. ok = !pthread_create(&worker->impl_->thread_, NULL, ThreadLoop, worker);
  204. if (ok) worker->status_ = OK;
  205. pthread_mutex_unlock(&worker->impl_->mutex_);
  206. if (!ok) {
  207. pthread_mutex_destroy(&worker->impl_->mutex_);
  208. pthread_cond_destroy(&worker->impl_->condition_);
  209. Error:
  210. WebPSafeFree(worker->impl_);
  211. worker->impl_ = NULL;
  212. return 0;
  213. }
  214. #else
  215. worker->status_ = OK;
  216. #endif
  217. } else if (worker->status_ > OK) {
  218. ok = Sync(worker);
  219. }
  220. assert(!ok || (worker->status_ == OK));
  221. return ok;
  222. }
  223. static void Execute(WebPWorker* const worker) {
  224. if (worker->hook != NULL) {
  225. worker->had_error |= !worker->hook(worker->data1, worker->data2);
  226. }
  227. }
  228. static void Launch(WebPWorker* const worker) {
  229. #ifdef WEBP_USE_THREAD
  230. ChangeState(worker, WORK);
  231. #else
  232. Execute(worker);
  233. #endif
  234. }
  235. static void End(WebPWorker* const worker) {
  236. #ifdef WEBP_USE_THREAD
  237. if (worker->impl_ != NULL) {
  238. ChangeState(worker, NOT_OK);
  239. pthread_join(worker->impl_->thread_, NULL);
  240. pthread_mutex_destroy(&worker->impl_->mutex_);
  241. pthread_cond_destroy(&worker->impl_->condition_);
  242. WebPSafeFree(worker->impl_);
  243. worker->impl_ = NULL;
  244. }
  245. #else
  246. worker->status_ = NOT_OK;
  247. assert(worker->impl_ == NULL);
  248. #endif
  249. assert(worker->status_ == NOT_OK);
  250. }
  251. //------------------------------------------------------------------------------
  252. static WebPWorkerInterface g_worker_interface = {
  253. Init, Reset, Sync, Launch, Execute, End
  254. };
  255. int WebPSetWorkerInterface(const WebPWorkerInterface* const winterface) {
  256. if (winterface == NULL ||
  257. winterface->Init == NULL || winterface->Reset == NULL ||
  258. winterface->Sync == NULL || winterface->Launch == NULL ||
  259. winterface->Execute == NULL || winterface->End == NULL) {
  260. return 0;
  261. }
  262. g_worker_interface = *winterface;
  263. return 1;
  264. }
  265. const WebPWorkerInterface* WebPGetWorkerInterface(void) {
  266. return &g_worker_interface;
  267. }
  268. //------------------------------------------------------------------------------