• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 //
12 // Multi-threaded worker
13 //
14 // Original source:
15 //  https://chromium.googlesource.com/webm/libwebp
16 
17 #ifndef AOM_AOM_UTIL_AOM_THREAD_H_
18 #define AOM_AOM_UTIL_AOM_THREAD_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define MAX_NUM_THREADS 64
25 
26 // State of the worker thread object
27 typedef enum {
28   AVX_WORKER_STATUS_NOT_OK = 0,  // object is unusable
29   AVX_WORKER_STATUS_OK,          // ready to work
30   AVX_WORKER_STATUS_WORKING      // busy finishing the current task
31 } AVxWorkerStatus;
32 
33 // Function to be called by the worker thread. Takes two opaque pointers as
34 // arguments (data1 and data2). Should return true on success and return false
35 // in case of error.
36 typedef int (*AVxWorkerHook)(void *, void *);
37 
38 // Platform-dependent implementation details for the worker.
39 typedef struct AVxWorkerImpl AVxWorkerImpl;
40 
41 // Synchronization object used to launch job in the worker thread
42 typedef struct {
43   AVxWorkerImpl *impl_;
44   AVxWorkerStatus status_;
45   // Thread name for the debugger. If not NULL, must point to a string that
46   // outlives the worker thread. For portability, use a name <= 15 characters
47   // long (not including the terminating NUL character).
48   const char *thread_name;
49   AVxWorkerHook hook;  // hook to call
50   void *data1;         // first argument passed to 'hook'
51   void *data2;         // second argument passed to 'hook'
52   int had_error;       // true if a call to 'hook' returned false
53 } AVxWorker;
54 
55 // The interface for all thread-worker related functions. All these functions
56 // must be implemented.
57 typedef struct {
58   // Must be called first, before any other method.
59   void (*init)(AVxWorker *const worker);
60   // Must be called to initialize the object and spawn the thread. Re-entrant.
61   // Will potentially launch the thread. Returns false in case of error.
62   int (*reset)(AVxWorker *const worker);
63   // Makes sure the previous work is finished. Returns true if worker->had_error
64   // was not set and no error condition was triggered by the working thread.
65   int (*sync)(AVxWorker *const worker);
66   // Triggers the thread to call hook() with data1 and data2 arguments. These
67   // hook/data1/data2 values can be changed at any time before calling this
68   // function, but not be changed afterward until the next call to Sync().
69   void (*launch)(AVxWorker *const worker);
70   // This function is similar to launch() except that it calls the
71   // hook directly instead of using a thread. Convenient to bypass the thread
72   // mechanism while still using the AVxWorker structs. sync() must
73   // still be called afterward (for error reporting).
74   void (*execute)(AVxWorker *const worker);
75   // Kill the thread and terminate the object. To use the object again, one
76   // must call reset() again.
77   void (*end)(AVxWorker *const worker);
78 } AVxWorkerInterface;
79 
80 // Install a new set of threading functions, overriding the defaults. This
81 // should be done before any workers are started, i.e., before any encoding or
82 // decoding takes place. The contents of the interface struct are copied, it
83 // is safe to free the corresponding memory after this call. This function is
84 // not thread-safe. Return false in case of invalid pointer or methods.
85 int aom_set_worker_interface(const AVxWorkerInterface *const winterface);
86 
87 // Retrieve the currently set thread worker interface.
88 const AVxWorkerInterface *aom_get_worker_interface(void);
89 
90 //------------------------------------------------------------------------------
91 
92 #ifdef __cplusplus
93 }  // extern "C"
94 #endif
95 
96 #endif  // AOM_AOM_UTIL_AOM_THREAD_H_
97