Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // |
Vikas Arora | 0406ce1 | 2013-08-09 15:57:12 -0700 | [diff] [blame^] | 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. |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
| 10 | // Multi-threaded worker |
| 11 | // |
| 12 | // Author: Skal (pascal.massimino@gmail.com) |
| 13 | |
| 14 | #ifndef WEBP_UTILS_THREAD_H_ |
| 15 | #define WEBP_UTILS_THREAD_H_ |
| 16 | |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 17 | #ifdef HAVE_CONFIG_H |
| 18 | #include "config.h" |
| 19 | #endif |
| 20 | |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 21 | #if defined(__cplusplus) || defined(c_plusplus) |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
| 25 | #if WEBP_USE_THREAD |
| 26 | |
| 27 | #if defined(_WIN32) |
| 28 | |
| 29 | #include <windows.h> |
| 30 | typedef HANDLE pthread_t; |
| 31 | typedef CRITICAL_SECTION pthread_mutex_t; |
| 32 | typedef struct { |
| 33 | HANDLE waiting_sem_; |
| 34 | HANDLE received_sem_; |
| 35 | HANDLE signal_event_; |
| 36 | } pthread_cond_t; |
| 37 | |
| 38 | #else |
| 39 | |
| 40 | #include <pthread.h> |
| 41 | |
| 42 | #endif /* _WIN32 */ |
| 43 | #endif /* WEBP_USE_THREAD */ |
| 44 | |
| 45 | // State of the worker thread object |
| 46 | typedef enum { |
| 47 | NOT_OK = 0, // object is unusable |
| 48 | OK, // ready to work |
| 49 | WORK // busy finishing the current task |
| 50 | } WebPWorkerStatus; |
| 51 | |
| 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*); |
| 55 | |
| 56 | // Synchronize object used to launch job in the worker thread |
| 57 | typedef struct { |
| 58 | #if WEBP_USE_THREAD |
| 59 | pthread_mutex_t mutex_; |
| 60 | pthread_cond_t condition_; |
| 61 | pthread_t thread_; |
| 62 | #endif |
| 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' |
| 68 | } WebPWorker; |
| 69 | |
| 70 | // Must be called first, before any other method. |
| 71 | void WebPWorkerInit(WebPWorker* const worker); |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 72 | // Must be called to initialize the object and spawn the thread. Re-entrant. |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 73 | // Will potentially launch the thread. Returns false in case of error. |
| 74 | int WebPWorkerReset(WebPWorker* const worker); |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 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. |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 77 | int WebPWorkerSync(WebPWorker* const worker); |
Vikas Arora | 1e7bf88 | 2013-03-13 16:43:18 -0700 | [diff] [blame] | 78 | // Triggers the thread to call hook() with data1 and data2 argument. These |
Vikas Arora | a241572 | 2012-08-09 16:18:58 -0700 | [diff] [blame] | 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); |
| 85 | |
| 86 | //------------------------------------------------------------------------------ |
| 87 | |
| 88 | #if defined(__cplusplus) || defined(c_plusplus) |
| 89 | } // extern "C" |
| 90 | #endif |
| 91 | |
| 92 | #endif /* WEBP_UTILS_THREAD_H_ */ |