Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // |
Vikas Arora | 0406ce1 | 2013-08-09 15:57:12 -0700 | [diff] [blame] | 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. |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
| 10 | // Multi-threaded worker |
| 11 | // |
| 12 | // Author: Skal (pascal.massimino@gmail.com) |
| 13 | |
| 14 | #ifndef WEBP_UTILS_THREAD_H_ |
| 15 | #define WEBP_UTILS_THREAD_H_ |
| 16 | |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 17 | #ifdef HAVE_CONFIG_H |
James Zern | 9e80ee9 | 2015-03-17 18:54:21 -0700 | [diff] [blame] | 18 | #include "../webp/config.h" |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 19 | #endif |
| 20 | |
James Zern | 9e80ee9 | 2015-03-17 18:54:21 -0700 | [diff] [blame] | 21 | #include "../webp/types.h" |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 22 | |
Vikas Arora | 8b72022 | 2014-01-02 16:48:02 -0800 | [diff] [blame] | 23 | #ifdef __cplusplus |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 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 | |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 38 | // Platform-dependent implementation details for the worker. |
| 39 | typedef struct WebPWorkerImpl WebPWorkerImpl; |
| 40 | |
| 41 | // Synchronization object used to launch job in the worker thread |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 42 | typedef struct { |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 43 | WebPWorkerImpl* impl_; |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 44 | WebPWorkerStatus status_; |
| 45 | WebPWorkerHook hook; // hook to call |
| 46 | void* data1; // first argument passed to 'hook' |
| 47 | void* data2; // second argument passed to 'hook' |
| 48 | int had_error; // return value of the last call to 'hook' |
| 49 | } WebPWorker; |
| 50 | |
Vikas Arora | 33f74da | 2014-07-25 13:53:32 -0700 | [diff] [blame] | 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)(WebPWorker* 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)(WebPWorker* 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)(WebPWorker* 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)(WebPWorker* 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 WebPWorker structs. Sync() must |
| 69 | // still be called afterward (for error reporting). |
| 70 | void (*Execute)(WebPWorker* const worker); |
| 71 | // Kill the thread and terminate the object. To use the object again, one |
| 72 | // must call Reset() again. |
| 73 | void (*End)(WebPWorker* const worker); |
| 74 | } WebPWorkerInterface; |
| 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 | WEBP_EXTERN(int) WebPSetWorkerInterface( |
| 82 | const WebPWorkerInterface* const interface); |
| 83 | |
| 84 | // Retrieve the currently set thread worker interface. |
| 85 | WEBP_EXTERN(const WebPWorkerInterface*) WebPGetWorkerInterface(void); |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 86 | |
| 87 | //------------------------------------------------------------------------------ |
| 88 | |
Vikas Arora | 8b72022 | 2014-01-02 16:48:02 -0800 | [diff] [blame] | 89 | #ifdef __cplusplus |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 90 | } // extern "C" |
| 91 | #endif |
| 92 | |
| 93 | #endif /* WEBP_UTILS_THREAD_H_ */ |