Home | History | Annotate | Download | only in utils

Lines Matching refs:worker

10 // Multi-threaded worker
137 WebPWorker* const worker = (WebPWorker*)ptr;
140 pthread_mutex_lock(&worker->mutex_);
141 while (worker->status_ == OK) { // wait in idling mode
142 pthread_cond_wait(&worker->condition_, &worker->mutex_);
144 if (worker->status_ == WORK) {
145 if (worker->hook) {
146 worker->had_error |= !worker->hook(worker->data1, worker->data2);
148 worker->status_ = OK;
149 } else if (worker->status_ == NOT_OK) { // finish the worker
153 pthread_cond_signal(&worker->condition_);
154 pthread_mutex_unlock(&worker->mutex_);
160 static void WebPWorkerChangeState(WebPWorker* const worker,
163 if (worker->status_ < OK) return;
165 pthread_mutex_lock(&worker->mutex_);
166 // wait for the worker to finish
167 while (worker->status_ != OK) {
168 pthread_cond_wait(&worker->condition_, &worker->mutex_);
172 worker->status_ = new_status;
173 pthread_cond_signal(&worker->condition_);
175 pthread_mutex_unlock(&worker->mutex_);
182 void WebPWorkerInit(WebPWorker* const worker) {
183 memset(worker, 0, sizeof(*worker));
184 worker->status_ = NOT_OK;
187 int WebPWorkerSync(WebPWorker* const worker) {
189 WebPWorkerChangeState(worker, OK);
191 assert(worker->status_ <= OK);
192 return !worker->had_error;
195 int WebPWorkerReset(WebPWorker* const worker) {
197 worker->had_error = 0;
198 if (worker->status_ < OK) {
200 if (pthread_mutex_init(&worker->mutex_, NULL) ||
201 pthread_cond_init(&worker->condition_, NULL)) {
204 pthread_mutex_lock(&worker->mutex_);
205 ok = !pthread_create(&worker->thread_, NULL, WebPWorkerThreadLoop, worker);
206 if (ok) worker->status_ = OK;
207 pthread_mutex_unlock(&worker->mutex_);
209 worker->status_ = OK;
211 } else if (worker->status_ > OK) {
212 ok = WebPWorkerSync(worker);
214 assert(!ok || (worker->status_ == OK));
218 void WebPWorkerLaunch(WebPWorker* const worker) {
220 WebPWorkerChangeState(worker, WORK);
222 if (worker->hook)
223 worker->had_error |= !worker->hook(worker->data1, worker->data2);
227 void WebPWorkerEnd(WebPWorker* const worker) {
228 if (worker->status_ >= OK) {
230 WebPWorkerChangeState(worker, NOT_OK);
231 pthread_join(worker->thread_, NULL);
232 pthread_mutex_destroy(&worker->mutex_);
233 pthread_cond_destroy(&worker->condition_);
235 worker->status_ = NOT_OK;
238 assert(worker->status_ == NOT_OK);