1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // This code is licensed under the same terms as WebM:
4 // Software License Agreement: http://www.webmproject.org/license/software/
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
8 // Multi-threaded worker
10 // Author: Skal (pascal.massimino@gmail.com)
12 #ifndef WEBP_UTILS_THREAD_H_
13 #define WEBP_UTILS_THREAD_H_
19 #if defined(__cplusplus) || defined(c_plusplus)
28 typedef HANDLE pthread_t;
29 typedef CRITICAL_SECTION pthread_mutex_t;
41 #endif /* WEBP_USE_THREAD */
43 // State of the worker thread object
45 NOT_OK = 0, // object is unusable
47 WORK // busy finishing the current task
50 // Function to be called by the worker thread. Takes two opaque pointers as
51 // arguments (data1 and data2), and should return false in case of error.
52 typedef int (*WebPWorkerHook)(void*, void*);
54 // Synchronize object used to launch job in the worker thread
57 pthread_mutex_t mutex_;
58 pthread_cond_t condition_;
61 WebPWorkerStatus status_;
62 WebPWorkerHook hook; // hook to call
63 void* data1; // first argument passed to 'hook'
64 void* data2; // second argument passed to 'hook'
65 int had_error; // return value of the last call to 'hook'
68 // Must be called first, before any other method.
69 void WebPWorkerInit(WebPWorker* const worker);
70 // Must be called to initialize the object and spawn the thread. Re-entrant.
71 // Will potentially launch the thread. Returns false in case of error.
72 int WebPWorkerReset(WebPWorker* const worker);
73 // Makes sure the previous work is finished. Returns true if worker->had_error
74 // was not set and no error condition was triggered by the working thread.
75 int WebPWorkerSync(WebPWorker* const worker);
76 // Triggers the thread to call hook() with data1 and data2 argument. These
77 // hook/data1/data2 can be changed at any time before calling this function,
78 // but not be changed afterward until the next call to WebPWorkerSync().
79 void WebPWorkerLaunch(WebPWorker* const worker);
80 // Kill the thread and terminate the object. To use the object again, one
81 // must call WebPWorkerReset() again.
82 void WebPWorkerEnd(WebPWorker* const worker);
84 //------------------------------------------------------------------------------
86 #if defined(__cplusplus) || defined(c_plusplus)
90 #endif /* WEBP_UTILS_THREAD_H_ */