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