1 // Copyright 2011 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Multi-threaded worker
12 // Author: Skal (pascal.massimino@gmail.com)
14 #ifndef WEBP_UTILS_THREAD_H_
15 #define WEBP_UTILS_THREAD_H_
21 #if defined(__cplusplus) || defined(c_plusplus)
30 typedef HANDLE pthread_t;
31 typedef CRITICAL_SECTION pthread_mutex_t;
43 #endif /* WEBP_USE_THREAD */
45 // State of the worker thread object
47 NOT_OK = 0, // object is unusable
49 WORK // busy finishing the current task
52 // Function to be called by the worker thread. Takes two opaque pointers as
53 // arguments (data1 and data2), and should return false in case of error.
54 typedef int (*WebPWorkerHook)(void*, void*);
56 // Synchronize object used to launch job in the worker thread
59 pthread_mutex_t mutex_;
60 pthread_cond_t condition_;
63 WebPWorkerStatus status_;
64 WebPWorkerHook hook; // hook to call
65 void* data1; // first argument passed to 'hook'
66 void* data2; // second argument passed to 'hook'
67 int had_error; // return value of the last call to 'hook'
70 // Must be called first, before any other method.
71 void WebPWorkerInit(WebPWorker* const worker);
72 // Must be called to initialize the object and spawn the thread. Re-entrant.
73 // Will potentially launch the thread. Returns false in case of error.
74 int WebPWorkerReset(WebPWorker* const worker);
75 // Makes sure the previous work is finished. Returns true if worker->had_error
76 // was not set and no error condition was triggered by the working thread.
77 int WebPWorkerSync(WebPWorker* const worker);
78 // Triggers the thread to call hook() with data1 and data2 argument. These
79 // hook/data1/data2 can be changed at any time before calling this function,
80 // but not be changed afterward until the next call to WebPWorkerSync().
81 void WebPWorkerLaunch(WebPWorker* const worker);
82 // Kill the thread and terminate the object. To use the object again, one
83 // must call WebPWorkerReset() again.
84 void WebPWorkerEnd(WebPWorker* const worker);
86 //------------------------------------------------------------------------------
88 #if defined(__cplusplus) || defined(c_plusplus)
92 #endif /* WEBP_UTILS_THREAD_H_ */