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