1 /****************************************************************************** 2 * 3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore 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 /*****************************************************************************/ 19 /* */ 20 /* File Name : ithread.c */ 21 /* */ 22 /* Description : Contains abstraction for threads, mutex and semaphores*/ 23 /* */ 24 /* List of Functions : */ 25 /* */ 26 /* Issues / Problems : None */ 27 /* */ 28 /* Revision History : */ 29 /* */ 30 /* DD MM YYYY Author(s) Changes */ 31 /* 07 09 2012 Harish Initial Version */ 32 /*****************************************************************************/ 33 /*****************************************************************************/ 34 /* File Includes */ 35 /*****************************************************************************/ 36 #include <string.h> 37 #include "ihevc_typedefs.h" 38 #include "ithread.h" 39 #include <sys/types.h> 40 41 //#define PTHREAD_AFFINITY 42 //#define SYSCALL_AFFINITY 43 44 #ifdef PTHREAD_AFFINITY 45 #define _GNU_SOURCE 46 #define __USE_GNU 47 #endif 48 49 #include <pthread.h> 50 #include <sched.h> 51 #include <semaphore.h> 52 #include <unistd.h> 53 54 UWORD32 ithread_get_handle_size(void) 55 { 56 return sizeof(pthread_t); 57 } 58 59 UWORD32 ithread_get_mutex_lock_size(void) 60 { 61 return sizeof(pthread_mutex_t); 62 } 63 64 WORD32 ithread_create(void *thread_handle, void *attribute, void *strt, void *argument) 65 { 66 return pthread_create((pthread_t *)thread_handle, attribute, (void * (*)(void *))strt, argument); 67 } 68 69 WORD32 ithread_join(void *thread_handle, void **val_ptr) 70 { 71 pthread_t *pthread_handle = (pthread_t *)thread_handle; 72 return pthread_join(*pthread_handle, val_ptr); 73 } 74 75 void ithread_exit(void *val_ptr) 76 { 77 return pthread_exit(val_ptr); 78 } 79 80 WORD32 ithread_get_mutex_struct_size(void) 81 { 82 return (sizeof(pthread_mutex_t)); 83 } 84 WORD32 ithread_mutex_init(void *mutex) 85 { 86 return pthread_mutex_init((pthread_mutex_t *)mutex, NULL); 87 } 88 89 WORD32 ithread_mutex_destroy(void *mutex) 90 { 91 return pthread_mutex_destroy((pthread_mutex_t *)mutex); 92 } 93 94 WORD32 ithread_mutex_lock(void *mutex) 95 { 96 return pthread_mutex_lock((pthread_mutex_t *)mutex); 97 } 98 99 WORD32 ithread_mutex_unlock(void *mutex) 100 { 101 return pthread_mutex_unlock((pthread_mutex_t *)mutex); 102 } 103 104 void ithread_yield(void) 105 { 106 sched_yield(); 107 } 108 109 void ithread_sleep(UWORD32 u4_time) 110 { 111 usleep(u4_time * 1000 * 1000); 112 } 113 114 void ithread_msleep(UWORD32 u4_time_ms) 115 { 116 usleep(u4_time_ms * 1000); 117 } 118 119 void ithread_usleep(UWORD32 u4_time_us) 120 { 121 usleep(u4_time_us); 122 } 123 124 UWORD32 ithread_get_sem_struct_size(void) 125 { 126 return (sizeof(sem_t)); 127 } 128 129 WORD32 ithread_sem_init(void *sem, WORD32 pshared, UWORD32 value) 130 { 131 return sem_init((sem_t *)sem, pshared, value); 132 } 133 134 WORD32 ithread_sem_post(void *sem) 135 { 136 return sem_post((sem_t *)sem); 137 } 138 139 WORD32 ithread_sem_wait(void *sem) 140 { 141 return sem_wait((sem_t *)sem); 142 } 143 144 WORD32 ithread_sem_destroy(void *sem) 145 { 146 return sem_destroy((sem_t *)sem); 147 } 148 149 WORD32 ithread_set_affinity(WORD32 core_id) 150 { 151 152 #ifdef PTHREAD_AFFINITY 153 cpu_set_t cpuset; 154 int num_cores = sysconf(_SC_NPROCESSORS_ONLN); 155 pthread_t cur_thread = pthread_self(); 156 157 if(core_id >= num_cores) 158 return -1; 159 160 CPU_ZERO(&cpuset); 161 CPU_SET(core_id, &cpuset); 162 163 return pthread_setaffinity_np(cur_thread, sizeof(cpu_set_t), &cpuset); 164 165 #elif SYSCALL_AFFINITY 166 WORD32 i4_sys_res; 167 168 pid_t pid = gettid(); 169 170 171 i4_sys_res = syscall(__NR_sched_setaffinity, pid, sizeof(i4_mask), &i4_mask); 172 if(i4_sys_res) 173 { 174 //WORD32 err; 175 //err = errno; 176 //perror("Error in setaffinity syscall PERROR : "); 177 //LOG_ERROR("Error in the syscall setaffinity: mask=0x%x err=0x%x", i4_mask, i4_sys_res); 178 return -1; 179 } 180 #endif 181 182 return core_id; 183 } 184