michael@0: // Copyright 2013 Google Inc. All Rights Reserved. michael@0: // michael@0: // Use of this source code is governed by a BSD-style license michael@0: // that can be found in the COPYING file in the root of the source michael@0: // tree. An additional intellectual property rights grant can be found michael@0: // in the file PATENTS. All contributing project authors may michael@0: // be found in the AUTHORS file in the root of the source tree. michael@0: // ----------------------------------------------------------------------------- michael@0: // michael@0: // Multi-threaded worker michael@0: // michael@0: // Original source: michael@0: // http://git.chromium.org/webm/libwebp.git michael@0: // 100644 blob 13a61a4c84194c3374080cbf03d881d3cd6af40d src/utils/thread.h michael@0: michael@0: michael@0: #ifndef VP9_DECODER_VP9_THREAD_H_ michael@0: #define VP9_DECODER_VP9_THREAD_H_ michael@0: michael@0: #include "./vpx_config.h" michael@0: michael@0: #if defined(__cplusplus) || defined(c_plusplus) michael@0: extern "C" { michael@0: #endif michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: michael@0: #if defined(_WIN32) michael@0: michael@0: #include // NOLINT michael@0: typedef HANDLE pthread_t; michael@0: typedef CRITICAL_SECTION pthread_mutex_t; michael@0: typedef struct { michael@0: HANDLE waiting_sem_; michael@0: HANDLE received_sem_; michael@0: HANDLE signal_event_; michael@0: } pthread_cond_t; michael@0: michael@0: #else michael@0: michael@0: #include // NOLINT michael@0: michael@0: #endif /* _WIN32 */ michael@0: #endif /* CONFIG_MULTITHREAD */ michael@0: michael@0: // State of the worker thread object michael@0: typedef enum { michael@0: NOT_OK = 0, // object is unusable michael@0: OK, // ready to work michael@0: WORK // busy finishing the current task michael@0: } VP9WorkerStatus; michael@0: michael@0: // Function to be called by the worker thread. Takes two opaque pointers as michael@0: // arguments (data1 and data2), and should return false in case of error. michael@0: typedef int (*VP9WorkerHook)(void*, void*); michael@0: michael@0: // Synchronize object used to launch job in the worker thread michael@0: typedef struct { michael@0: #if CONFIG_MULTITHREAD michael@0: pthread_mutex_t mutex_; michael@0: pthread_cond_t condition_; michael@0: pthread_t thread_; michael@0: #endif michael@0: VP9WorkerStatus status_; michael@0: VP9WorkerHook hook; // hook to call michael@0: void* data1; // first argument passed to 'hook' michael@0: void* data2; // second argument passed to 'hook' michael@0: int had_error; // return value of the last call to 'hook' michael@0: } VP9Worker; michael@0: michael@0: // Must be called first, before any other method. michael@0: void vp9_worker_init(VP9Worker* const worker); michael@0: // Must be called to initialize the object and spawn the thread. Re-entrant. michael@0: // Will potentially launch the thread. Returns false in case of error. michael@0: int vp9_worker_reset(VP9Worker* const worker); michael@0: // Makes sure the previous work is finished. Returns true if worker->had_error michael@0: // was not set and no error condition was triggered by the working thread. michael@0: int vp9_worker_sync(VP9Worker* const worker); michael@0: // Triggers the thread to call hook() with data1 and data2 argument. These michael@0: // hook/data1/data2 can be changed at any time before calling this function, michael@0: // but not be changed afterward until the next call to vp9_worker_sync(). michael@0: void vp9_worker_launch(VP9Worker* const worker); michael@0: // This function is similar to vp9_worker_launch() except that it calls the michael@0: // hook directly instead of using a thread. Convenient to bypass the thread michael@0: // mechanism while still using the VP9Worker structs. vp9_worker_sync() must michael@0: // still be called afterward (for error reporting). michael@0: void vp9_worker_execute(VP9Worker* const worker); michael@0: // Kill the thread and terminate the object. To use the object again, one michael@0: // must call vp9_worker_reset() again. michael@0: void vp9_worker_end(VP9Worker* const worker); michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: #if defined(__cplusplus) || defined(c_plusplus) michael@0: } // extern "C" michael@0: #endif michael@0: michael@0: #endif // VP9_DECODER_VP9_THREAD_H_