/src/libavc/common/ithread.c
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Copyright (C) 2015 The Android Open Source Project |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at: |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | * |
17 | | ***************************************************************************** |
18 | | * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore |
19 | | */ |
20 | | /*****************************************************************************/ |
21 | | /* */ |
22 | | /* File Name : ithread.c */ |
23 | | /* */ |
24 | | /* Description : Contains abstraction for threads, mutex and semaphores*/ |
25 | | /* */ |
26 | | /* List of Functions : */ |
27 | | /* */ |
28 | | /* Issues / Problems : None */ |
29 | | /* */ |
30 | | /* Revision History : */ |
31 | | /* */ |
32 | | /* DD MM YYYY Author(s) Changes */ |
33 | | /* 07 09 2012 Harish Initial Version */ |
34 | | /*****************************************************************************/ |
35 | | /*****************************************************************************/ |
36 | | /* File Includes */ |
37 | | /*****************************************************************************/ |
38 | | #include <string.h> |
39 | | #include "ih264_typedefs.h" |
40 | | |
41 | | |
42 | | |
43 | | #include "ithread.h" |
44 | | #include <sys/types.h> |
45 | | |
46 | | |
47 | 74.1k | #define UNUSED(x) ((void)(x)) |
48 | | |
49 | | //#define PTHREAD_AFFINITY |
50 | | //#define SYSCALL_AFFINITY |
51 | | |
52 | | #ifdef PTHREAD_AFFINITY |
53 | | #define _GNU_SOURCE |
54 | | #define __USE_GNU |
55 | | #endif |
56 | | |
57 | | #include <pthread.h> |
58 | | #include <sched.h> |
59 | | #include <semaphore.h> |
60 | | #include <unistd.h> |
61 | | #ifdef PTHREAD_AFFINITY |
62 | | #include <sys/prctl.h> |
63 | | #endif |
64 | | |
65 | | |
66 | | UWORD32 ithread_get_handle_size(void) |
67 | 3.20k | { |
68 | 3.20k | return sizeof(pthread_t); |
69 | 3.20k | } |
70 | | |
71 | | UWORD32 ithread_get_mutex_lock_size(void) |
72 | 28.0k | { |
73 | 28.0k | return sizeof(pthread_mutex_t); |
74 | 28.0k | } |
75 | | |
76 | | |
77 | | WORD32 ithread_create(void *thread_handle, void *attribute, void *strt, void *argument) |
78 | 1.29k | { |
79 | 1.29k | UNUSED(attribute); |
80 | 1.29k | return pthread_create((pthread_t *)thread_handle, NULL,(void *(*)(void *)) strt, argument); |
81 | 1.29k | } |
82 | | |
83 | | WORD32 ithread_join(void *thread_handle, void ** val_ptr) |
84 | 1.29k | { |
85 | 1.29k | pthread_t *pthread_handle = (pthread_t *)thread_handle; |
86 | 1.29k | UNUSED(val_ptr); |
87 | 1.29k | return pthread_join(*pthread_handle, NULL); |
88 | 1.29k | } |
89 | | |
90 | | WORD32 ithread_get_mutex_struct_size(void) |
91 | 0 | { |
92 | 0 | return(sizeof(pthread_mutex_t)); |
93 | 0 | } |
94 | | WORD32 ithread_mutex_init(void *mutex) |
95 | 13.1k | { |
96 | 13.1k | return pthread_mutex_init((pthread_mutex_t *) mutex, NULL); |
97 | 13.1k | } |
98 | | |
99 | | WORD32 ithread_mutex_destroy(void *mutex) |
100 | 6.41k | { |
101 | 6.41k | return pthread_mutex_destroy((pthread_mutex_t *) mutex); |
102 | 6.41k | } |
103 | | |
104 | | WORD32 ithread_mutex_lock(void *mutex) |
105 | 620k | { |
106 | 620k | return pthread_mutex_lock((pthread_mutex_t *)mutex); |
107 | 620k | } |
108 | | |
109 | | WORD32 ithread_mutex_unlock(void *mutex) |
110 | 620k | { |
111 | 620k | return pthread_mutex_unlock((pthread_mutex_t *)mutex); |
112 | 620k | } |
113 | | |
114 | | void ithread_yield(void) |
115 | 9.38M | { |
116 | 9.38M | sched_yield(); |
117 | 9.38M | } |
118 | | |
119 | | void ithread_sleep(UWORD32 u4_time) |
120 | 0 | { |
121 | 0 | usleep(u4_time * 1000 * 1000); |
122 | 0 | } |
123 | | |
124 | | void ithread_msleep(UWORD32 u4_time_ms) |
125 | 0 | { |
126 | 0 | usleep(u4_time_ms * 1000); |
127 | 0 | } |
128 | | |
129 | | void ithread_usleep(UWORD32 u4_time_us) |
130 | 0 | { |
131 | 0 | usleep(u4_time_us); |
132 | 0 | } |
133 | | |
134 | | UWORD32 ithread_get_sem_struct_size(void) |
135 | 0 | { |
136 | 0 | return(sizeof(sem_t)); |
137 | 0 | } |
138 | | |
139 | | |
140 | | WORD32 ithread_sem_init(void *sem,WORD32 pshared,UWORD32 value) |
141 | 0 | { |
142 | 0 | return sem_init((sem_t *)sem,pshared,value); |
143 | 0 | } |
144 | | |
145 | | WORD32 ithread_sem_post(void *sem) |
146 | 0 | { |
147 | 0 | return sem_post((sem_t *)sem); |
148 | 0 | } |
149 | | |
150 | | |
151 | | WORD32 ithread_sem_wait(void *sem) |
152 | 0 | { |
153 | 0 | return sem_wait((sem_t *)sem); |
154 | 0 | } |
155 | | |
156 | | |
157 | | WORD32 ithread_sem_destroy(void *sem) |
158 | 0 | { |
159 | 0 | return sem_destroy((sem_t *)sem); |
160 | 0 | } |
161 | | |
162 | | void ithread_set_name(CHAR *pc_thread_name) |
163 | 71.5k | { |
164 | | |
165 | 71.5k | #ifndef WIN32 |
166 | 71.5k | #ifndef QNX |
167 | 71.5k | #ifndef IOS |
168 | 71.5k | UNUSED(pc_thread_name); |
169 | | //prctl(PR_SET_NAME, (unsigned long)pu1_thread_name, 0, 0, 0); |
170 | 71.5k | #endif |
171 | 71.5k | #endif |
172 | 71.5k | #endif |
173 | | |
174 | 71.5k | } |
175 | | WORD32 ithread_set_affinity(WORD32 core_id) |
176 | 0 | { |
177 | | #ifdef PTHREAD_AFFINITY |
178 | | cpu_set_t cpuset; |
179 | | int num_cores = sysconf(_SC_NPROCESSORS_ONLN); |
180 | | pthread_t cur_thread = pthread_self(); |
181 | | |
182 | | if (core_id >= num_cores) |
183 | | return -1; |
184 | | |
185 | | CPU_ZERO(&cpuset); |
186 | | CPU_SET(core_id, &cpuset); |
187 | | |
188 | | return pthread_setaffinity_np(cur_thread, sizeof(cpu_set_t), &cpuset); |
189 | | |
190 | | #elif SYSCALL_AFFINITY |
191 | | WORD32 i4_sys_res; |
192 | | UNUSED(core_id); |
193 | | |
194 | | pid_t pid = gettid(); |
195 | | |
196 | | |
197 | | i4_sys_res = syscall(__NR_sched_setaffinity, pid, sizeof(i4_mask), &i4_mask); |
198 | | if (i4_sys_res) |
199 | | { |
200 | | //WORD32 err; |
201 | | //err = errno; |
202 | | //perror("Error in setaffinity syscall PERROR : "); |
203 | | //LOG_ERROR("Error in the syscall setaffinity: mask=0x%x err=0x%x", i4_mask, i4_sys_res); |
204 | | return -1; |
205 | | } |
206 | | #else |
207 | 0 | UNUSED(core_id); |
208 | 0 | #endif |
209 | 0 | return 1; |
210 | |
|
211 | 0 | } |
212 | | |
213 | | WORD32 ithread_get_cond_struct_size(void) |
214 | 4.80k | { |
215 | 4.80k | return (sizeof(pthread_cond_t)); |
216 | 4.80k | } |
217 | | |
218 | | WORD32 ithread_cond_init(void *cond) |
219 | 6.41k | { |
220 | 6.41k | return pthread_cond_init((pthread_cond_t *)cond, NULL); |
221 | 6.41k | } |
222 | | |
223 | | WORD32 ithread_cond_destroy(void *cond) |
224 | 6.41k | { |
225 | 6.41k | return pthread_cond_destroy((pthread_cond_t *)cond); |
226 | 6.41k | } |
227 | | |
228 | | WORD32 ithread_cond_wait(void *cond, void *mutex) |
229 | 42.3k | { |
230 | 42.3k | return pthread_cond_wait((pthread_cond_t *)cond, (pthread_mutex_t *)mutex); |
231 | 42.3k | } |
232 | | |
233 | | WORD32 ithread_cond_signal(void *cond) |
234 | 51.7k | { |
235 | 51.7k | return pthread_cond_signal((pthread_cond_t *)cond); |
236 | 51.7k | } |
237 | | |
238 | | UWORD32 ithread_get_cond_size(void) |
239 | 0 | { |
240 | 0 | return sizeof(pthread_cond_t); |
241 | 0 | } |
242 | | |
243 | | WORD32 ithread_cond_broadcast(void *cond) |
244 | 0 | { |
245 | 0 | return pthread_cond_broadcast((pthread_cond_t *)cond); |
246 | 0 | } |