thread.h 3.6 KB

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