1 /*
2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "usr_lib_ops.h"
32 #include "pthread.h"
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 const static UsrLibcOps *g_usrLibc = NULL;
39
pthread_attr_init(pthread_attr_t * attr)40 int pthread_attr_init(pthread_attr_t *attr)
41 {
42 return g_usrLibc->pthread_attr_init(attr);
43 }
44
pthread_attr_destroy(pthread_attr_t * attr)45 int pthread_attr_destroy(pthread_attr_t *attr)
46 {
47 return g_usrLibc->pthread_attr_destroy(attr);
48 }
49
pthread_attr_setdetachstate(pthread_attr_t * attr,int detachState)50 int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachState)
51 {
52 return g_usrLibc->pthread_attr_setdetachstate(attr, detachState);
53 }
54
pthread_attr_getdetachstate(const pthread_attr_t * attr,int * detachState)55 int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachState)
56 {
57 return g_usrLibc->pthread_attr_getdetachstate(attr, detachState);
58 }
59
pthread_attr_setscope(pthread_attr_t * attr,int scope)60 int pthread_attr_setscope(pthread_attr_t *attr, int scope)
61 {
62 return g_usrLibc->pthread_attr_setscope(attr, scope);
63 }
64
pthread_attr_getscope(const pthread_attr_t * attr,int * scope)65 int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
66 {
67 return g_usrLibc->pthread_attr_getscope(attr, scope);
68 }
69
pthread_attr_setinheritsched(pthread_attr_t * attr,int inherit)70 int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit)
71 {
72 return g_usrLibc->pthread_attr_setinheritsched(attr, inherit);
73 }
74
pthread_attr_getinheritsched(const pthread_attr_t * attr,int * inherit)75 int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inherit)
76 {
77 return g_usrLibc->pthread_attr_getinheritsched(attr, inherit);
78 }
79
pthread_attr_setschedpolicy(pthread_attr_t * attr,int policy)80 int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
81 {
82 return g_usrLibc->pthread_attr_setschedpolicy(attr, policy);
83 }
84
pthread_attr_getschedpolicy(const pthread_attr_t * attr,int * policy)85 int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
86 {
87 return g_usrLibc->pthread_attr_getschedpolicy(attr, policy);
88 }
89
pthread_attr_setschedparam(pthread_attr_t * attr,const struct sched_param * param)90 int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
91 {
92 return g_usrLibc->pthread_attr_setschedparam(attr, param);
93 }
94
pthread_attr_getschedparam(const pthread_attr_t * attr,struct sched_param * param)95 int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
96 {
97 return g_usrLibc->pthread_attr_getschedparam(attr, param);
98 }
99
pthread_attr_setstack(pthread_attr_t * a,void * addr,size_t size)100 int pthread_attr_setstack(pthread_attr_t *a, void *addr, size_t size)
101 {
102 return g_usrLibc->pthread_attr_setstack(a, addr, size);
103 }
104
pthread_attr_getstack(const pthread_attr_t * restrict a,void ** restrict addr,size_t * restrict size)105 int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr, size_t *restrict size)
106 {
107 return g_usrLibc->pthread_attr_getstack(a, addr, size);
108 }
109
pthread_attr_setstacksize(pthread_attr_t * attr,size_t stackSize)110 int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stackSize)
111 {
112 return g_usrLibc->pthread_attr_setstacksize(attr, stackSize);
113 }
114
pthread_attr_getstacksize(const pthread_attr_t * attr,size_t * stackSize)115 int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stackSize)
116 {
117 return g_usrLibc->pthread_attr_getstacksize(attr, stackSize);
118 }
119
sched_get_priority_min(int policy)120 int sched_get_priority_min(int policy)
121 {
122 return g_usrLibc->sched_get_priority_min(policy);
123 }
124
sched_get_priority_max(int policy)125 int sched_get_priority_max(int policy)
126 {
127 return g_usrLibc->sched_get_priority_max(policy);
128 }
129
pthread_create(pthread_t * thread,const pthread_attr_t * attr,void * (* startRoutine)(void *),void * arg)130 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
131 void *(*startRoutine)(void *), void *arg)
132 {
133 return g_usrLibc->pthread_create(thread, attr, startRoutine, arg);
134 }
135
pthread_setschedparam(pthread_t thread,int policy,const struct sched_param * param)136 int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
137 {
138 return g_usrLibc->pthread_setschedparam(thread, policy, param);
139 }
140
pthread_getschedparam(pthread_t thread,int * policy,struct sched_param * param)141 int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
142 {
143 return g_usrLibc->pthread_getschedparam(thread, policy, param);
144 }
145
pthread_self(void)146 pthread_t pthread_self(void)
147 {
148 return g_usrLibc->pthread_self();
149 }
150
pthread_cancel(pthread_t thread)151 int pthread_cancel(pthread_t thread)
152 {
153 return g_usrLibc->pthread_cancel(thread);
154 }
155
pthread_join(pthread_t thread,void ** retval)156 int pthread_join(pthread_t thread, void **retval)
157 {
158 return g_usrLibc->pthread_join(thread, retval);
159 }
160
pthread_detach(pthread_t thread)161 int pthread_detach(pthread_t thread)
162 {
163 return g_usrLibc->pthread_detach(thread);
164 }
165
pthread_exit(void * retVal)166 void pthread_exit(void *retVal)
167 {
168 g_usrLibc->pthread_exit(retVal);
169 }
170
pthread_setname_np(pthread_t thread,const char * name)171 int pthread_setname_np(pthread_t thread, const char *name)
172 {
173 return g_usrLibc->pthread_setname_np(thread, name);
174 }
175
pthread_getname_np(pthread_t thread,char * buf,size_t buflen)176 int pthread_getname_np(pthread_t thread, char *buf, size_t buflen)
177 {
178 return g_usrLibc->pthread_getname_np(thread, buf, buflen);
179 }
180
pthread_mutexattr_init(pthread_mutexattr_t * mutexAttr)181 int pthread_mutexattr_init(pthread_mutexattr_t *mutexAttr)
182 {
183 return g_usrLibc->pthread_mutexattr_init(mutexAttr);
184 }
185
pthread_mutexattr_settype(pthread_mutexattr_t * mutexAttr,int type)186 int pthread_mutexattr_settype(pthread_mutexattr_t *mutexAttr, int type)
187 {
188 return g_usrLibc->pthread_mutexattr_settype(mutexAttr, type);
189 }
190
pthread_mutexattr_destroy(pthread_mutexattr_t * mutexAttr)191 int pthread_mutexattr_destroy(pthread_mutexattr_t *mutexAttr)
192 {
193 return g_usrLibc->pthread_mutexattr_destroy(mutexAttr);
194 }
195
pthread_mutex_init(pthread_mutex_t * mutex,const pthread_mutexattr_t * mutexAttr)196 int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexAttr)
197 {
198 return g_usrLibc->pthread_mutex_init(mutex, mutexAttr);
199 }
200
pthread_mutex_destroy(pthread_mutex_t * mutex)201 int pthread_mutex_destroy(pthread_mutex_t *mutex)
202 {
203 return g_usrLibc->pthread_mutex_destroy(mutex);
204 }
205
pthread_mutex_timedlock(pthread_mutex_t * mutex,const struct timespec * absTimeout)206 int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *absTimeout)
207 {
208 return g_usrLibc->pthread_mutex_timedlock(mutex, absTimeout);
209 }
210
pthread_mutex_lock(pthread_mutex_t * mutex)211 int pthread_mutex_lock(pthread_mutex_t *mutex)
212 {
213 return g_usrLibc->pthread_mutex_lock(mutex);
214 }
215
pthread_mutex_trylock(pthread_mutex_t * mutex)216 int pthread_mutex_trylock(pthread_mutex_t *mutex)
217 {
218 return g_usrLibc->pthread_mutex_trylock(mutex);
219 }
220
pthread_mutex_unlock(pthread_mutex_t * mutex)221 int pthread_mutex_unlock(pthread_mutex_t *mutex)
222 {
223 return g_usrLibc->pthread_mutex_unlock(mutex);
224 }
225
pthread_condattr_init(pthread_condattr_t * attr)226 int pthread_condattr_init(pthread_condattr_t *attr)
227 {
228 return g_usrLibc->pthread_condattr_init(attr);
229 }
230
pthread_condattr_destroy(pthread_condattr_t * attr)231 int pthread_condattr_destroy(pthread_condattr_t *attr)
232 {
233 return g_usrLibc->pthread_condattr_destroy(attr);
234 }
235
pthread_condattr_setclock(pthread_condattr_t * attr,clockid_t clk)236 int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clk)
237 {
238 return g_usrLibc->pthread_condattr_setclock(attr, clk);
239 }
240
pthread_cond_init(pthread_cond_t * cond,const pthread_condattr_t * attr)241 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
242 {
243 return g_usrLibc->pthread_cond_init(cond, attr);
244 }
245
pthread_cond_destroy(pthread_cond_t * cond)246 int pthread_cond_destroy(pthread_cond_t *cond)
247 {
248 return g_usrLibc->pthread_cond_destroy(cond);
249 }
250
pthread_cond_broadcast(pthread_cond_t * cond)251 int pthread_cond_broadcast(pthread_cond_t *cond)
252 {
253 return g_usrLibc->pthread_cond_broadcast(cond);
254 }
255
pthread_cond_signal(pthread_cond_t * cond)256 int pthread_cond_signal(pthread_cond_t *cond)
257 {
258 return g_usrLibc->pthread_cond_signal(cond);
259 }
260
pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec * ts)261 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *ts)
262 {
263 return g_usrLibc->pthread_cond_timedwait(cond, mutex, ts);
264 }
265
pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)266 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
267 {
268 return g_usrLibc->pthread_cond_wait(cond, mutex);
269 }
270
sem_init(sem_t * sem,int shared,unsigned int value)271 int sem_init(sem_t *sem, int shared, unsigned int value)
272 {
273 return g_usrLibc->sem_init(sem, shared, value);
274 }
275
sem_destroy(sem_t * sem)276 int sem_destroy(sem_t *sem)
277 {
278 return g_usrLibc->sem_destroy(sem);
279 }
280
sem_wait(sem_t * sem)281 int sem_wait(sem_t *sem)
282 {
283 return g_usrLibc->sem_wait(sem);
284 }
285
sem_post(sem_t * sem)286 int sem_post(sem_t *sem)
287 {
288 return g_usrLibc->sem_post(sem);
289 }
290
sem_timedwait(sem_t * sem,const struct timespec * timeout)291 int sem_timedwait(sem_t *sem, const struct timespec *timeout)
292 {
293 return g_usrLibc->sem_timedwait(sem, timeout);
294 }
295
sem_getvalue(sem_t * sem,int * currVal)296 int sem_getvalue(sem_t *sem, int *currVal)
297 {
298 return g_usrLibc->sem_getvalue(sem, currVal);
299 }
300
nanosleep(const struct timespec * rqtp,struct timespec * rmtp)301 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
302 {
303 return g_usrLibc->nanosleep(rqtp, rmtp);
304 }
305
timer_create(clockid_t clockID,struct sigevent * restrict evp,timer_t * restrict timerID)306 int timer_create(clockid_t clockID, struct sigevent *restrict evp, timer_t *restrict timerID)
307 {
308 return g_usrLibc->timer_create(clockID, evp, timerID);
309 }
310
timer_delete(timer_t timerID)311 int timer_delete(timer_t timerID)
312 {
313 return g_usrLibc->timer_delete(timerID);
314 }
315
timer_settime(timer_t timerID,int flags,const struct itimerspec * restrict value,struct itimerspec * restrict oldValue)316 int timer_settime(timer_t timerID, int flags, const struct itimerspec *restrict value, struct itimerspec *restrict oldValue)
317 {
318 return g_usrLibc->timer_settime(timerID, flags, value, oldValue);
319 }
320
timer_gettime(timer_t timerID,struct itimerspec * value)321 int timer_gettime(timer_t timerID, struct itimerspec *value)
322 {
323 return g_usrLibc->timer_gettime(timerID, value);
324 }
325
timer_getoverrun(timer_t timerID)326 int timer_getoverrun(timer_t timerID)
327 {
328 return g_usrLibc->timer_getoverrun(timerID);
329 }
330
clock_settime(clockid_t clockID,const struct timespec * tp)331 int clock_settime(clockid_t clockID, const struct timespec *tp)
332 {
333 return g_usrLibc->clock_settime(clockID, tp);
334 }
335
clock_gettime(clockid_t clockID,struct timespec * tp)336 int clock_gettime(clockid_t clockID, struct timespec *tp)
337 {
338 return g_usrLibc->clock_gettime(clockID, tp);
339 }
340
clock_getres(clockid_t clockID,struct timespec * tp)341 int clock_getres(clockid_t clockID, struct timespec *tp)
342 {
343 return g_usrLibc->clock_getres(clockID, tp);
344 }
345
clock_nanosleep(clockid_t clk,int flags,const struct timespec * req,struct timespec * rem)346 int clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem)
347 {
348 return g_usrLibc->clock_nanosleep(clk, flags, req, rem);
349 }
350
clock(void)351 clock_t clock(void)
352 {
353 return g_usrLibc->clock();
354 }
355
time(time_t * timer)356 time_t time(time_t *timer)
357 {
358 return g_usrLibc->time(timer);
359 }
360
gmtime_r(const time_t * timep,struct tm * result)361 struct tm *gmtime_r(const time_t *timep, struct tm *result)
362 {
363 return g_usrLibc->gmtime_r(timep, result);
364 }
365
gmtime(const time_t * timer)366 struct tm *gmtime(const time_t *timer)
367 {
368 return gmtime_r(timer, NULL);
369 }
370
localtime_r(const time_t * timep,struct tm * result)371 struct tm *localtime_r(const time_t *timep, struct tm *result)
372 {
373 return g_usrLibc->localtime_r(timep, result);
374 }
375
localtime(const time_t * timer)376 struct tm *localtime(const time_t *timer)
377 {
378 return localtime_r(timer, NULL);
379 }
380
mktime(struct tm * tmptr)381 time_t mktime(struct tm *tmptr)
382 {
383 return g_usrLibc->mktime(tmptr);
384 }
385
gettimeofday(struct timeval * tv,void * ptz)386 int gettimeofday(struct timeval *tv, void *ptz)
387 {
388 return g_usrLibc->gettimeofday(tv, ptz);
389 }
390
settimeofday(const struct timeval * tv,const struct timezone * tz)391 int settimeofday(const struct timeval *tv, const struct timezone *tz)
392 {
393 return g_usrLibc->settimeofday(tv, tz);
394 }
395
396 #define OS_SYS_NS_PER_US 1000
397 #define OS_SYS_NS_PER_SECOND 1000000000
usleep(unsigned useconds)398 int usleep(unsigned useconds)
399 {
400 struct timespec specTime = { 0 };
401 UINT64 nanoseconds = (UINT64)useconds * OS_SYS_NS_PER_US;
402
403 specTime.tv_sec = (time_t)(nanoseconds / OS_SYS_NS_PER_SECOND);
404 specTime.tv_nsec = (long)(nanoseconds % OS_SYS_NS_PER_SECOND);
405 return nanosleep(&specTime, NULL);
406 }
407
sleep(unsigned seconds)408 unsigned sleep(unsigned seconds)
409 {
410 struct timespec specTime = { 0 };
411 UINT64 nanoseconds = (UINT64)seconds * OS_SYS_NS_PER_SECOND;
412
413 specTime.tv_sec = (time_t)(nanoseconds / OS_SYS_NS_PER_SECOND);
414 specTime.tv_nsec = (long)(nanoseconds % OS_SYS_NS_PER_SECOND);
415 return nanosleep(&specTime, NULL);
416 }
417
free(void * ptr)418 void free(void *ptr)
419 {
420 g_usrLibc->free(ptr);
421 }
422
malloc(size_t size)423 void *malloc(size_t size)
424 {
425 g_usrLibc->malloc(size);
426 }
427
memcpy(void * restrict dest,const void * restrict src,size_t n)428 void *memcpy(void *restrict dest, const void *restrict src, size_t n)
429 {
430 return g_usrLibc->memcpy(dest, src, n);
431 }
432
memcmp(const void * vl,const void * vr,size_t n)433 int memcmp(const void *vl, const void *vr, size_t n)
434 {
435 return g_usrLibc->memcmp(vl, vr, n);
436 }
437
memset(void * dest,int c,size_t n)438 void *memset(void *dest, int c, size_t n)
439 {
440 return g_usrLibc->memset(dest, c, n);
441 }
442
strlen(const char * s)443 size_t strlen(const char *s)
444 {
445 return g_usrLibc->strlen(s);
446 }
447