xref: /aosp_15_r20/external/libaom/aom_util/aom_thread.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker //
12*77c1e3ccSAndroid Build Coastguard Worker // Multi-threaded worker
13*77c1e3ccSAndroid Build Coastguard Worker //
14*77c1e3ccSAndroid Build Coastguard Worker // Original source:
15*77c1e3ccSAndroid Build Coastguard Worker //  https://chromium.googlesource.com/webm/libwebp
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker // Enable GNU extensions in glibc so that we can call pthread_setname_np().
18*77c1e3ccSAndroid Build Coastguard Worker // This must be before any #include statements.
19*77c1e3ccSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
20*77c1e3ccSAndroid Build Coastguard Worker #define _GNU_SOURCE
21*77c1e3ccSAndroid Build Coastguard Worker #endif
22*77c1e3ccSAndroid Build Coastguard Worker 
23*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
24*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>  // for memset()
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
27*77c1e3ccSAndroid Build Coastguard Worker 
28*77c1e3ccSAndroid Build Coastguard Worker #include "aom_mem/aom_mem.h"
29*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/sanitizer.h"
30*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/aom_pthread.h"
31*77c1e3ccSAndroid Build Coastguard Worker #include "aom_util/aom_thread.h"
32*77c1e3ccSAndroid Build Coastguard Worker 
33*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
34*77c1e3ccSAndroid Build Coastguard Worker 
35*77c1e3ccSAndroid Build Coastguard Worker struct AVxWorkerImpl {
36*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_t mutex_;
37*77c1e3ccSAndroid Build Coastguard Worker   pthread_cond_t condition_;
38*77c1e3ccSAndroid Build Coastguard Worker   pthread_t thread_;
39*77c1e3ccSAndroid Build Coastguard Worker };
40*77c1e3ccSAndroid Build Coastguard Worker 
41*77c1e3ccSAndroid Build Coastguard Worker //------------------------------------------------------------------------------
42*77c1e3ccSAndroid Build Coastguard Worker 
43*77c1e3ccSAndroid Build Coastguard Worker static void execute(AVxWorker *const worker);  // Forward declaration.
44*77c1e3ccSAndroid Build Coastguard Worker 
thread_loop(void * ptr)45*77c1e3ccSAndroid Build Coastguard Worker static THREADFN thread_loop(void *ptr) {
46*77c1e3ccSAndroid Build Coastguard Worker   AVxWorker *const worker = (AVxWorker *)ptr;
47*77c1e3ccSAndroid Build Coastguard Worker #ifdef __APPLE__
48*77c1e3ccSAndroid Build Coastguard Worker   if (worker->thread_name != NULL) {
49*77c1e3ccSAndroid Build Coastguard Worker     // Apple's version of pthread_setname_np takes one argument and operates on
50*77c1e3ccSAndroid Build Coastguard Worker     // the current thread only. The maximum size of the thread_name buffer was
51*77c1e3ccSAndroid Build Coastguard Worker     // noted in the Chromium source code and was confirmed by experiments. If
52*77c1e3ccSAndroid Build Coastguard Worker     // thread_name is too long, pthread_setname_np returns -1 with errno
53*77c1e3ccSAndroid Build Coastguard Worker     // ENAMETOOLONG (63).
54*77c1e3ccSAndroid Build Coastguard Worker     char thread_name[64];
55*77c1e3ccSAndroid Build Coastguard Worker     strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
56*77c1e3ccSAndroid Build Coastguard Worker     thread_name[sizeof(thread_name) - 1] = '\0';
57*77c1e3ccSAndroid Build Coastguard Worker     pthread_setname_np(thread_name);
58*77c1e3ccSAndroid Build Coastguard Worker   }
59*77c1e3ccSAndroid Build Coastguard Worker #elif (defined(__GLIBC__) && !defined(__GNU__)) || defined(__BIONIC__)
60*77c1e3ccSAndroid Build Coastguard Worker   if (worker->thread_name != NULL) {
61*77c1e3ccSAndroid Build Coastguard Worker     // Linux and Android require names (with nul) fit in 16 chars, otherwise
62*77c1e3ccSAndroid Build Coastguard Worker     // pthread_setname_np() returns ERANGE (34).
63*77c1e3ccSAndroid Build Coastguard Worker     char thread_name[16];
64*77c1e3ccSAndroid Build Coastguard Worker     strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
65*77c1e3ccSAndroid Build Coastguard Worker     thread_name[sizeof(thread_name) - 1] = '\0';
66*77c1e3ccSAndroid Build Coastguard Worker     pthread_setname_np(pthread_self(), thread_name);
67*77c1e3ccSAndroid Build Coastguard Worker   }
68*77c1e3ccSAndroid Build Coastguard Worker #endif
69*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_lock(&worker->impl_->mutex_);
70*77c1e3ccSAndroid Build Coastguard Worker   for (;;) {
71*77c1e3ccSAndroid Build Coastguard Worker     while (worker->status_ == AVX_WORKER_STATUS_OK) {  // wait in idling mode
72*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
73*77c1e3ccSAndroid Build Coastguard Worker     }
74*77c1e3ccSAndroid Build Coastguard Worker     if (worker->status_ == AVX_WORKER_STATUS_WORKING) {
75*77c1e3ccSAndroid Build Coastguard Worker       // When worker->status_ is AVX_WORKER_STATUS_WORKING, the main thread
76*77c1e3ccSAndroid Build Coastguard Worker       // doesn't change worker->status_ and will wait until the worker changes
77*77c1e3ccSAndroid Build Coastguard Worker       // worker->status_ to AVX_WORKER_STATUS_OK. See change_state(). So the
78*77c1e3ccSAndroid Build Coastguard Worker       // worker can safely call execute() without holding worker->impl_->mutex_.
79*77c1e3ccSAndroid Build Coastguard Worker       // When the worker reacquires worker->impl_->mutex_, worker->status_ must
80*77c1e3ccSAndroid Build Coastguard Worker       // still be AVX_WORKER_STATUS_WORKING.
81*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_unlock(&worker->impl_->mutex_);
82*77c1e3ccSAndroid Build Coastguard Worker       execute(worker);
83*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_lock(&worker->impl_->mutex_);
84*77c1e3ccSAndroid Build Coastguard Worker       assert(worker->status_ == AVX_WORKER_STATUS_WORKING);
85*77c1e3ccSAndroid Build Coastguard Worker       worker->status_ = AVX_WORKER_STATUS_OK;
86*77c1e3ccSAndroid Build Coastguard Worker       // signal to the main thread that we're done (for sync())
87*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_signal(&worker->impl_->condition_);
88*77c1e3ccSAndroid Build Coastguard Worker     } else {
89*77c1e3ccSAndroid Build Coastguard Worker       assert(worker->status_ == AVX_WORKER_STATUS_NOT_OK);  // finish the worker
90*77c1e3ccSAndroid Build Coastguard Worker       break;
91*77c1e3ccSAndroid Build Coastguard Worker     }
92*77c1e3ccSAndroid Build Coastguard Worker   }
93*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_unlock(&worker->impl_->mutex_);
94*77c1e3ccSAndroid Build Coastguard Worker   return THREAD_EXIT_SUCCESS;  // Thread is finished
95*77c1e3ccSAndroid Build Coastguard Worker }
96*77c1e3ccSAndroid Build Coastguard Worker 
97*77c1e3ccSAndroid Build Coastguard Worker // main thread state control
change_state(AVxWorker * const worker,AVxWorkerStatus new_status)98*77c1e3ccSAndroid Build Coastguard Worker static void change_state(AVxWorker *const worker, AVxWorkerStatus new_status) {
99*77c1e3ccSAndroid Build Coastguard Worker   // No-op when attempting to change state on a thread that didn't come up.
100*77c1e3ccSAndroid Build Coastguard Worker   // Checking status_ without acquiring the lock first would result in a data
101*77c1e3ccSAndroid Build Coastguard Worker   // race.
102*77c1e3ccSAndroid Build Coastguard Worker   if (worker->impl_ == NULL) return;
103*77c1e3ccSAndroid Build Coastguard Worker 
104*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_lock(&worker->impl_->mutex_);
105*77c1e3ccSAndroid Build Coastguard Worker   if (worker->status_ >= AVX_WORKER_STATUS_OK) {
106*77c1e3ccSAndroid Build Coastguard Worker     // wait for the worker to finish
107*77c1e3ccSAndroid Build Coastguard Worker     while (worker->status_ != AVX_WORKER_STATUS_OK) {
108*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
109*77c1e3ccSAndroid Build Coastguard Worker     }
110*77c1e3ccSAndroid Build Coastguard Worker     // assign new status and release the working thread if needed
111*77c1e3ccSAndroid Build Coastguard Worker     if (new_status != AVX_WORKER_STATUS_OK) {
112*77c1e3ccSAndroid Build Coastguard Worker       worker->status_ = new_status;
113*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_signal(&worker->impl_->condition_);
114*77c1e3ccSAndroid Build Coastguard Worker     }
115*77c1e3ccSAndroid Build Coastguard Worker   }
116*77c1e3ccSAndroid Build Coastguard Worker   pthread_mutex_unlock(&worker->impl_->mutex_);
117*77c1e3ccSAndroid Build Coastguard Worker }
118*77c1e3ccSAndroid Build Coastguard Worker 
119*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_MULTITHREAD
120*77c1e3ccSAndroid Build Coastguard Worker 
121*77c1e3ccSAndroid Build Coastguard Worker //------------------------------------------------------------------------------
122*77c1e3ccSAndroid Build Coastguard Worker 
init(AVxWorker * const worker)123*77c1e3ccSAndroid Build Coastguard Worker static void init(AVxWorker *const worker) {
124*77c1e3ccSAndroid Build Coastguard Worker   memset(worker, 0, sizeof(*worker));
125*77c1e3ccSAndroid Build Coastguard Worker   worker->status_ = AVX_WORKER_STATUS_NOT_OK;
126*77c1e3ccSAndroid Build Coastguard Worker }
127*77c1e3ccSAndroid Build Coastguard Worker 
sync(AVxWorker * const worker)128*77c1e3ccSAndroid Build Coastguard Worker static int sync(AVxWorker *const worker) {
129*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
130*77c1e3ccSAndroid Build Coastguard Worker   change_state(worker, AVX_WORKER_STATUS_OK);
131*77c1e3ccSAndroid Build Coastguard Worker #endif
132*77c1e3ccSAndroid Build Coastguard Worker   assert(worker->status_ <= AVX_WORKER_STATUS_OK);
133*77c1e3ccSAndroid Build Coastguard Worker   return !worker->had_error;
134*77c1e3ccSAndroid Build Coastguard Worker }
135*77c1e3ccSAndroid Build Coastguard Worker 
reset(AVxWorker * const worker)136*77c1e3ccSAndroid Build Coastguard Worker static int reset(AVxWorker *const worker) {
137*77c1e3ccSAndroid Build Coastguard Worker   int ok = 1;
138*77c1e3ccSAndroid Build Coastguard Worker   worker->had_error = 0;
139*77c1e3ccSAndroid Build Coastguard Worker   if (worker->status_ < AVX_WORKER_STATUS_OK) {
140*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
141*77c1e3ccSAndroid Build Coastguard Worker     worker->impl_ = (AVxWorkerImpl *)aom_calloc(1, sizeof(*worker->impl_));
142*77c1e3ccSAndroid Build Coastguard Worker     if (worker->impl_ == NULL) {
143*77c1e3ccSAndroid Build Coastguard Worker       return 0;
144*77c1e3ccSAndroid Build Coastguard Worker     }
145*77c1e3ccSAndroid Build Coastguard Worker     if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) {
146*77c1e3ccSAndroid Build Coastguard Worker       goto Error;
147*77c1e3ccSAndroid Build Coastguard Worker     }
148*77c1e3ccSAndroid Build Coastguard Worker     if (pthread_cond_init(&worker->impl_->condition_, NULL)) {
149*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_destroy(&worker->impl_->mutex_);
150*77c1e3ccSAndroid Build Coastguard Worker       goto Error;
151*77c1e3ccSAndroid Build Coastguard Worker     }
152*77c1e3ccSAndroid Build Coastguard Worker     pthread_attr_t attr;
153*77c1e3ccSAndroid Build Coastguard Worker     if (pthread_attr_init(&attr)) goto Error2;
154*77c1e3ccSAndroid Build Coastguard Worker       // Debug ASan builds require at least ~1MiB of stack; prevents
155*77c1e3ccSAndroid Build Coastguard Worker       // failures on macOS arm64 where the default is 512KiB.
156*77c1e3ccSAndroid Build Coastguard Worker       // See: https://crbug.com/aomedia/3379
157*77c1e3ccSAndroid Build Coastguard Worker #if defined(AOM_ADDRESS_SANITIZER) && defined(__APPLE__) && AOM_ARCH_ARM && \
158*77c1e3ccSAndroid Build Coastguard Worker     !defined(NDEBUG)
159*77c1e3ccSAndroid Build Coastguard Worker     const size_t kMinStackSize = 1024 * 1024;
160*77c1e3ccSAndroid Build Coastguard Worker #else
161*77c1e3ccSAndroid Build Coastguard Worker     const size_t kMinStackSize = 256 * 1024;
162*77c1e3ccSAndroid Build Coastguard Worker #endif
163*77c1e3ccSAndroid Build Coastguard Worker     size_t stacksize;
164*77c1e3ccSAndroid Build Coastguard Worker     if (!pthread_attr_getstacksize(&attr, &stacksize)) {
165*77c1e3ccSAndroid Build Coastguard Worker       if (stacksize < kMinStackSize &&
166*77c1e3ccSAndroid Build Coastguard Worker           pthread_attr_setstacksize(&attr, kMinStackSize)) {
167*77c1e3ccSAndroid Build Coastguard Worker         pthread_attr_destroy(&attr);
168*77c1e3ccSAndroid Build Coastguard Worker         goto Error2;
169*77c1e3ccSAndroid Build Coastguard Worker       }
170*77c1e3ccSAndroid Build Coastguard Worker     }
171*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_lock(&worker->impl_->mutex_);
172*77c1e3ccSAndroid Build Coastguard Worker     ok = !pthread_create(&worker->impl_->thread_, &attr, thread_loop, worker);
173*77c1e3ccSAndroid Build Coastguard Worker     if (ok) worker->status_ = AVX_WORKER_STATUS_OK;
174*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_unlock(&worker->impl_->mutex_);
175*77c1e3ccSAndroid Build Coastguard Worker     pthread_attr_destroy(&attr);
176*77c1e3ccSAndroid Build Coastguard Worker     if (!ok) {
177*77c1e3ccSAndroid Build Coastguard Worker     Error2:
178*77c1e3ccSAndroid Build Coastguard Worker       pthread_mutex_destroy(&worker->impl_->mutex_);
179*77c1e3ccSAndroid Build Coastguard Worker       pthread_cond_destroy(&worker->impl_->condition_);
180*77c1e3ccSAndroid Build Coastguard Worker     Error:
181*77c1e3ccSAndroid Build Coastguard Worker       aom_free(worker->impl_);
182*77c1e3ccSAndroid Build Coastguard Worker       worker->impl_ = NULL;
183*77c1e3ccSAndroid Build Coastguard Worker       return 0;
184*77c1e3ccSAndroid Build Coastguard Worker     }
185*77c1e3ccSAndroid Build Coastguard Worker #else
186*77c1e3ccSAndroid Build Coastguard Worker     worker->status_ = AVX_WORKER_STATUS_OK;
187*77c1e3ccSAndroid Build Coastguard Worker #endif
188*77c1e3ccSAndroid Build Coastguard Worker   } else if (worker->status_ > AVX_WORKER_STATUS_OK) {
189*77c1e3ccSAndroid Build Coastguard Worker     ok = sync(worker);
190*77c1e3ccSAndroid Build Coastguard Worker   }
191*77c1e3ccSAndroid Build Coastguard Worker   assert(!ok || (worker->status_ == AVX_WORKER_STATUS_OK));
192*77c1e3ccSAndroid Build Coastguard Worker   return ok;
193*77c1e3ccSAndroid Build Coastguard Worker }
194*77c1e3ccSAndroid Build Coastguard Worker 
execute(AVxWorker * const worker)195*77c1e3ccSAndroid Build Coastguard Worker static void execute(AVxWorker *const worker) {
196*77c1e3ccSAndroid Build Coastguard Worker   if (worker->hook != NULL) {
197*77c1e3ccSAndroid Build Coastguard Worker     worker->had_error |= !worker->hook(worker->data1, worker->data2);
198*77c1e3ccSAndroid Build Coastguard Worker   }
199*77c1e3ccSAndroid Build Coastguard Worker }
200*77c1e3ccSAndroid Build Coastguard Worker 
launch(AVxWorker * const worker)201*77c1e3ccSAndroid Build Coastguard Worker static void launch(AVxWorker *const worker) {
202*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
203*77c1e3ccSAndroid Build Coastguard Worker   change_state(worker, AVX_WORKER_STATUS_WORKING);
204*77c1e3ccSAndroid Build Coastguard Worker #else
205*77c1e3ccSAndroid Build Coastguard Worker   execute(worker);
206*77c1e3ccSAndroid Build Coastguard Worker #endif
207*77c1e3ccSAndroid Build Coastguard Worker }
208*77c1e3ccSAndroid Build Coastguard Worker 
end(AVxWorker * const worker)209*77c1e3ccSAndroid Build Coastguard Worker static void end(AVxWorker *const worker) {
210*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_MULTITHREAD
211*77c1e3ccSAndroid Build Coastguard Worker   if (worker->impl_ != NULL) {
212*77c1e3ccSAndroid Build Coastguard Worker     change_state(worker, AVX_WORKER_STATUS_NOT_OK);
213*77c1e3ccSAndroid Build Coastguard Worker     pthread_join(worker->impl_->thread_, NULL);
214*77c1e3ccSAndroid Build Coastguard Worker     pthread_mutex_destroy(&worker->impl_->mutex_);
215*77c1e3ccSAndroid Build Coastguard Worker     pthread_cond_destroy(&worker->impl_->condition_);
216*77c1e3ccSAndroid Build Coastguard Worker     aom_free(worker->impl_);
217*77c1e3ccSAndroid Build Coastguard Worker     worker->impl_ = NULL;
218*77c1e3ccSAndroid Build Coastguard Worker   }
219*77c1e3ccSAndroid Build Coastguard Worker #else
220*77c1e3ccSAndroid Build Coastguard Worker   worker->status_ = AVX_WORKER_STATUS_NOT_OK;
221*77c1e3ccSAndroid Build Coastguard Worker   assert(worker->impl_ == NULL);
222*77c1e3ccSAndroid Build Coastguard Worker #endif
223*77c1e3ccSAndroid Build Coastguard Worker   assert(worker->status_ == AVX_WORKER_STATUS_NOT_OK);
224*77c1e3ccSAndroid Build Coastguard Worker }
225*77c1e3ccSAndroid Build Coastguard Worker 
226*77c1e3ccSAndroid Build Coastguard Worker //------------------------------------------------------------------------------
227*77c1e3ccSAndroid Build Coastguard Worker 
228*77c1e3ccSAndroid Build Coastguard Worker static AVxWorkerInterface g_worker_interface = { init,   reset,   sync,
229*77c1e3ccSAndroid Build Coastguard Worker                                                  launch, execute, end };
230*77c1e3ccSAndroid Build Coastguard Worker 
aom_set_worker_interface(const AVxWorkerInterface * const winterface)231*77c1e3ccSAndroid Build Coastguard Worker int aom_set_worker_interface(const AVxWorkerInterface *const winterface) {
232*77c1e3ccSAndroid Build Coastguard Worker   if (winterface == NULL || winterface->init == NULL ||
233*77c1e3ccSAndroid Build Coastguard Worker       winterface->reset == NULL || winterface->sync == NULL ||
234*77c1e3ccSAndroid Build Coastguard Worker       winterface->launch == NULL || winterface->execute == NULL ||
235*77c1e3ccSAndroid Build Coastguard Worker       winterface->end == NULL) {
236*77c1e3ccSAndroid Build Coastguard Worker     return 0;
237*77c1e3ccSAndroid Build Coastguard Worker   }
238*77c1e3ccSAndroid Build Coastguard Worker   g_worker_interface = *winterface;
239*77c1e3ccSAndroid Build Coastguard Worker   return 1;
240*77c1e3ccSAndroid Build Coastguard Worker }
241*77c1e3ccSAndroid Build Coastguard Worker 
aom_get_worker_interface(void)242*77c1e3ccSAndroid Build Coastguard Worker const AVxWorkerInterface *aom_get_worker_interface(void) {
243*77c1e3ccSAndroid Build Coastguard Worker   return &g_worker_interface;
244*77c1e3ccSAndroid Build Coastguard Worker }
245*77c1e3ccSAndroid Build Coastguard Worker 
246*77c1e3ccSAndroid Build Coastguard Worker //------------------------------------------------------------------------------
247