1 | // Copyright 2011 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 | // Author: Skal (pascal.massimino@gmail.com) |
13 | |
14 | #ifndef WEBP_UTILS_THREAD_UTILS_H_ |
15 | #define WEBP_UTILS_THREAD_UTILS_H_ |
16 | |
17 | #ifdef HAVE_CONFIG_H |
18 | #include "src/webp/config.h" |
19 | #endif |
20 | |
21 | #include "src/webp/types.h" |
22 | |
23 | #ifdef __cplusplus |
24 | extern "C" { |
25 | #endif |
26 | |
27 | // State of the worker thread object |
28 | typedef enum { |
29 | NOT_OK = 0, // object is unusable |
30 | OK, // ready to work |
31 | WORK // busy finishing the current task |
32 | } WebPWorkerStatus; |
33 | |
34 | // Function to be called by the worker thread. Takes two opaque pointers as |
35 | // arguments (data1 and data2), and should return false in case of error. |
36 | typedef int (*WebPWorkerHook)(void*, void*); |
37 | |
38 | // Synchronization object used to launch job in the worker thread |
39 | typedef struct { |
40 | void* impl_; // platform-dependent implementation worker details |
41 | WebPWorkerStatus status_; |
42 | WebPWorkerHook hook; // hook to call |
43 | void* data1; // first argument passed to 'hook' |
44 | void* data2; // second argument passed to 'hook' |
45 | int had_error; // return value of the last call to 'hook' |
46 | } WebPWorker; |
47 | |
48 | // The interface for all thread-worker related functions. All these functions |
49 | // must be implemented. |
50 | typedef struct { |
51 | // Must be called first, before any other method. |
52 | void (*Init)(WebPWorker* const worker); |
53 | // Must be called to initialize the object and spawn the thread. Re-entrant. |
54 | // Will potentially launch the thread. Returns false in case of error. |
55 | int (*Reset)(WebPWorker* const worker); |
56 | // Makes sure the previous work is finished. Returns true if worker->had_error |
57 | // was not set and no error condition was triggered by the working thread. |
58 | int (*Sync)(WebPWorker* const worker); |
59 | // Triggers the thread to call hook() with data1 and data2 arguments. These |
60 | // hook/data1/data2 values can be changed at any time before calling this |
61 | // function, but not be changed afterward until the next call to Sync(). |
62 | void (*Launch)(WebPWorker* const worker); |
63 | // This function is similar to Launch() except that it calls the |
64 | // hook directly instead of using a thread. Convenient to bypass the thread |
65 | // mechanism while still using the WebPWorker structs. Sync() must |
66 | // still be called afterward (for error reporting). |
67 | void (*Execute)(WebPWorker* const worker); |
68 | // Kill the thread and terminate the object. To use the object again, one |
69 | // must call Reset() again. |
70 | void (*End)(WebPWorker* const worker); |
71 | } WebPWorkerInterface; |
72 | |
73 | // Install a new set of threading functions, overriding the defaults. This |
74 | // should be done before any workers are started, i.e., before any encoding or |
75 | // decoding takes place. The contents of the interface struct are copied, it |
76 | // is safe to free the corresponding memory after this call. This function is |
77 | // not thread-safe. Return false in case of invalid pointer or methods. |
78 | WEBP_EXTERN int WebPSetWorkerInterface( |
79 | const WebPWorkerInterface* const winterface); |
80 | |
81 | // Retrieve the currently set thread worker interface. |
82 | WEBP_EXTERN const WebPWorkerInterface* WebPGetWorkerInterface(void); |
83 | |
84 | //------------------------------------------------------------------------------ |
85 | |
86 | #ifdef __cplusplus |
87 | } // extern "C" |
88 | #endif |
89 | |
90 | #endif // WEBP_UTILS_THREAD_UTILS_H_ |
91 | |