blob: 12848fedeff0624b91b81a489f7bfcec31492829 [file] [log] [blame]
James Zern183b77d2013-07-30 22:46:58 -07001// 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
James Zerne656f442014-06-19 21:14:51 -070014// 100644 blob 7bd451b124ae3b81596abfbcc823e3cb129d3a38 src/utils/thread.h
James Zern183b77d2013-07-30 22:46:58 -070015
16#ifndef VP9_DECODER_VP9_THREAD_H_
17#define VP9_DECODER_VP9_THREAD_H_
18
Jim Bankoskida17ffa2013-09-29 11:49:52 -070019#include "./vpx_config.h"
James Zern183b77d2013-07-30 22:46:58 -070020
James Zern40aa9102014-01-18 12:16:11 -080021#ifdef __cplusplus
James Zern183b77d2013-07-30 22:46:58 -070022extern "C" {
23#endif
24
hkuangdd88f482015-02-05 13:59:15 -080025// Set maximum decode threads to be 8 due to the limit of frame buffers
26// and not enough semaphores in the emulation layer on windows.
27#define MAX_DECODE_THREADS 8
28
James Zern183b77d2013-07-30 22:46:58 -070029#if CONFIG_MULTITHREAD
30
James Zernd167a1a2015-02-10 12:47:14 -080031#if defined(_WIN32) && !HAVE_PTHREAD_H
Yunqing Wang903801f2013-12-27 15:25:54 -080032#include <errno.h> // NOLINT
33#include <process.h> // NOLINT
Jim Bankoskic52d8542013-10-01 15:19:39 -070034#include <windows.h> // NOLINT
James Zern183b77d2013-07-30 22:46:58 -070035typedef HANDLE pthread_t;
36typedef CRITICAL_SECTION pthread_mutex_t;
37typedef struct {
38 HANDLE waiting_sem_;
39 HANDLE received_sem_;
40 HANDLE signal_event_;
41} pthread_cond_t;
42
Yunqing Wang903801f2013-12-27 15:25:54 -080043//------------------------------------------------------------------------------
44// simplistic pthread emulation layer
James Zern183b77d2013-07-30 22:46:58 -070045
Yunqing Wang903801f2013-12-27 15:25:54 -080046// _beginthreadex requires __stdcall
47#define THREADFN unsigned int __stdcall
48#define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
49
50static INLINE int pthread_create(pthread_t* const thread, const void* attr,
51 unsigned int (__stdcall *start)(void*),
52 void* arg) {
53 (void)attr;
54 *thread = (pthread_t)_beginthreadex(NULL, /* void *security */
55 0, /* unsigned stack_size */
56 start,
57 arg,
58 0, /* unsigned initflag */
59 NULL); /* unsigned *thrdaddr */
60 if (*thread == NULL) return 1;
61 SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
62 return 0;
63}
64
65static INLINE int pthread_join(pthread_t thread, void** value_ptr) {
66 (void)value_ptr;
67 return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 ||
68 CloseHandle(thread) == 0);
69}
70
71// Mutex
72static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
73 void* mutexattr) {
74 (void)mutexattr;
75 InitializeCriticalSection(mutex);
76 return 0;
77}
78
79static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
80 return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
81}
82
83static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
84 EnterCriticalSection(mutex);
85 return 0;
86}
87
88static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
89 LeaveCriticalSection(mutex);
90 return 0;
91}
92
93static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
94 DeleteCriticalSection(mutex);
95 return 0;
96}
97
98// Condition
99static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
100 int ok = 1;
101 ok &= (CloseHandle(condition->waiting_sem_) != 0);
102 ok &= (CloseHandle(condition->received_sem_) != 0);
103 ok &= (CloseHandle(condition->signal_event_) != 0);
104 return !ok;
105}
106
107static INLINE int pthread_cond_init(pthread_cond_t *const condition,
108 void* cond_attr) {
109 (void)cond_attr;
hkuangdd88f482015-02-05 13:59:15 -0800110 condition->waiting_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
111 condition->received_sem_ = CreateSemaphore(NULL, 0, MAX_DECODE_THREADS, NULL);
Yunqing Wang903801f2013-12-27 15:25:54 -0800112 condition->signal_event_ = CreateEvent(NULL, FALSE, FALSE, NULL);
113 if (condition->waiting_sem_ == NULL ||
114 condition->received_sem_ == NULL ||
115 condition->signal_event_ == NULL) {
116 pthread_cond_destroy(condition);
117 return 1;
118 }
119 return 0;
120}
121
122static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
123 int ok = 1;
124 if (WaitForSingleObject(condition->waiting_sem_, 0) == WAIT_OBJECT_0) {
125 // a thread is waiting in pthread_cond_wait: allow it to be notified
126 ok = SetEvent(condition->signal_event_);
127 // wait until the event is consumed so the signaler cannot consume
128 // the event via its own pthread_cond_wait.
129 ok &= (WaitForSingleObject(condition->received_sem_, INFINITE) !=
130 WAIT_OBJECT_0);
131 }
132 return !ok;
133}
134
135static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
136 pthread_mutex_t *const mutex) {
137 int ok;
138 // note that there is a consumer available so the signal isn't dropped in
139 // pthread_cond_signal
140 if (!ReleaseSemaphore(condition->waiting_sem_, 1, NULL))
141 return 1;
142 // now unlock the mutex so pthread_cond_signal may be issued
143 pthread_mutex_unlock(mutex);
144 ok = (WaitForSingleObject(condition->signal_event_, INFINITE) ==
145 WAIT_OBJECT_0);
146 ok &= ReleaseSemaphore(condition->received_sem_, 1, NULL);
147 pthread_mutex_lock(mutex);
148 return !ok;
149}
150#else // _WIN32
Jim Bankoskic52d8542013-10-01 15:19:39 -0700151#include <pthread.h> // NOLINT
Yunqing Wang903801f2013-12-27 15:25:54 -0800152# define THREADFN void*
153# define THREAD_RETURN(val) val
154#endif
James Zern183b77d2013-07-30 22:46:58 -0700155
Yunqing Wang903801f2013-12-27 15:25:54 -0800156#endif // CONFIG_MULTITHREAD
James Zern183b77d2013-07-30 22:46:58 -0700157
158// State of the worker thread object
159typedef enum {
160 NOT_OK = 0, // object is unusable
161 OK, // ready to work
162 WORK // busy finishing the current task
163} VP9WorkerStatus;
164
165// Function to be called by the worker thread. Takes two opaque pointers as
166// arguments (data1 and data2), and should return false in case of error.
167typedef int (*VP9WorkerHook)(void*, void*);
168
James Zerne656f442014-06-19 21:14:51 -0700169// Platform-dependent implementation details for the worker.
170typedef struct VP9WorkerImpl VP9WorkerImpl;
171
172// Synchronization object used to launch job in the worker thread
James Zern183b77d2013-07-30 22:46:58 -0700173typedef struct {
James Zerne656f442014-06-19 21:14:51 -0700174 VP9WorkerImpl *impl_;
James Zern183b77d2013-07-30 22:46:58 -0700175 VP9WorkerStatus status_;
176 VP9WorkerHook hook; // hook to call
James Zerne656f442014-06-19 21:14:51 -0700177 void *data1; // first argument passed to 'hook'
178 void *data2; // second argument passed to 'hook'
James Zern183b77d2013-07-30 22:46:58 -0700179 int had_error; // return value of the last call to 'hook'
180} VP9Worker;
181
James Zerne656f442014-06-19 21:14:51 -0700182// The interface for all thread-worker related functions. All these functions
183// must be implemented.
184typedef struct {
185 // Must be called first, before any other method.
186 void (*init)(VP9Worker *const worker);
187 // Must be called to initialize the object and spawn the thread. Re-entrant.
188 // Will potentially launch the thread. Returns false in case of error.
189 int (*reset)(VP9Worker *const worker);
190 // Makes sure the previous work is finished. Returns true if worker->had_error
191 // was not set and no error condition was triggered by the working thread.
192 int (*sync)(VP9Worker *const worker);
193 // Triggers the thread to call hook() with data1 and data2 arguments. These
194 // hook/data1/data2 values can be changed at any time before calling this
195 // function, but not be changed afterward until the next call to Sync().
196 void (*launch)(VP9Worker *const worker);
197 // This function is similar to launch() except that it calls the
198 // hook directly instead of using a thread. Convenient to bypass the thread
199 // mechanism while still using the VP9Worker structs. sync() must
200 // still be called afterward (for error reporting).
201 void (*execute)(VP9Worker *const worker);
202 // Kill the thread and terminate the object. To use the object again, one
203 // must call reset() again.
204 void (*end)(VP9Worker *const worker);
205} VP9WorkerInterface;
206
207// Install a new set of threading functions, overriding the defaults. This
208// should be done before any workers are started, i.e., before any encoding or
209// decoding takes place. The contents of the interface struct are copied, it
210// is safe to free the corresponding memory after this call. This function is
211// not thread-safe. Return false in case of invalid pointer or methods.
212int vp9_set_worker_interface(const VP9WorkerInterface *const winterface);
213
214// Retrieve the currently set thread worker interface.
215const VP9WorkerInterface *vp9_get_worker_interface(void);
James Zern183b77d2013-07-30 22:46:58 -0700216
217//------------------------------------------------------------------------------
218
James Zern40aa9102014-01-18 12:16:11 -0800219#ifdef __cplusplus
James Zern183b77d2013-07-30 22:46:58 -0700220} // extern "C"
221#endif
222
Jim Bankoskida17ffa2013-09-29 11:49:52 -0700223#endif // VP9_DECODER_VP9_THREAD_H_