Home | History | Annotate | Download | only in decoder
      1 // Copyright 2013 Google Inc. All Rights Reserved.
      2 //
      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.
      8 // -----------------------------------------------------------------------------
      9 //
     10 // Multi-threaded worker
     11 //
     12 // Original source:
     13 //  http://git.chromium.org/webm/libwebp.git
     14 //  100644 blob 13a61a4c84194c3374080cbf03d881d3cd6af40d  src/utils/thread.h
     15 
     16 
     17 #ifndef VP9_DECODER_VP9_THREAD_H_
     18 #define VP9_DECODER_VP9_THREAD_H_
     19 
     20 #include "./vpx_config.h"
     21 
     22 #if defined(__cplusplus) || defined(c_plusplus)
     23 extern "C" {
     24 #endif
     25 
     26 #if CONFIG_MULTITHREAD
     27 
     28 #if defined(_WIN32)
     29 
     30 #include <windows.h>  // NOLINT
     31 typedef HANDLE pthread_t;
     32 typedef CRITICAL_SECTION pthread_mutex_t;
     33 typedef struct {
     34   HANDLE waiting_sem_;
     35   HANDLE received_sem_;
     36   HANDLE signal_event_;
     37 } pthread_cond_t;
     38 
     39 #else
     40 
     41 #include <pthread.h> // NOLINT
     42 
     43 #endif    /* _WIN32 */
     44 #endif    /* CONFIG_MULTITHREAD */
     45 
     46 // State of the worker thread object
     47 typedef enum {
     48   NOT_OK = 0,   // object is unusable
     49   OK,           // ready to work
     50   WORK          // busy finishing the current task
     51 } VP9WorkerStatus;
     52 
     53 // Function to be called by the worker thread. Takes two opaque pointers as
     54 // arguments (data1 and data2), and should return false in case of error.
     55 typedef int (*VP9WorkerHook)(void*, void*);
     56 
     57 // Synchronize object used to launch job in the worker thread
     58 typedef struct {
     59 #if CONFIG_MULTITHREAD
     60   pthread_mutex_t mutex_;
     61   pthread_cond_t  condition_;
     62   pthread_t       thread_;
     63 #endif
     64   VP9WorkerStatus status_;
     65   VP9WorkerHook hook;     // hook to call
     66   void* data1;            // first argument passed to 'hook'
     67   void* data2;            // second argument passed to 'hook'
     68   int had_error;          // return value of the last call to 'hook'
     69 } VP9Worker;
     70 
     71 // Must be called first, before any other method.
     72 void vp9_worker_init(VP9Worker* const worker);
     73 // Must be called to initialize the object and spawn the thread. Re-entrant.
     74 // Will potentially launch the thread. Returns false in case of error.
     75 int vp9_worker_reset(VP9Worker* const worker);
     76 // Makes sure the previous work is finished. Returns true if worker->had_error
     77 // was not set and no error condition was triggered by the working thread.
     78 int vp9_worker_sync(VP9Worker* const worker);
     79 // Triggers the thread to call hook() with data1 and data2 argument. These
     80 // hook/data1/data2 can be changed at any time before calling this function,
     81 // but not be changed afterward until the next call to vp9_worker_sync().
     82 void vp9_worker_launch(VP9Worker* const worker);
     83 // This function is similar to vp9_worker_launch() except that it calls the
     84 // hook directly instead of using a thread. Convenient to bypass the thread
     85 // mechanism while still using the VP9Worker structs. vp9_worker_sync() must
     86 // still be called afterward (for error reporting).
     87 void vp9_worker_execute(VP9Worker* const worker);
     88 // Kill the thread and terminate the object. To use the object again, one
     89 // must call vp9_worker_reset() again.
     90 void vp9_worker_end(VP9Worker* const worker);
     91 
     92 //------------------------------------------------------------------------------
     93 
     94 #if defined(__cplusplus) || defined(c_plusplus)
     95 }    // extern "C"
     96 #endif
     97 
     98 #endif  // VP9_DECODER_VP9_THREAD_H_
     99