OLD | NEW |
1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Multi-threaded worker | 10 // Multi-threaded worker |
11 // | 11 // |
12 // Author: Skal (pascal.massimino@gmail.com) | 12 // Author: Skal (pascal.massimino@gmail.com) |
13 | 13 |
14 #ifndef WEBP_UTILS_THREAD_H_ | 14 #ifndef WEBP_UTILS_THREAD_H_ |
15 #define WEBP_UTILS_THREAD_H_ | 15 #define WEBP_UTILS_THREAD_H_ |
16 | 16 |
17 #ifdef HAVE_CONFIG_H | 17 #ifdef HAVE_CONFIG_H |
18 #include "config.h" | 18 #include "config.h" |
19 #endif | 19 #endif |
20 | 20 |
21 #if defined(__cplusplus) || defined(c_plusplus) | 21 #ifdef __cplusplus |
22 extern "C" { | 22 extern "C" { |
23 #endif | 23 #endif |
24 | 24 |
25 #if WEBP_USE_THREAD | 25 #ifdef WEBP_USE_THREAD |
26 | 26 |
27 #if defined(_WIN32) | 27 #if defined(_WIN32) |
28 | 28 |
29 #include <windows.h> | 29 #include <windows.h> |
30 typedef HANDLE pthread_t; | 30 typedef HANDLE pthread_t; |
31 typedef CRITICAL_SECTION pthread_mutex_t; | 31 typedef CRITICAL_SECTION pthread_mutex_t; |
32 typedef struct { | 32 typedef struct { |
33 HANDLE waiting_sem_; | 33 HANDLE waiting_sem_; |
34 HANDLE received_sem_; | 34 HANDLE received_sem_; |
35 HANDLE signal_event_; | 35 HANDLE signal_event_; |
(...skipping 12 matching lines...) Expand all Loading... |
48 OK, // ready to work | 48 OK, // ready to work |
49 WORK // busy finishing the current task | 49 WORK // busy finishing the current task |
50 } WebPWorkerStatus; | 50 } WebPWorkerStatus; |
51 | 51 |
52 // Function to be called by the worker thread. Takes two opaque pointers as | 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. | 53 // arguments (data1 and data2), and should return false in case of error. |
54 typedef int (*WebPWorkerHook)(void*, void*); | 54 typedef int (*WebPWorkerHook)(void*, void*); |
55 | 55 |
56 // Synchronize object used to launch job in the worker thread | 56 // Synchronize object used to launch job in the worker thread |
57 typedef struct { | 57 typedef struct { |
58 #if WEBP_USE_THREAD | 58 #ifdef WEBP_USE_THREAD |
59 pthread_mutex_t mutex_; | 59 pthread_mutex_t mutex_; |
60 pthread_cond_t condition_; | 60 pthread_cond_t condition_; |
61 pthread_t thread_; | 61 pthread_t thread_; |
62 #endif | 62 #endif |
63 WebPWorkerStatus status_; | 63 WebPWorkerStatus status_; |
64 WebPWorkerHook hook; // hook to call | 64 WebPWorkerHook hook; // hook to call |
65 void* data1; // first argument passed to 'hook' | 65 void* data1; // first argument passed to 'hook' |
66 void* data2; // second argument passed to 'hook' | 66 void* data2; // second argument passed to 'hook' |
67 int had_error; // return value of the last call to 'hook' | 67 int had_error; // return value of the last call to 'hook' |
68 } WebPWorker; | 68 } WebPWorker; |
69 | 69 |
70 // Must be called first, before any other method. | 70 // Must be called first, before any other method. |
71 void WebPWorkerInit(WebPWorker* const worker); | 71 void WebPWorkerInit(WebPWorker* const worker); |
72 // Must be called to initialize the object and spawn the thread. Re-entrant. | 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. | 73 // Will potentially launch the thread. Returns false in case of error. |
74 int WebPWorkerReset(WebPWorker* const worker); | 74 int WebPWorkerReset(WebPWorker* const worker); |
75 // Makes sure the previous work is finished. Returns true if worker->had_error | 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. | 76 // was not set and no error condition was triggered by the working thread. |
77 int WebPWorkerSync(WebPWorker* const worker); | 77 int WebPWorkerSync(WebPWorker* const worker); |
78 // Triggers the thread to call hook() with data1 and data2 argument. These | 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, | 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(). | 80 // but not be changed afterward until the next call to WebPWorkerSync(). |
81 void WebPWorkerLaunch(WebPWorker* const worker); | 81 void WebPWorkerLaunch(WebPWorker* const worker); |
| 82 // This function is similar to WebPWorkerLaunch() except that it calls the |
| 83 // hook directly instead of using a thread. Convenient to bypass the thread |
| 84 // mechanism while still using the WebPWorker structs. WebPWorkerSync() must |
| 85 // still be called afterward (for error reporting). |
| 86 void WebPWorkerExecute(WebPWorker* const worker); |
82 // Kill the thread and terminate the object. To use the object again, one | 87 // Kill the thread and terminate the object. To use the object again, one |
83 // must call WebPWorkerReset() again. | 88 // must call WebPWorkerReset() again. |
84 void WebPWorkerEnd(WebPWorker* const worker); | 89 void WebPWorkerEnd(WebPWorker* const worker); |
85 | 90 |
86 //------------------------------------------------------------------------------ | 91 //------------------------------------------------------------------------------ |
87 | 92 |
88 #if defined(__cplusplus) || defined(c_plusplus) | 93 #ifdef __cplusplus |
89 } // extern "C" | 94 } // extern "C" |
90 #endif | 95 #endif |
91 | 96 |
92 #endif /* WEBP_UTILS_THREAD_H_ */ | 97 #endif /* WEBP_UTILS_THREAD_H_ */ |
OLD | NEW |