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 #ifdef HAVE_CONFIG_H 16 #include "config.h" 17 #endif 18 19 #if defined(__cplusplus) || defined(c_plusplus) 20 extern "C" { 21 #endif 22 23 #if WEBP_USE_THREAD 24 25 #if defined(_WIN32) 26 27 #include <windows.h> 28 typedef HANDLE pthread_t; 29 typedef CRITICAL_SECTION pthread_mutex_t; 30 typedef struct { 31 HANDLE waiting_sem_; 32 HANDLE received_sem_; 33 HANDLE signal_event_; 34 } pthread_cond_t; 35 36 #else 37 38 #include <pthread.h> 39 40 #endif /* _WIN32 */ 41 #endif /* WEBP_USE_THREAD */ 42 43 // State of the worker thread object 44 typedef enum { 45 NOT_OK = 0, // object is unusable 46 OK, // ready to work 47 WORK // busy finishing the current task 48 } WebPWorkerStatus; 49 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*); 53 54 // Synchronize object used to launch job in the worker thread 55 typedef struct { 56 #if WEBP_USE_THREAD 57 pthread_mutex_t mutex_; 58 pthread_cond_t condition_; 59 pthread_t thread_; 60 #endif 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' 66 } WebPWorker; 67 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); 83 84 //------------------------------------------------------------------------------ 85 86 #if defined(__cplusplus) || defined(c_plusplus) 87 } // extern "C" 88 #endif 89 90 #endif /* WEBP_UTILS_THREAD_H_ */ 91