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 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #if CONFIG_MULTITHREAD
27 
28 #if defined(_WIN32)
29 #include <errno.h>  // NOLINT
30 #include <process.h>  // NOLINT
31 #include <windows.h>  // NOLINT
32 typedef HANDLE pthread_t;
33 typedef CRITICAL_SECTION pthread_mutex_t;
34 typedef struct {
35   HANDLE waiting_sem_;
36   HANDLE received_sem_;
37   HANDLE signal_event_;
38 } pthread_cond_t;
39 
40 //------------------------------------------------------------------------------
41 // simplistic pthread emulation layer
42 
43 // _beginthreadex requires __stdcall
44 #define THREADFN unsigned int __stdcall
45 #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
46 
pthread_create(pthread_t * const thread,const void * attr,unsigned int (__stdcall * start)(void *),void * arg)47 static INLINE int pthread_create(pthread_t* const thread, const void* attr,
48                                  unsigned int (__stdcall *start)(void*),
49                                  void* arg) {
50   (void)attr;
51   *thread = (pthread_t)_beginthreadex(NULL,   /* void *security */
52                                       0,      /* unsigned stack_size */
53                                       start,
54                                       arg,
55                                       0,      /* unsigned initflag */
56                                       NULL);  /* unsigned *thrdaddr */
57   if (*thread == NULL) return 1;
58   SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
59   return 0;
60 }
61 
pthread_join(pthread_t thread,void ** value_ptr)62 static INLINE int pthread_join(pthread_t thread, void** value_ptr) {
63   (void)value_ptr;
64   return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 ||
65           CloseHandle(thread) == 0);
66 }
67 
68 // Mutex
pthread_mutex_init(pthread_mutex_t * const mutex,void * mutexattr)69 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
70                                      void* mutexattr) {
71   (void)mutexattr;
72   InitializeCriticalSection(mutex);
73   return 0;
74 }
75 
pthread_mutex_trylock(pthread_mutex_t * const mutex)76 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
77   return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
78 }
79 
pthread_mutex_lock(pthread_mutex_t * const mutex)80 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
81   EnterCriticalSection(mutex);
82   return 0;
83 }
84 
pthread_mutex_unlock(pthread_mutex_t * const mutex)85 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
86   LeaveCriticalSection(mutex);
87   return 0;
88 }
89 
pthread_mutex_destroy(pthread_mutex_t * const mutex)90 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
91   DeleteCriticalSection(mutex);
92   return 0;
93 }
94 
95 // Condition
pthread_cond_destroy(pthread_cond_t * const condition)96 static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
97   int ok = 1;
98   ok &= (CloseHandle(condition->waiting_sem_) != 0);
99   ok &= (CloseHandle(condition->received_sem_) != 0);
100   ok &= (CloseHandle(condition->signal_event_) != 0);
101   return !ok;
102 }
103 
pthread_cond_init(pthread_cond_t * const condition,void * cond_attr)104 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
105                                     void* cond_attr) {
106   (void)cond_attr;
107   condition->waiting_sem_ = CreateSemaphore(NULL, 0, 1, NULL);
108   condition->received_sem_ = CreateSemaphore(NULL, 0, 1, NULL);
109   condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
110   if (condition->waiting_sem_ == NULL ||
111       condition->received_sem_ == NULL ||
112       condition->signal_event_ == NULL) {
113     pthread_cond_destroy(condition);
114     return 1;
115   }
116   return 0;
117 }
118 
pthread_cond_signal(pthread_cond_t * const condition)119 static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
120   int ok = 1;
121   if (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
122     // a thread is waiting in pthread_cond_wait: allow it to be notified
123     ok = SetEvent(condition->signal_event_);
124     // wait until the event is consumed so the signaler cannot consume
125     // the event via its own pthread_cond_wait.
126     ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
127            WAIT_OBJECT_0);
128   }
129   return !ok;
130 }
131 
pthread_cond_wait(pthread_cond_t * const condition,pthread_mutex_t * const mutex)132 static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
133                                     pthread_mutex_t *const mutex) {
134   int ok;
135   // note that there is a consumer available so the signal isn't dropped in
136   // pthread_cond_signal
137   if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL))
138     return 1;
139   // now unlock the mutex so pthread_cond_signal may be issued
140   pthread_mutex_unlock(mutex);
141   ok = (WaitForSingleObject(condition->signal_event_, INFINITE) ==
142         WAIT_OBJECT_0);
143   ok &= ReleaseSemaphore(condition->received_sem_, 1, NULL);
144   pthread_mutex_lock(mutex);
145   return !ok;
146 }
147 #else  // _WIN32
148 #include <pthread.h> // NOLINT
149 # define THREADFN void*
150 # define THREAD_RETURN(val) val
151 #endif
152 
153 #endif  // CONFIG_MULTITHREAD
154 
155 // State of the worker thread object
156 typedef enum {
157   NOT_OK = 0,   // object is unusable
158   OK,           // ready to work
159   WORK          // busy finishing the current task
160 } VP9WorkerStatus;
161 
162 // Function to be called by the worker thread. Takes two opaque pointers as
163 // arguments (data1 and data2), and should return false in case of error.
164 typedef int (*VP9WorkerHook)(void*, void*);
165 
166 // Synchronize object used to launch job in the worker thread
167 typedef struct {
168 #if CONFIG_MULTITHREAD
169   pthread_mutex_t mutex_;
170   pthread_cond_t  condition_;
171   pthread_t       thread_;
172 #endif
173   VP9WorkerStatus status_;
174   VP9WorkerHook hook;     // hook to call
175   void* data1;            // first argument passed to 'hook'
176   void* data2;            // second argument passed to 'hook'
177   int had_error;          // return value of the last call to 'hook'
178 } VP9Worker;
179 
180 // Must be called first, before any other method.
181 void vp9_worker_init(VP9Worker* const worker);
182 // Must be called to initialize the object and spawn the thread. Re-entrant.
183 // Will potentially launch the thread. Returns false in case of error.
184 int vp9_worker_reset(VP9Worker* const worker);
185 // Makes sure the previous work is finished. Returns true if worker->had_error
186 // was not set and no error condition was triggered by the working thread.
187 int vp9_worker_sync(VP9Worker* const worker);
188 // Triggers the thread to call hook() with data1 and data2 argument. These
189 // hook/data1/data2 can be changed at any time before calling this function,
190 // but not be changed afterward until the next call to vp9_worker_sync().
191 void vp9_worker_launch(VP9Worker* const worker);
192 // This function is similar to vp9_worker_launch() except that it calls the
193 // hook directly instead of using a thread. Convenient to bypass the thread
194 // mechanism while still using the VP9Worker structs. vp9_worker_sync() must
195 // still be called afterward (for error reporting).
196 void vp9_worker_execute(VP9Worker* const worker);
197 // Kill the thread and terminate the object. To use the object again, one
198 // must call vp9_worker_reset() again.
199 void vp9_worker_end(VP9Worker* const worker);
200 
201 //------------------------------------------------------------------------------
202 
203 #ifdef __cplusplus
204 }    // extern "C"
205 #endif
206 
207 #endif  // VP9_DECODER_VP9_THREAD_H_
208