History

re-structure SWelsDecoderContext and others and prepare for the support of multi-threading support.

xiaotiansf committed 7 years ago

Commit 2f5ecec1 parent 9ed97c729 files · +2285 −1568Browse files

Showing the changes to codec/decoder/core/src/wels_decoder_thread.cpp (1 of 29 files). Show all

Unified · Split

codec/decoder/core/src/wels_decoder_thread.cpp+317−0
@@ -0,0 +1,317 @@
1/*!
2 * \copy
3 * Copyright (c) 2009-2019, Cisco Systems
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * \file wels_decoder_thread.cpp
33 *
34 * \brief Interfaces introduced in thread programming
35 *
36 * \date 08/06/2018 Created
37 *
38 *************************************************************************************
39 */
40
41
42#ifdef __linux__
43#ifndef _GNU_SOURCE
44#define _GNU_SOURCE
45#endif
46#include <sched.h>
47#elif !defined(_WIN32) && !defined(__CYGWIN__)
48#include <sys/types.h>
49#include <sys/param.h>
50#include <unistd.h>
51#ifndef __Fuchsia__
52#include <sys/sysctl.h>
53#endif
54#ifdef __APPLE__
55#define HW_NCPU_NAME "hw.logicalcpu"
56#else
57#define HW_NCPU_NAME "hw.ncpu"
58#endif
59#endif
60#ifdef ANDROID_NDK
61#include <cpu-features.h>
62#endif
63#ifdef __ANDROID__
64#include <android/api-level.h>
65#endif
66
67#include "wels_decoder_thread.h"
68#include <stdio.h>
69#include <stdlib.h>
70
71int32_t GetCPUCount() {
72 WelsLogicalProcessInfo pInfo;
73 pInfo.ProcessorCount = 1;
74 WelsQueryLogicalProcessInfo (&pInfo);
75 return pInfo.ProcessorCount;
76}
77
78int ThreadCreate (SWelsDecThread* t, LPWELS_THREAD_ROUTINE tf, void* ta) {
79 WELS_THREAD_ATTR attr = 0;
80 return WelsThreadCreate (& (t->h), tf, ta, attr);
81}
82
83int ThreadWait (SWelsDecThread* t) {
84 return WelsThreadJoin (t->h);
85}
86
87#if defined(_WIN32) || defined(__CYGWIN__)
88
89int EventCreate (SWelsDecEvent* e, int manualReset, int initialState) {
90 e->h = CreateEvent (NULL, manualReset, initialState, NULL);
91 e->isSignaled = initialState;
92 return (e->h != NULL) ? 0 : 1;
93}
94
95void EventReset (SWelsDecEvent* e) {
96 ResetEvent (e->h);
97 e->isSignaled = 0;
98}
99
100void EventPost (SWelsDecEvent* e) {
101 SetEvent (e->h);
102 e->isSignaled = 1;
103}
104
105int EventWait (SWelsDecEvent* e, int32_t timeout) {
106 DWORD result;
107 if ((uint32_t)timeout == WELS_DEC_THREAD_WAIT_INFINITE || timeout < 0)
108 result = WaitForSingleObject (e->h, INFINITE);
109 else
110 result = WaitForSingleObject (e->h, timeout);
111
112 if (result == WAIT_OBJECT_0)
113 return WELS_DEC_THREAD_WAIT_SIGNALED;
114 else
115 return WAIT_TIMEOUT;
116}
117
118void EventDestroy (SWelsDecEvent* e) {
119 CloseHandle (e->h);
120 e->h = NULL;
121}
122
123int SemCreate (SWelsDecSemphore* s, long value, long max) {
124 s->h = CreateSemaphore (NULL, value, max, NULL);
125 return (s->h != NULL) ? 0 : 1;
126}
127
128int SemWait (SWelsDecSemphore* s, int32_t timeout) {
129 DWORD result;
130 if ((uint32_t)timeout == WELS_DEC_THREAD_WAIT_INFINITE || timeout < 0)
131 result = WaitForSingleObject (s->h, INFINITE);
132 else
133 result = WaitForSingleObject (s->h, timeout);
134
135 if (result == WAIT_OBJECT_0) {
136 return WELS_DEC_THREAD_WAIT_SIGNALED;
137 } else {
138 return WELS_DEC_THREAD_WAIT_TIMEDOUT;
139 }
140}
141
142void SemRelease (SWelsDecSemphore* s, long* prevcount) {
143 ReleaseSemaphore (s->h, 1, prevcount);
144}
145
146void SemDestroy (SWelsDecSemphore* s) {
147 CloseHandle (s->h);
148 s->h = NULL;
149}
150
151#else /* _WIN32 */
152
153static void getTimespecFromTimeout (struct timespec* ts, int32_t timeout) {
154 struct timeval tv;
155 gettimeofday (&tv, 0);
156 ts->tv_nsec = tv.tv_usec * 1000 + timeout * 1000000;
157 ts->tv_sec = tv.tv_sec + ts->tv_nsec / 1000000000;
158 ts->tv_nsec %= 1000000000;
159}
160int EventCreate (SWelsDecEvent* e, int manualReset, int initialState) {
161 if (pthread_mutex_init (& (e->m), NULL))
162 return 1;
163 if (pthread_cond_init (& (e->c), NULL))
164 return 2;
165
166 e->isSignaled = initialState;
167 e->manualReset = manualReset;
168
169 return 0;
170}
171
172void EventReset (SWelsDecEvent* e) {
173 pthread_mutex_lock (& (e->m));
174 e->isSignaled = 0;
175 pthread_mutex_unlock (& (e->m));
176}
177
178void EventPost (SWelsDecEvent* e) {
179 pthread_mutex_lock (& (e->m));
180 pthread_cond_broadcast (& (e->c));
181 e->isSignaled = 1;
182 pthread_mutex_unlock (& (e->m));
183}
184
185int EventWait (SWelsDecEvent* e, int32_t timeout) {
186 pthread_mutex_lock (& (e->m));
187 int signaled = e->isSignaled;
188 if (timeout == 0) {
189 pthread_mutex_unlock (& (e->m));
190 if (signaled)
191 return WELS_DEC_THREAD_WAIT_SIGNALED;
192 else
193 return WELS_DEC_THREAD_WAIT_TIMEDOUT;
194 }
195 if (signaled) {
196 if (!e->manualReset) {
197 e->isSignaled = 0;
198 }
199 pthread_mutex_unlock (& (e->m));
200 return WELS_DEC_THREAD_WAIT_SIGNALED;
201 }
202 int rc = 0;
203 if (timeout == WELS_DEC_THREAD_WAIT_INFINITE || timeout < 0) {
204 rc = pthread_cond_wait (& (e->c), & (e->m));
205 } else {
206 struct timespec ts;
207 getTimespecFromTimeout (&ts, timeout);
208 rc = pthread_cond_timedwait (& (e->c), & (e->m), &ts);
209 }
210 if (!e->manualReset) {
211 e->isSignaled = 0;
212 }
213 pthread_mutex_unlock (& (e->m));
214 if (rc == 0)
215 return WELS_DEC_THREAD_WAIT_SIGNALED;
216 else
217 return WELS_DEC_THREAD_WAIT_TIMEDOUT;
218}
219
220void EventDestroy (SWelsDecEvent* e) {
221 pthread_mutex_destroy (& (e->m));
222 pthread_cond_destroy (& (e->c));
223}
224
225int SemCreate (SWelsDecSemphore* s, long value, long max) {
226 s->v = value;
227 s->max = max;
228 if (pthread_mutex_init (& (s->m), NULL))
229 return 1;
230 const char* event_name = "";
231 if (WelsEventOpen (& (s->e), event_name)) {
232 return 2;
233 }
234 return 0;
235}
236
237int SemWait (SWelsDecSemphore* s, int32_t timeout) {
238#if defined(__APPLE__)
239 pthread_mutex_lock (& (s->m));
240#endif
241 int rc = 0;
242 if (timeout != 0) {
243 while ((s->v) == 0) {
244 if (timeout == WELS_DEC_THREAD_WAIT_INFINITE || timeout < 0) {
245 // infinite wait until released
246#if defined(__APPLE__)
247 rc = pthread_cond_wait (& (s->e), & (s->m));
248#else
249 rc = sem_wait (s->e);
250 if (rc != 0) rc = errno;
251#endif
252 } else {
253 struct timespec ts;
254 getTimespecFromTimeout (&ts, timeout);
255#if defined(__APPLE__)
256 rc = pthread_cond_timedwait (& (s->e), & (s->m), &ts);
257#else
258 rc = sem_timedwait (s->e, &ts);
259 if (rc != 0) rc = errno;
260#endif
261 if (rc != EINTR) {
262 // if timed out we return to the caller
263 break;
264 }
265 }
266 }
267 // only decrement counter if semaphore was signaled
268 if (rc == 0)
269 s->v -= 1;
270
271 } else {
272 // Special handling for timeout of 0
273 if (s->v > 0) {
274 s->v -= 1;
275 rc = 0;
276 } else {
277 rc = 1;
278 }
279 }
280#if defined(__APPLE__)
281 pthread_mutex_unlock (& (s->m));
282#endif
283 // set return value
284 if (rc == 0)
285 return WELS_DEC_THREAD_WAIT_SIGNALED;
286 else
287 return WELS_DEC_THREAD_WAIT_TIMEDOUT;
288}
289
290void SemRelease (SWelsDecSemphore* s, long* o_pPrevCount) {
291 long prevcount;
292#ifdef __APPLE__
293 pthread_mutex_lock (& (s->m));
294 prevcount = s->v;
295 if (s->v < s->max)
296 s->v += 1;
297 pthread_cond_signal (& (s->e));
298 pthread_mutex_unlock (& (s->m));
299#else
300 prevcount = s->v;
301 if (s->v < s->max)
302 s->v += 1;
303 sem_post (s->e);
304#endif
305 if (o_pPrevCount != NULL) {
306 *o_pPrevCount = prevcount;
307 }
308}
309
310void SemDestroy (SWelsDecSemphore* s) {
311 pthread_mutex_destroy (& (s->m));
312 const char* event_name = "";
313 WelsEventClose (& (s->e), event_name);
314}
315
316#endif /* !_WIN32 */
317

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t