1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp9/decoder/vp9_thread.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 1.4 +// Copyright 2013 Google Inc. All Rights Reserved. 1.5 +// 1.6 +// Use of this source code is governed by a BSD-style license 1.7 +// that can be found in the COPYING file in the root of the source 1.8 +// tree. An additional intellectual property rights grant can be found 1.9 +// in the file PATENTS. All contributing project authors may 1.10 +// be found in the AUTHORS file in the root of the source tree. 1.11 +// ----------------------------------------------------------------------------- 1.12 +// 1.13 +// Multi-threaded worker 1.14 +// 1.15 +// Original source: 1.16 +// http://git.chromium.org/webm/libwebp.git 1.17 +// 100644 blob 13a61a4c84194c3374080cbf03d881d3cd6af40d src/utils/thread.h 1.18 + 1.19 + 1.20 +#ifndef VP9_DECODER_VP9_THREAD_H_ 1.21 +#define VP9_DECODER_VP9_THREAD_H_ 1.22 + 1.23 +#include "./vpx_config.h" 1.24 + 1.25 +#if defined(__cplusplus) || defined(c_plusplus) 1.26 +extern "C" { 1.27 +#endif 1.28 + 1.29 +#if CONFIG_MULTITHREAD 1.30 + 1.31 +#if defined(_WIN32) 1.32 + 1.33 +#include <windows.h> // NOLINT 1.34 +typedef HANDLE pthread_t; 1.35 +typedef CRITICAL_SECTION pthread_mutex_t; 1.36 +typedef struct { 1.37 + HANDLE waiting_sem_; 1.38 + HANDLE received_sem_; 1.39 + HANDLE signal_event_; 1.40 +} pthread_cond_t; 1.41 + 1.42 +#else 1.43 + 1.44 +#include <pthread.h> // NOLINT 1.45 + 1.46 +#endif /* _WIN32 */ 1.47 +#endif /* CONFIG_MULTITHREAD */ 1.48 + 1.49 +// State of the worker thread object 1.50 +typedef enum { 1.51 + NOT_OK = 0, // object is unusable 1.52 + OK, // ready to work 1.53 + WORK // busy finishing the current task 1.54 +} VP9WorkerStatus; 1.55 + 1.56 +// Function to be called by the worker thread. Takes two opaque pointers as 1.57 +// arguments (data1 and data2), and should return false in case of error. 1.58 +typedef int (*VP9WorkerHook)(void*, void*); 1.59 + 1.60 +// Synchronize object used to launch job in the worker thread 1.61 +typedef struct { 1.62 +#if CONFIG_MULTITHREAD 1.63 + pthread_mutex_t mutex_; 1.64 + pthread_cond_t condition_; 1.65 + pthread_t thread_; 1.66 +#endif 1.67 + VP9WorkerStatus status_; 1.68 + VP9WorkerHook hook; // hook to call 1.69 + void* data1; // first argument passed to 'hook' 1.70 + void* data2; // second argument passed to 'hook' 1.71 + int had_error; // return value of the last call to 'hook' 1.72 +} VP9Worker; 1.73 + 1.74 +// Must be called first, before any other method. 1.75 +void vp9_worker_init(VP9Worker* const worker); 1.76 +// Must be called to initialize the object and spawn the thread. Re-entrant. 1.77 +// Will potentially launch the thread. Returns false in case of error. 1.78 +int vp9_worker_reset(VP9Worker* const worker); 1.79 +// Makes sure the previous work is finished. Returns true if worker->had_error 1.80 +// was not set and no error condition was triggered by the working thread. 1.81 +int vp9_worker_sync(VP9Worker* const worker); 1.82 +// Triggers the thread to call hook() with data1 and data2 argument. These 1.83 +// hook/data1/data2 can be changed at any time before calling this function, 1.84 +// but not be changed afterward until the next call to vp9_worker_sync(). 1.85 +void vp9_worker_launch(VP9Worker* const worker); 1.86 +// This function is similar to vp9_worker_launch() except that it calls the 1.87 +// hook directly instead of using a thread. Convenient to bypass the thread 1.88 +// mechanism while still using the VP9Worker structs. vp9_worker_sync() must 1.89 +// still be called afterward (for error reporting). 1.90 +void vp9_worker_execute(VP9Worker* const worker); 1.91 +// Kill the thread and terminate the object. To use the object again, one 1.92 +// must call vp9_worker_reset() again. 1.93 +void vp9_worker_end(VP9Worker* const worker); 1.94 + 1.95 +//------------------------------------------------------------------------------ 1.96 + 1.97 +#if defined(__cplusplus) || defined(c_plusplus) 1.98 +} // extern "C" 1.99 +#endif 1.100 + 1.101 +#endif // VP9_DECODER_VP9_THREAD_H_