/src/libhevc/common/ithread.c
Line | Count | Source (jump to first uncovered line) |
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 | 4.96k | { |
56 | 4.96k | return sizeof(pthread_t); |
57 | 4.96k | } |
58 | | |
59 | | UWORD32 ithread_get_mutex_lock_size(void) |
60 | 11.5k | { |
61 | 11.5k | return sizeof(pthread_mutex_t); |
62 | 11.5k | } |
63 | | |
64 | | WORD32 ithread_create(void *thread_handle, void *attribute, void *strt, void *argument) |
65 | 470 | { |
66 | 470 | return pthread_create((pthread_t *)thread_handle, attribute, (void * (*)(void *))strt, argument); |
67 | 470 | } |
68 | | |
69 | | WORD32 ithread_join(void *thread_handle, void **val_ptr) |
70 | 470 | { |
71 | 470 | pthread_t *pthread_handle = (pthread_t *)thread_handle; |
72 | 470 | return pthread_join(*pthread_handle, val_ptr); |
73 | 470 | } |
74 | | |
75 | | void ithread_exit(void *val_ptr) |
76 | 0 | { |
77 | 0 | return pthread_exit(val_ptr); |
78 | 0 | } |
79 | | |
80 | | WORD32 ithread_get_mutex_struct_size(void) |
81 | 0 | { |
82 | 0 | return (sizeof(pthread_mutex_t)); |
83 | 0 | } |
84 | | WORD32 ithread_mutex_init(void *mutex) |
85 | 11.0k | { |
86 | 11.0k | return pthread_mutex_init((pthread_mutex_t *)mutex, NULL); |
87 | 11.0k | } |
88 | | |
89 | | WORD32 ithread_mutex_destroy(void *mutex) |
90 | 11.0k | { |
91 | 11.0k | return pthread_mutex_destroy((pthread_mutex_t *)mutex); |
92 | 11.0k | } |
93 | | |
94 | | WORD32 ithread_mutex_lock(void *mutex) |
95 | 630k | { |
96 | 630k | return pthread_mutex_lock((pthread_mutex_t *)mutex); |
97 | 630k | } |
98 | | |
99 | | WORD32 ithread_mutex_unlock(void *mutex) |
100 | 631k | { |
101 | 631k | return pthread_mutex_unlock((pthread_mutex_t *)mutex); |
102 | 631k | } |
103 | | |
104 | | void ithread_yield(void) |
105 | 9.74M | { |
106 | 9.74M | sched_yield(); |
107 | 9.74M | } |
108 | | |
109 | | void ithread_sleep(UWORD32 u4_time) |
110 | 0 | { |
111 | 0 | usleep(u4_time * 1000 * 1000); |
112 | 0 | } |
113 | | |
114 | | void ithread_msleep(UWORD32 u4_time_ms) |
115 | 0 | { |
116 | 0 | usleep(u4_time_ms * 1000); |
117 | 0 | } |
118 | | |
119 | | void ithread_usleep(UWORD32 u4_time_us) |
120 | 0 | { |
121 | 0 | usleep(u4_time_us); |
122 | 0 | } |
123 | | |
124 | | UWORD32 ithread_get_sem_struct_size(void) |
125 | 0 | { |
126 | 0 | return (sizeof(sem_t)); |
127 | 0 | } |
128 | | |
129 | | WORD32 ithread_sem_init(void *sem, WORD32 pshared, UWORD32 value) |
130 | 0 | { |
131 | 0 | return sem_init((sem_t *)sem, pshared, value); |
132 | 0 | } |
133 | | |
134 | | WORD32 ithread_sem_post(void *sem) |
135 | 0 | { |
136 | 0 | return sem_post((sem_t *)sem); |
137 | 0 | } |
138 | | |
139 | | WORD32 ithread_sem_wait(void *sem) |
140 | 0 | { |
141 | 0 | return sem_wait((sem_t *)sem); |
142 | 0 | } |
143 | | |
144 | | WORD32 ithread_sem_destroy(void *sem) |
145 | 0 | { |
146 | 0 | return sem_destroy((sem_t *)sem); |
147 | 0 | } |
148 | | |
149 | | WORD32 ithread_set_affinity(WORD32 core_id) |
150 | 21.7k | { |
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 | 21.7k | return core_id; |
183 | 21.7k | } |
184 | | |
185 | | WORD32 ithread_get_cond_struct_size(void) |
186 | 4.96k | { |
187 | 4.96k | return (sizeof(pthread_cond_t)); |
188 | 4.96k | } |
189 | | |
190 | | WORD32 ithread_cond_init(void *cond) |
191 | 8.83k | { |
192 | 8.83k | return pthread_cond_init((pthread_cond_t *)cond, NULL); |
193 | 8.83k | } |
194 | | |
195 | | WORD32 ithread_cond_destroy(void *cond) |
196 | 8.83k | { |
197 | 8.83k | return pthread_cond_destroy((pthread_cond_t *)cond); |
198 | 8.83k | } |
199 | | |
200 | | WORD32 ithread_cond_wait(void *cond, void *mutex) |
201 | 9.31k | { |
202 | 9.31k | return pthread_cond_wait((pthread_cond_t *)cond, (pthread_mutex_t *)mutex); |
203 | 9.31k | } |
204 | | |
205 | | WORD32 ithread_cond_signal(void *cond) |
206 | 14.0k | { |
207 | 14.0k | return pthread_cond_signal((pthread_cond_t *)cond); |
208 | 14.0k | } |