xref: /aosp_15_r20/external/libvpx/vpx_util/vpx_thread.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1*fb1b10abSAndroid Build Coastguard Worker // Copyright 2013 Google Inc. All Rights Reserved.
2*fb1b10abSAndroid Build Coastguard Worker //
3*fb1b10abSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license
4*fb1b10abSAndroid Build Coastguard Worker // that can be found in the COPYING file in the root of the source
5*fb1b10abSAndroid Build Coastguard Worker // tree. An additional intellectual property rights grant can be found
6*fb1b10abSAndroid Build Coastguard Worker // in the file PATENTS. All contributing project authors may
7*fb1b10abSAndroid Build Coastguard Worker // be found in the AUTHORS file in the root of the source tree.
8*fb1b10abSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
9*fb1b10abSAndroid Build Coastguard Worker //
10*fb1b10abSAndroid Build Coastguard Worker // Multi-threaded worker
11*fb1b10abSAndroid Build Coastguard Worker //
12*fb1b10abSAndroid Build Coastguard Worker // Original source:
13*fb1b10abSAndroid Build Coastguard Worker //  https://chromium.googlesource.com/webm/libwebp
14*fb1b10abSAndroid Build Coastguard Worker 
15*fb1b10abSAndroid Build Coastguard Worker #ifndef VPX_VPX_UTIL_VPX_THREAD_H_
16*fb1b10abSAndroid Build Coastguard Worker #define VPX_VPX_UTIL_VPX_THREAD_H_
17*fb1b10abSAndroid Build Coastguard Worker 
18*fb1b10abSAndroid Build Coastguard Worker #ifdef __cplusplus
19*fb1b10abSAndroid Build Coastguard Worker extern "C" {
20*fb1b10abSAndroid Build Coastguard Worker #endif
21*fb1b10abSAndroid Build Coastguard Worker 
22*fb1b10abSAndroid Build Coastguard Worker // State of the worker thread object
23*fb1b10abSAndroid Build Coastguard Worker typedef enum {
24*fb1b10abSAndroid Build Coastguard Worker   VPX_WORKER_STATUS_NOT_OK = 0,  // object is unusable
25*fb1b10abSAndroid Build Coastguard Worker   VPX_WORKER_STATUS_OK,          // ready to work
26*fb1b10abSAndroid Build Coastguard Worker   VPX_WORKER_STATUS_WORKING      // busy finishing the current task
27*fb1b10abSAndroid Build Coastguard Worker } VPxWorkerStatus;
28*fb1b10abSAndroid Build Coastguard Worker 
29*fb1b10abSAndroid Build Coastguard Worker // Function to be called by the worker thread. Takes two opaque pointers as
30*fb1b10abSAndroid Build Coastguard Worker // arguments (data1 and data2). Should return true on success and return false
31*fb1b10abSAndroid Build Coastguard Worker // in case of error.
32*fb1b10abSAndroid Build Coastguard Worker typedef int (*VPxWorkerHook)(void *, void *);
33*fb1b10abSAndroid Build Coastguard Worker 
34*fb1b10abSAndroid Build Coastguard Worker // Platform-dependent implementation details for the worker.
35*fb1b10abSAndroid Build Coastguard Worker typedef struct VPxWorkerImpl VPxWorkerImpl;
36*fb1b10abSAndroid Build Coastguard Worker 
37*fb1b10abSAndroid Build Coastguard Worker // Synchronization object used to launch job in the worker thread
38*fb1b10abSAndroid Build Coastguard Worker typedef struct {
39*fb1b10abSAndroid Build Coastguard Worker   VPxWorkerImpl *impl_;
40*fb1b10abSAndroid Build Coastguard Worker   VPxWorkerStatus status_;
41*fb1b10abSAndroid Build Coastguard Worker   // Thread name for the debugger. If not NULL, must point to a string that
42*fb1b10abSAndroid Build Coastguard Worker   // outlives the worker thread. For portability, use a name <= 15 characters
43*fb1b10abSAndroid Build Coastguard Worker   // long (not including the terminating NUL character).
44*fb1b10abSAndroid Build Coastguard Worker   const char *thread_name;
45*fb1b10abSAndroid Build Coastguard Worker   VPxWorkerHook hook;  // hook to call
46*fb1b10abSAndroid Build Coastguard Worker   void *data1;         // first argument passed to 'hook'
47*fb1b10abSAndroid Build Coastguard Worker   void *data2;         // second argument passed to 'hook'
48*fb1b10abSAndroid Build Coastguard Worker   int had_error;       // true if a call to 'hook' returned false
49*fb1b10abSAndroid Build Coastguard Worker } VPxWorker;
50*fb1b10abSAndroid Build Coastguard Worker 
51*fb1b10abSAndroid Build Coastguard Worker // The interface for all thread-worker related functions. All these functions
52*fb1b10abSAndroid Build Coastguard Worker // must be implemented.
53*fb1b10abSAndroid Build Coastguard Worker typedef struct {
54*fb1b10abSAndroid Build Coastguard Worker   // Must be called first, before any other method.
55*fb1b10abSAndroid Build Coastguard Worker   void (*init)(VPxWorker *const worker);
56*fb1b10abSAndroid Build Coastguard Worker   // Must be called to initialize the object and spawn the thread. Re-entrant.
57*fb1b10abSAndroid Build Coastguard Worker   // Will potentially launch the thread. Returns false in case of error.
58*fb1b10abSAndroid Build Coastguard Worker   int (*reset)(VPxWorker *const worker);
59*fb1b10abSAndroid Build Coastguard Worker   // Makes sure the previous work is finished. Returns true if worker->had_error
60*fb1b10abSAndroid Build Coastguard Worker   // was not set and no error condition was triggered by the working thread.
61*fb1b10abSAndroid Build Coastguard Worker   int (*sync)(VPxWorker *const worker);
62*fb1b10abSAndroid Build Coastguard Worker   // Triggers the thread to call hook() with data1 and data2 arguments. These
63*fb1b10abSAndroid Build Coastguard Worker   // hook/data1/data2 values can be changed at any time before calling this
64*fb1b10abSAndroid Build Coastguard Worker   // function, but not be changed afterward until the next call to Sync().
65*fb1b10abSAndroid Build Coastguard Worker   void (*launch)(VPxWorker *const worker);
66*fb1b10abSAndroid Build Coastguard Worker   // This function is similar to launch() except that it calls the
67*fb1b10abSAndroid Build Coastguard Worker   // hook directly instead of using a thread. Convenient to bypass the thread
68*fb1b10abSAndroid Build Coastguard Worker   // mechanism while still using the VPxWorker structs. sync() must
69*fb1b10abSAndroid Build Coastguard Worker   // still be called afterward (for error reporting).
70*fb1b10abSAndroid Build Coastguard Worker   void (*execute)(VPxWorker *const worker);
71*fb1b10abSAndroid Build Coastguard Worker   // Kill the thread and terminate the object. To use the object again, one
72*fb1b10abSAndroid Build Coastguard Worker   // must call reset() again.
73*fb1b10abSAndroid Build Coastguard Worker   void (*end)(VPxWorker *const worker);
74*fb1b10abSAndroid Build Coastguard Worker } VPxWorkerInterface;
75*fb1b10abSAndroid Build Coastguard Worker 
76*fb1b10abSAndroid Build Coastguard Worker // Install a new set of threading functions, overriding the defaults. This
77*fb1b10abSAndroid Build Coastguard Worker // should be done before any workers are started, i.e., before any encoding or
78*fb1b10abSAndroid Build Coastguard Worker // decoding takes place. The contents of the interface struct are copied, it
79*fb1b10abSAndroid Build Coastguard Worker // is safe to free the corresponding memory after this call. This function is
80*fb1b10abSAndroid Build Coastguard Worker // not thread-safe. Return false in case of invalid pointer or methods.
81*fb1b10abSAndroid Build Coastguard Worker int vpx_set_worker_interface(const VPxWorkerInterface *const winterface);
82*fb1b10abSAndroid Build Coastguard Worker 
83*fb1b10abSAndroid Build Coastguard Worker // Retrieve the currently set thread worker interface.
84*fb1b10abSAndroid Build Coastguard Worker const VPxWorkerInterface *vpx_get_worker_interface(void);
85*fb1b10abSAndroid Build Coastguard Worker 
86*fb1b10abSAndroid Build Coastguard Worker //------------------------------------------------------------------------------
87*fb1b10abSAndroid Build Coastguard Worker 
88*fb1b10abSAndroid Build Coastguard Worker #ifdef __cplusplus
89*fb1b10abSAndroid Build Coastguard Worker }  // extern "C"
90*fb1b10abSAndroid Build Coastguard Worker #endif
91*fb1b10abSAndroid Build Coastguard Worker 
92*fb1b10abSAndroid Build Coastguard Worker #endif  // VPX_VPX_UTIL_VPX_THREAD_H_
93