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