Path: blob/master/thirdparty/libwebp/src/utils/thread_utils.h
9912 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Multi-threaded worker10//11// Author: Skal ([email protected])1213#ifndef WEBP_UTILS_THREAD_UTILS_H_14#define WEBP_UTILS_THREAD_UTILS_H_1516#ifdef HAVE_CONFIG_H17#include "src/webp/config.h"18#endif1920#include "src/webp/types.h"2122#ifdef __cplusplus23extern "C" {24#endif2526// State of the worker thread object27typedef enum {28NOT_OK = 0, // object is unusable29OK, // ready to work30WORK // busy finishing the current task31} WebPWorkerStatus;3233// Function to be called by the worker thread. Takes two opaque pointers as34// arguments (data1 and data2), and should return false in case of error.35typedef int (*WebPWorkerHook)(void*, void*);3637// Synchronization object used to launch job in the worker thread38typedef struct {39void* impl_; // platform-dependent implementation worker details40WebPWorkerStatus status_;41WebPWorkerHook hook; // hook to call42void* data1; // first argument passed to 'hook'43void* data2; // second argument passed to 'hook'44int had_error; // return value of the last call to 'hook'45} WebPWorker;4647// The interface for all thread-worker related functions. All these functions48// must be implemented.49typedef struct {50// Must be called first, before any other method.51void (*Init)(WebPWorker* const worker);52// Must be called to initialize the object and spawn the thread. Re-entrant.53// Will potentially launch the thread. Returns false in case of error.54int (*Reset)(WebPWorker* const worker);55// Makes sure the previous work is finished. Returns true if worker->had_error56// was not set and no error condition was triggered by the working thread.57int (*Sync)(WebPWorker* const worker);58// Triggers the thread to call hook() with data1 and data2 arguments. These59// hook/data1/data2 values can be changed at any time before calling this60// function, but not be changed afterward until the next call to Sync().61void (*Launch)(WebPWorker* const worker);62// This function is similar to Launch() except that it calls the63// hook directly instead of using a thread. Convenient to bypass the thread64// mechanism while still using the WebPWorker structs. Sync() must65// still be called afterward (for error reporting).66void (*Execute)(WebPWorker* const worker);67// Kill the thread and terminate the object. To use the object again, one68// must call Reset() again.69void (*End)(WebPWorker* const worker);70} WebPWorkerInterface;7172// Install a new set of threading functions, overriding the defaults. This73// should be done before any workers are started, i.e., before any encoding or74// decoding takes place. The contents of the interface struct are copied, it75// is safe to free the corresponding memory after this call. This function is76// not thread-safe. Return false in case of invalid pointer or methods.77WEBP_EXTERN int WebPSetWorkerInterface(78const WebPWorkerInterface* const winterface);7980// Retrieve the currently set thread worker interface.81WEBP_EXTERN const WebPWorkerInterface* WebPGetWorkerInterface(void);8283//------------------------------------------------------------------------------8485#ifdef __cplusplus86} // extern "C"87#endif8889#endif // WEBP_UTILS_THREAD_UTILS_H_909192