Blame src/utils/thread_utils.h

Packit 9c6abc
// Copyright 2011 Google Inc. All Rights Reserved.
Packit 9c6abc
//
Packit 9c6abc
// Use of this source code is governed by a BSD-style license
Packit 9c6abc
// that can be found in the COPYING file in the root of the source
Packit 9c6abc
// tree. An additional intellectual property rights grant can be found
Packit 9c6abc
// in the file PATENTS. All contributing project authors may
Packit 9c6abc
// be found in the AUTHORS file in the root of the source tree.
Packit 9c6abc
// -----------------------------------------------------------------------------
Packit 9c6abc
//
Packit 9c6abc
// Multi-threaded worker
Packit 9c6abc
//
Packit 9c6abc
// Author: Skal (pascal.massimino@gmail.com)
Packit 9c6abc
Packit 9c6abc
#ifndef WEBP_UTILS_THREAD_UTILS_H_
Packit 9c6abc
#define WEBP_UTILS_THREAD_UTILS_H_
Packit 9c6abc
Packit 9c6abc
#ifdef HAVE_CONFIG_H
Packit 9c6abc
#include "src/webp/config.h"
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
#include "src/webp/types.h"
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
extern "C" {
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
// State of the worker thread object
Packit 9c6abc
typedef enum {
Packit 9c6abc
  NOT_OK = 0,   // object is unusable
Packit 9c6abc
  OK,           // ready to work
Packit 9c6abc
  WORK          // busy finishing the current task
Packit 9c6abc
} WebPWorkerStatus;
Packit 9c6abc
Packit 9c6abc
// Function to be called by the worker thread. Takes two opaque pointers as
Packit 9c6abc
// arguments (data1 and data2), and should return false in case of error.
Packit 9c6abc
typedef int (*WebPWorkerHook)(void*, void*);
Packit 9c6abc
Packit 9c6abc
// Synchronization object used to launch job in the worker thread
Packit 9c6abc
typedef struct {
Packit 9c6abc
  void* impl_;            // platform-dependent implementation worker details
Packit 9c6abc
  WebPWorkerStatus status_;
Packit 9c6abc
  WebPWorkerHook hook;    // hook to call
Packit 9c6abc
  void* data1;            // first argument passed to 'hook'
Packit 9c6abc
  void* data2;            // second argument passed to 'hook'
Packit 9c6abc
  int had_error;          // return value of the last call to 'hook'
Packit 9c6abc
} WebPWorker;
Packit 9c6abc
Packit 9c6abc
// The interface for all thread-worker related functions. All these functions
Packit 9c6abc
// must be implemented.
Packit 9c6abc
typedef struct {
Packit 9c6abc
  // Must be called first, before any other method.
Packit 9c6abc
  void (*Init)(WebPWorker* const worker);
Packit 9c6abc
  // Must be called to initialize the object and spawn the thread. Re-entrant.
Packit 9c6abc
  // Will potentially launch the thread. Returns false in case of error.
Packit 9c6abc
  int (*Reset)(WebPWorker* const worker);
Packit 9c6abc
  // Makes sure the previous work is finished. Returns true if worker->had_error
Packit 9c6abc
  // was not set and no error condition was triggered by the working thread.
Packit 9c6abc
  int (*Sync)(WebPWorker* const worker);
Packit 9c6abc
  // Triggers the thread to call hook() with data1 and data2 arguments. These
Packit 9c6abc
  // hook/data1/data2 values can be changed at any time before calling this
Packit 9c6abc
  // function, but not be changed afterward until the next call to Sync().
Packit 9c6abc
  void (*Launch)(WebPWorker* const worker);
Packit 9c6abc
  // This function is similar to Launch() except that it calls the
Packit 9c6abc
  // hook directly instead of using a thread. Convenient to bypass the thread
Packit 9c6abc
  // mechanism while still using the WebPWorker structs. Sync() must
Packit 9c6abc
  // still be called afterward (for error reporting).
Packit 9c6abc
  void (*Execute)(WebPWorker* const worker);
Packit 9c6abc
  // Kill the thread and terminate the object. To use the object again, one
Packit 9c6abc
  // must call Reset() again.
Packit 9c6abc
  void (*End)(WebPWorker* const worker);
Packit 9c6abc
} WebPWorkerInterface;
Packit 9c6abc
Packit 9c6abc
// Install a new set of threading functions, overriding the defaults. This
Packit 9c6abc
// should be done before any workers are started, i.e., before any encoding or
Packit 9c6abc
// decoding takes place. The contents of the interface struct are copied, it
Packit 9c6abc
// is safe to free the corresponding memory after this call. This function is
Packit 9c6abc
// not thread-safe. Return false in case of invalid pointer or methods.
Packit 9c6abc
WEBP_EXTERN int WebPSetWorkerInterface(
Packit 9c6abc
    const WebPWorkerInterface* const winterface);
Packit 9c6abc
Packit 9c6abc
// Retrieve the currently set thread worker interface.
Packit 9c6abc
WEBP_EXTERN const WebPWorkerInterface* WebPGetWorkerInterface(void);
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
}    // extern "C"
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
#endif  /* WEBP_UTILS_THREAD_UTILS_H_ */