xref: /aosp_15_r20/external/libaom/aom_util/aom_thread.h (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 #ifndef AOM_AOM_UTIL_AOM_THREAD_H_
18*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_UTIL_AOM_THREAD_H_
19*77c1e3ccSAndroid Build Coastguard Worker 
20*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
21*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
22*77c1e3ccSAndroid Build Coastguard Worker #endif
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker // State of the worker thread object
25*77c1e3ccSAndroid Build Coastguard Worker typedef enum {
26*77c1e3ccSAndroid Build Coastguard Worker   AVX_WORKER_STATUS_NOT_OK = 0,  // object is unusable
27*77c1e3ccSAndroid Build Coastguard Worker   AVX_WORKER_STATUS_OK,          // ready to work
28*77c1e3ccSAndroid Build Coastguard Worker   AVX_WORKER_STATUS_WORKING      // busy finishing the current task
29*77c1e3ccSAndroid Build Coastguard Worker } AVxWorkerStatus;
30*77c1e3ccSAndroid Build Coastguard Worker 
31*77c1e3ccSAndroid Build Coastguard Worker // Function to be called by the worker thread. Takes two opaque pointers as
32*77c1e3ccSAndroid Build Coastguard Worker // arguments (data1 and data2). Should return true on success and return false
33*77c1e3ccSAndroid Build Coastguard Worker // in case of error.
34*77c1e3ccSAndroid Build Coastguard Worker typedef int (*AVxWorkerHook)(void *, void *);
35*77c1e3ccSAndroid Build Coastguard Worker 
36*77c1e3ccSAndroid Build Coastguard Worker // Platform-dependent implementation details for the worker.
37*77c1e3ccSAndroid Build Coastguard Worker typedef struct AVxWorkerImpl AVxWorkerImpl;
38*77c1e3ccSAndroid Build Coastguard Worker 
39*77c1e3ccSAndroid Build Coastguard Worker // Synchronization object used to launch job in the worker thread
40*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
41*77c1e3ccSAndroid Build Coastguard Worker   AVxWorkerImpl *impl_;
42*77c1e3ccSAndroid Build Coastguard Worker   AVxWorkerStatus status_;
43*77c1e3ccSAndroid Build Coastguard Worker   // Thread name for the debugger. If not NULL, must point to a string that
44*77c1e3ccSAndroid Build Coastguard Worker   // outlives the worker thread. For portability, use a name <= 15 characters
45*77c1e3ccSAndroid Build Coastguard Worker   // long (not including the terminating NUL character).
46*77c1e3ccSAndroid Build Coastguard Worker   const char *thread_name;
47*77c1e3ccSAndroid Build Coastguard Worker   AVxWorkerHook hook;  // hook to call
48*77c1e3ccSAndroid Build Coastguard Worker   void *data1;         // first argument passed to 'hook'
49*77c1e3ccSAndroid Build Coastguard Worker   void *data2;         // second argument passed to 'hook'
50*77c1e3ccSAndroid Build Coastguard Worker   int had_error;       // true if a call to 'hook' returned false
51*77c1e3ccSAndroid Build Coastguard Worker } AVxWorker;
52*77c1e3ccSAndroid Build Coastguard Worker 
53*77c1e3ccSAndroid Build Coastguard Worker // The interface for all thread-worker related functions. All these functions
54*77c1e3ccSAndroid Build Coastguard Worker // must be implemented.
55*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
56*77c1e3ccSAndroid Build Coastguard Worker   // Must be called first, before any other method.
57*77c1e3ccSAndroid Build Coastguard Worker   void (*init)(AVxWorker *const worker);
58*77c1e3ccSAndroid Build Coastguard Worker   // Must be called to initialize the object and spawn the thread. Re-entrant.
59*77c1e3ccSAndroid Build Coastguard Worker   // Will potentially launch the thread. Returns false in case of error.
60*77c1e3ccSAndroid Build Coastguard Worker   int (*reset)(AVxWorker *const worker);
61*77c1e3ccSAndroid Build Coastguard Worker   // Makes sure the previous work is finished. Returns true if worker->had_error
62*77c1e3ccSAndroid Build Coastguard Worker   // was not set and no error condition was triggered by the working thread.
63*77c1e3ccSAndroid Build Coastguard Worker   int (*sync)(AVxWorker *const worker);
64*77c1e3ccSAndroid Build Coastguard Worker   // Triggers the thread to call hook() with data1 and data2 arguments. These
65*77c1e3ccSAndroid Build Coastguard Worker   // hook/data1/data2 values can be changed at any time before calling this
66*77c1e3ccSAndroid Build Coastguard Worker   // function, but not be changed afterward until the next call to Sync().
67*77c1e3ccSAndroid Build Coastguard Worker   void (*launch)(AVxWorker *const worker);
68*77c1e3ccSAndroid Build Coastguard Worker   // This function is similar to launch() except that it calls the
69*77c1e3ccSAndroid Build Coastguard Worker   // hook directly instead of using a thread. Convenient to bypass the thread
70*77c1e3ccSAndroid Build Coastguard Worker   // mechanism while still using the AVxWorker structs. sync() must
71*77c1e3ccSAndroid Build Coastguard Worker   // still be called afterward (for error reporting).
72*77c1e3ccSAndroid Build Coastguard Worker   void (*execute)(AVxWorker *const worker);
73*77c1e3ccSAndroid Build Coastguard Worker   // Kill the thread and terminate the object. To use the object again, one
74*77c1e3ccSAndroid Build Coastguard Worker   // must call reset() again.
75*77c1e3ccSAndroid Build Coastguard Worker   void (*end)(AVxWorker *const worker);
76*77c1e3ccSAndroid Build Coastguard Worker } AVxWorkerInterface;
77*77c1e3ccSAndroid Build Coastguard Worker 
78*77c1e3ccSAndroid Build Coastguard Worker // Install a new set of threading functions, overriding the defaults. This
79*77c1e3ccSAndroid Build Coastguard Worker // should be done before any workers are started, i.e., before any encoding or
80*77c1e3ccSAndroid Build Coastguard Worker // decoding takes place. The contents of the interface struct are copied, it
81*77c1e3ccSAndroid Build Coastguard Worker // is safe to free the corresponding memory after this call. This function is
82*77c1e3ccSAndroid Build Coastguard Worker // not thread-safe. Return false in case of invalid pointer or methods.
83*77c1e3ccSAndroid Build Coastguard Worker int aom_set_worker_interface(const AVxWorkerInterface *const winterface);
84*77c1e3ccSAndroid Build Coastguard Worker 
85*77c1e3ccSAndroid Build Coastguard Worker // Retrieve the currently set thread worker interface.
86*77c1e3ccSAndroid Build Coastguard Worker const AVxWorkerInterface *aom_get_worker_interface(void);
87*77c1e3ccSAndroid Build Coastguard Worker 
88*77c1e3ccSAndroid Build Coastguard Worker //------------------------------------------------------------------------------
89*77c1e3ccSAndroid Build Coastguard Worker 
90*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
91*77c1e3ccSAndroid Build Coastguard Worker }  // extern "C"
92*77c1e3ccSAndroid Build Coastguard Worker #endif
93*77c1e3ccSAndroid Build Coastguard Worker 
94*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_UTIL_AOM_THREAD_H_
95