1 // Copyright 2013 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Multi-threaded worker
13 // http://git.chromium.org/webm/libwebp.git
14 // 100644 blob eff8f2a8c20095aade3c292b0e9292dac6cb3587 src/utils/thread.c
18 #include <string.h> // for memset()
19 #include "./vp9_thread.h"
21 #if defined(__cplusplus) || defined(c_plusplus)
25 #if CONFIG_MULTITHREAD
27 //------------------------------------------------------------------------------
29 static THREADFN thread_loop(void *ptr) { // thread loop
30 VP9Worker* const worker = (VP9Worker*)ptr;
33 pthread_mutex_lock(&worker->mutex_);
34 while (worker->status_ == OK) { // wait in idling mode
35 pthread_cond_wait(&worker->condition_, &worker->mutex_);
37 if (worker->status_ == WORK) {
38 vp9_worker_execute(worker);
40 } else if (worker->status_ == NOT_OK) { // finish the worker
43 // signal to the main thread that we're done (for Sync())
44 pthread_cond_signal(&worker->condition_);
45 pthread_mutex_unlock(&worker->mutex_);
47 return THREAD_RETURN(NULL); // Thread is finished
50 // main thread state control
51 static void change_state(VP9Worker* const worker,
52 VP9WorkerStatus new_status) {
53 // no-op when attempting to change state on a thread that didn't come up
54 if (worker->status_ < OK) return;
56 pthread_mutex_lock(&worker->mutex_);
57 // wait for the worker to finish
58 while (worker->status_ != OK) {
59 pthread_cond_wait(&worker->condition_, &worker->mutex_);
61 // assign new status and release the working thread if needed
62 if (new_status != OK) {
63 worker->status_ = new_status;
64 pthread_cond_signal(&worker->condition_);
66 pthread_mutex_unlock(&worker->mutex_);
69 #endif // CONFIG_MULTITHREAD
71 //------------------------------------------------------------------------------
73 void vp9_worker_init(VP9Worker* const worker) {
74 memset(worker, 0, sizeof(*worker));
75 worker->status_ = NOT_OK;
78 int vp9_worker_sync(VP9Worker* const worker) {
79 #if CONFIG_MULTITHREAD
80 change_state(worker, OK);
82 assert(worker->status_ <= OK);
83 return !worker->had_error;
86 int vp9_worker_reset(VP9Worker* const worker) {
88 worker->had_error = 0;
89 if (worker->status_ < OK) {
90 #if CONFIG_MULTITHREAD
91 if (pthread_mutex_init(&worker->mutex_, NULL) ||
92 pthread_cond_init(&worker->condition_, NULL)) {
95 pthread_mutex_lock(&worker->mutex_);
96 ok = !pthread_create(&worker->thread_, NULL, thread_loop, worker);
97 if (ok) worker->status_ = OK;
98 pthread_mutex_unlock(&worker->mutex_);
100 worker->status_ = OK;
102 } else if (worker->status_ > OK) {
103 ok = vp9_worker_sync(worker);
105 assert(!ok || (worker->status_ == OK));
109 void vp9_worker_execute(VP9Worker* const worker) {
110 if (worker->hook != NULL) {
111 worker->had_error |= !worker->hook(worker->data1, worker->data2);
115 void vp9_worker_launch(VP9Worker* const worker) {
116 #if CONFIG_MULTITHREAD
117 change_state(worker, WORK);
119 vp9_worker_execute(worker);
123 void vp9_worker_end(VP9Worker* const worker) {
124 if (worker->status_ >= OK) {
125 #if CONFIG_MULTITHREAD
126 change_state(worker, NOT_OK);
127 pthread_join(worker->thread_, NULL);
128 pthread_mutex_destroy(&worker->mutex_);
129 pthread_cond_destroy(&worker->condition_);
131 worker->status_ = NOT_OK;
134 assert(worker->status_ == NOT_OK);
137 //------------------------------------------------------------------------------
139 #if defined(__cplusplus) || defined(c_plusplus)