• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h
33  *
34  * \brief   Interfaces introduced in thread programming
35  *
36  * \date    08/06/2018 Created
37  *
38  *************************************************************************************
39  */
40 
41 #ifndef   _WELS_DECODER_THREAD_H_
42 #define   _WELS_DECODER_THREAD_H_
43 
44 #include "WelsThreadLib.h"
45 
46 #ifdef  __cplusplus
47 extern "C" {
48 #endif
49 
50 #define WELS_DEC_MAX_NUM_CPU 16
51 #define WELS_DEC_MAX_THREAD_STACK_SIZE   4096
52 #define WELS_DEC_THREAD_COMMAND_RUN 0
53 #define WELS_DEC_THREAD_COMMAND_ABORT 1
54 
55 #if defined(_WIN32) || defined(__CYGWIN__)
56 typedef struct tagWelsDecSemphore {
57   WELS_THREAD_HANDLE h;
58 } SWelsDecSemphore;
59 
60 typedef struct tagWelsDecEvent {
61   WELS_THREAD_HANDLE h;
62   int isSignaled;
63 } SWelsDecEvent;
64 
65 typedef struct tagWelsDecThread {
66   WELS_THREAD_HANDLE h;
67 } SWelsDecThread;
68 
69 #define WelsDecThreadFunc(fn,a) DWORD WINAPI fn(LPVOID a)
70 #define WelsDecThreadFuncArg(a) LPWELS_THREAD_ROUTINE a
71 #define WELS_DEC_THREAD_WAIT_TIMEDOUT    WAIT_TIMEOUT
72 #define WELS_DEC_THREAD_WAIT_SIGNALED    WAIT_OBJECT_0
73 #define WELS_DEC_THREAD_WAIT_INFINITE    INFINITE
74 
75 #else // NON-WINDOWS
76 
77 typedef   pthread_mutexattr_t       WELS_MUTEX_ATTR;
78 
79 typedef struct tagWelsDecSemphore {
80   long max;
81   long v;
82   WELS_EVENT  e;
83   WELS_MUTEX  m;
84 } SWelsDecSemphore;
85 
86 typedef struct tagWelsDecEvent {
87   int manualReset;
88   int isSignaled;
89   pthread_cond_t c;
90   WELS_MUTEX m;
91 } SWelsDecEvent;
92 
93 typedef struct tagWelsDecThread {
94   WELS_THREAD_HANDLE h;
95 } SWelsDecThread;
96 
97 #define WelsDecThreadFunc(fn,a) void* fn(void* a)
98 #define WelsDecThreadFuncArg(a) void* (*a)(void*)
99 
100 #define WELS_DEC_THREAD_WAIT_TIMEDOUT    ETIMEDOUT
101 #define WELS_DEC_THREAD_WAIT_SIGNALED    EINTR
102 #define WELS_DEC_THREAD_WAIT_INFINITE    -1
103 
104 #endif//_WIN32
105 
106 #define WelsDecThreadReturn   WELS_THREAD_ROUTINE_RETURN(0);
107 
108 int32_t GetCPUCount();
109 
110 // Event
111 int EventCreate (SWelsDecEvent* e, int manualReset, int initialState);
112 void EventPost (SWelsDecEvent* e);
113 int EventWait (SWelsDecEvent* e, int32_t timeout);
114 void EventReset (SWelsDecEvent* e);
115 void EventDestroy (SWelsDecEvent* e);
116 
117 // Semaphore
118 int SemCreate (SWelsDecSemphore* s, long value, long max);
119 int SemWait (SWelsDecSemphore* s, int32_t timeout);
120 void SemRelease (SWelsDecSemphore* s, long* prev_count);
121 void SemDestroy (SWelsDecSemphore* s);
122 
123 // Thread
124 int ThreadCreate (SWelsDecThread* t, LPWELS_THREAD_ROUTINE tf, void* ta);
125 int ThreadWait (SWelsDecThread* t);
126 
127 #define DECLARE_PROCTHREAD(name, argument) \
128   WelsDecThreadFunc(name,argument)
129 
130 #define DECLARE_PROCTHREAD_PTR(name) \
131   LPWELS_THREAD_ROUTINE name
132 
133 #define CREATE_THREAD(ph, threadproc,argument) \
134   ThreadCreate(ph, threadproc, (void*)argument)
135 
136 #define CREATE_EVENT(ph, manualreset,initial_state,name) \
137   EventCreate(ph,(int)(manualreset),(int)(initial_state))
138 
139 #define CREATE_SEMAPHORE(ph, initial_count,max_count, name) \
140   SemCreate(ph, (long)initial_count,(long)(max_count))
141 
142 #define CLOSE_EVENT(ph) \
143   EventDestroy(ph)
144 
145 #define CLOSE_SEMAPHORE(ph) \
146   SemDestroy(ph)
147 
148 #define SET_EVENT(ph) \
149   EventPost(ph)
150 
151 #define RESET_EVENT(ph) \
152   EventReset(ph)
153 
154 #define RELEASE_SEMAPHORE(ph) \
155   SemRelease(ph,NULL)
156 
157 #define WAIT_EVENT(ph,timeout) \
158   EventWait(ph, (int32_t)timeout)
159 
160 #define WAIT_THREAD(ph) \
161   ThreadWait(ph)
162 
163 #define WAIT_SEMAPHORE(ph,timeout) \
164   SemWait(ph,(int32_t)timeout)
165 
166 #ifdef  __cplusplus
167 }
168 #endif
169 
170 #endif
171