• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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 // -----------------------------------------------------------------------------
7 //
8 // Multi-threaded worker
9 //
10 // Author: Skal (pascal.massimino@gmail.com)
11 
12 #ifndef WEBP_UTILS_THREAD_H_
13 #define WEBP_UTILS_THREAD_H_
14 
15 #if defined(__cplusplus) || defined(c_plusplus)
16 extern "C" {
17 #endif
18 
19 #if WEBP_USE_THREAD
20 
21 #if defined(_WIN32)
22 
23 #include <windows.h>
24 typedef HANDLE pthread_t;
25 typedef CRITICAL_SECTION pthread_mutex_t;
26 typedef struct {
27   HANDLE waiting_sem_;
28   HANDLE received_sem_;
29   HANDLE signal_event_;
30 } pthread_cond_t;
31 
32 #else
33 
34 #include <pthread.h>
35 
36 #endif    /* _WIN32 */
37 #endif    /* WEBP_USE_THREAD */
38 
39 // State of the worker thread object
40 typedef enum {
41   NOT_OK = 0,   // object is unusable
42   OK,           // ready to work
43   WORK          // busy finishing the current task
44 } WebPWorkerStatus;
45 
46 // Function to be called by the worker thread. Takes two opaque pointers as
47 // arguments (data1 and data2), and should return false in case of error.
48 typedef int (*WebPWorkerHook)(void*, void*);
49 
50 // Synchronize object used to launch job in the worker thread
51 typedef struct {
52 #if WEBP_USE_THREAD
53   pthread_mutex_t mutex_;
54   pthread_cond_t  condition_;
55   pthread_t       thread_;
56 #endif
57   WebPWorkerStatus status_;
58   WebPWorkerHook hook;    // hook to call
59   void* data1;            // first argument passed to 'hook'
60   void* data2;            // second argument passed to 'hook'
61   int had_error;          // return value of the last call to 'hook'
62 } WebPWorker;
63 
64 // Must be called first, before any other method.
65 void WebPWorkerInit(WebPWorker* const worker);
66 // Must be called initialize the object and spawn the thread. Re-entrant.
67 // Will potentially launch the thread. Returns false in case of error.
68 int WebPWorkerReset(WebPWorker* const worker);
69 // Make sure the previous work is finished. Returns true if worker->had_error
70 // was not set and not error condition was triggered by the working thread.
71 int WebPWorkerSync(WebPWorker* const worker);
72 // Trigger the thread to call hook() with data1 and data2 argument. These
73 // hook/data1/data2 can be changed at any time before calling this function,
74 // but not be changed afterward until the next call to WebPWorkerSync().
75 void WebPWorkerLaunch(WebPWorker* const worker);
76 // Kill the thread and terminate the object. To use the object again, one
77 // must call WebPWorkerReset() again.
78 void WebPWorkerEnd(WebPWorker* const worker);
79 
80 //------------------------------------------------------------------------------
81 
82 #if defined(__cplusplus) || defined(c_plusplus)
83 }    // extern "C"
84 #endif
85 
86 #endif  /* WEBP_UTILS_THREAD_H_ */
87