Lines Matching full:worker

12 // Multi-threaded worker
43 static void execute(AVxWorker *const worker); // Forward declaration.
46 AVxWorker *const worker = (AVxWorker *)ptr; in thread_loop() local
48 if (worker->thread_name != NULL) { in thread_loop()
55 strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1); in thread_loop()
60 if (worker->thread_name != NULL) { in thread_loop()
64 strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1); in thread_loop()
69 pthread_mutex_lock(&worker->impl_->mutex_); in thread_loop()
71 while (worker->status_ == AVX_WORKER_STATUS_OK) { // wait in idling mode in thread_loop()
72 pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_); in thread_loop()
74 if (worker->status_ == AVX_WORKER_STATUS_WORKING) { in thread_loop()
75 // When worker->status_ is AVX_WORKER_STATUS_WORKING, the main thread in thread_loop()
76 // doesn't change worker->status_ and will wait until the worker changes in thread_loop()
77 // worker->status_ to AVX_WORKER_STATUS_OK. See change_state(). So the in thread_loop()
78 // worker can safely call execute() without holding worker->impl_->mutex_. in thread_loop()
79 // When the worker reacquires worker->impl_->mutex_, worker->status_ must in thread_loop()
81 pthread_mutex_unlock(&worker->impl_->mutex_); in thread_loop()
82 execute(worker); in thread_loop()
83 pthread_mutex_lock(&worker->impl_->mutex_); in thread_loop()
84 assert(worker->status_ == AVX_WORKER_STATUS_WORKING); in thread_loop()
85 worker->status_ = AVX_WORKER_STATUS_OK; in thread_loop()
87 pthread_cond_signal(&worker->impl_->condition_); in thread_loop()
89 assert(worker->status_ == AVX_WORKER_STATUS_NOT_OK); // finish the worker in thread_loop()
93 pthread_mutex_unlock(&worker->impl_->mutex_); in thread_loop()
98 static void change_state(AVxWorker *const worker, AVxWorkerStatus new_status) { in change_state() argument
102 if (worker->impl_ == NULL) return; in change_state()
104 pthread_mutex_lock(&worker->impl_->mutex_); in change_state()
105 if (worker->status_ >= AVX_WORKER_STATUS_OK) { in change_state()
106 // wait for the worker to finish in change_state()
107 while (worker->status_ != AVX_WORKER_STATUS_OK) { in change_state()
108 pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_); in change_state()
112 worker->status_ = new_status; in change_state()
113 pthread_cond_signal(&worker->impl_->condition_); in change_state()
116 pthread_mutex_unlock(&worker->impl_->mutex_); in change_state()
123 static void init(AVxWorker *const worker) { in init() argument
124 memset(worker, 0, sizeof(*worker)); in init()
125 worker->status_ = AVX_WORKER_STATUS_NOT_OK; in init()
128 static int sync(AVxWorker *const worker) { in sync() argument
130 change_state(worker, AVX_WORKER_STATUS_OK); in sync()
132 assert(worker->status_ <= AVX_WORKER_STATUS_OK); in sync()
133 return !worker->had_error; in sync()
136 static int reset(AVxWorker *const worker) { in reset() argument
138 worker->had_error = 0; in reset()
139 if (worker->status_ < AVX_WORKER_STATUS_OK) { in reset()
141 worker->impl_ = (AVxWorkerImpl *)aom_calloc(1, sizeof(*worker->impl_)); in reset()
142 if (worker->impl_ == NULL) { in reset()
145 if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) { in reset()
148 if (pthread_cond_init(&worker->impl_->condition_, NULL)) { in reset()
149 pthread_mutex_destroy(&worker->impl_->mutex_); in reset()
171 pthread_mutex_lock(&worker->impl_->mutex_); in reset()
172 ok = !pthread_create(&worker->impl_->thread_, &attr, thread_loop, worker); in reset()
173 if (ok) worker->status_ = AVX_WORKER_STATUS_OK; in reset()
174 pthread_mutex_unlock(&worker->impl_->mutex_); in reset()
178 pthread_mutex_destroy(&worker->impl_->mutex_); in reset()
179 pthread_cond_destroy(&worker->impl_->condition_); in reset()
181 aom_free(worker->impl_); in reset()
182 worker->impl_ = NULL; in reset()
186 worker->status_ = AVX_WORKER_STATUS_OK; in reset()
188 } else if (worker->status_ > AVX_WORKER_STATUS_OK) { in reset()
189 ok = sync(worker); in reset()
191 assert(!ok || (worker->status_ == AVX_WORKER_STATUS_OK)); in reset()
195 static void execute(AVxWorker *const worker) { in execute() argument
196 if (worker->hook != NULL) { in execute()
197 worker->had_error |= !worker->hook(worker->data1, worker->data2); in execute()
201 static void launch(AVxWorker *const worker) { in launch() argument
203 change_state(worker, AVX_WORKER_STATUS_WORKING); in launch()
205 execute(worker); in launch()
209 static void end(AVxWorker *const worker) { in end() argument
211 if (worker->impl_ != NULL) { in end()
212 change_state(worker, AVX_WORKER_STATUS_NOT_OK); in end()
213 pthread_join(worker->impl_->thread_, NULL); in end()
214 pthread_mutex_destroy(&worker->impl_->mutex_); in end()
215 pthread_cond_destroy(&worker->impl_->condition_); in end()
216 aom_free(worker->impl_); in end()
217 worker->impl_ = NULL; in end()
220 worker->status_ = AVX_WORKER_STATUS_NOT_OK; in end()
221 assert(worker->impl_ == NULL); in end()
223 assert(worker->status_ == AVX_WORKER_STATUS_NOT_OK); in end()