4 * Copyright(c) 2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
42 #include <rte_config.h>
45 #include "lthread_api.h"
46 #include "pthread_shim.h"
48 #define RTE_LOGTYPE_PTHREAD_SHIM RTE_LOGTYPE_USER3
50 #define POSIX_ERRNO(x) (x)
53 * this flag determines at run time if we override pthread
54 * calls and map then to equivalent lthread calls
55 * or of we call the standard pthread function
57 static __thread int override;
61 * this structures contains function pointers that will be
62 * initialised to the loaded address of the real
63 * pthread library API functions
65 struct pthread_lib_funcs {
66 int (*f_pthread_barrier_destroy)
67 (pthread_barrier_t *);
68 int (*f_pthread_barrier_init)
69 (pthread_barrier_t *, const pthread_barrierattr_t *, unsigned);
70 int (*f_pthread_barrier_wait)
71 (pthread_barrier_t *);
72 int (*f_pthread_cond_broadcast)
74 int (*f_pthread_cond_destroy)
76 int (*f_pthread_cond_init)
77 (pthread_cond_t *, const pthread_condattr_t *);
78 int (*f_pthread_cond_signal)
80 int (*f_pthread_cond_timedwait)
81 (pthread_cond_t *, pthread_mutex_t *, const struct timespec *);
82 int (*f_pthread_cond_wait)
83 (pthread_cond_t *, pthread_mutex_t *);
84 int (*f_pthread_create)
85 (pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
86 int (*f_pthread_detach)
88 int (*f_pthread_equal)
89 (pthread_t, pthread_t);
90 void (*f_pthread_exit)
92 void * (*f_pthread_getspecific)
94 int (*f_pthread_getcpuclockid)
95 (pthread_t, clockid_t *);
98 int (*f_pthread_key_create)
99 (pthread_key_t *, void (*) (void *));
100 int (*f_pthread_key_delete)
102 int (*f_pthread_mutex_destroy)
103 (pthread_mutex_t *__mutex);
104 int (*f_pthread_mutex_init)
105 (pthread_mutex_t *__mutex, const pthread_mutexattr_t *);
106 int (*f_pthread_mutex_lock)
107 (pthread_mutex_t *__mutex);
108 int (*f_pthread_mutex_trylock)
109 (pthread_mutex_t *__mutex);
110 int (*f_pthread_mutex_timedlock)
111 (pthread_mutex_t *__mutex, const struct timespec *);
112 int (*f_pthread_mutex_unlock)
113 (pthread_mutex_t *__mutex);
114 int (*f_pthread_once)
115 (pthread_once_t *, void (*) (void));
116 int (*f_pthread_rwlock_destroy)
117 (pthread_rwlock_t *__rwlock);
118 int (*f_pthread_rwlock_init)
119 (pthread_rwlock_t *__rwlock, const pthread_rwlockattr_t *);
120 int (*f_pthread_rwlock_rdlock)
121 (pthread_rwlock_t *__rwlock);
122 int (*f_pthread_rwlock_timedrdlock)
123 (pthread_rwlock_t *__rwlock, const struct timespec *);
124 int (*f_pthread_rwlock_timedwrlock)
125 (pthread_rwlock_t *__rwlock, const struct timespec *);
126 int (*f_pthread_rwlock_tryrdlock)
127 (pthread_rwlock_t *__rwlock);
128 int (*f_pthread_rwlock_trywrlock)
129 (pthread_rwlock_t *__rwlock);
130 int (*f_pthread_rwlock_unlock)
131 (pthread_rwlock_t *__rwlock);
132 int (*f_pthread_rwlock_wrlock)
133 (pthread_rwlock_t *__rwlock);
134 pthread_t (*f_pthread_self)
136 int (*f_pthread_setspecific)
137 (pthread_key_t, const void *);
138 int (*f_pthread_spin_init)
139 (pthread_spinlock_t *__spin, int);
140 int (*f_pthread_spin_destroy)
141 (pthread_spinlock_t *__spin);
142 int (*f_pthread_spin_lock)
143 (pthread_spinlock_t *__spin);
144 int (*f_pthread_spin_trylock)
145 (pthread_spinlock_t *__spin);
146 int (*f_pthread_spin_unlock)
147 (pthread_spinlock_t *__spin);
148 int (*f_pthread_cancel)
150 int (*f_pthread_setcancelstate)
152 int (*f_pthread_setcanceltype)
154 void (*f_pthread_testcancel)
156 int (*f_pthread_getschedparam)
157 (pthread_t pthread, int *, struct sched_param *);
158 int (*f_pthread_setschedparam)
159 (pthread_t, int, const struct sched_param *);
160 int (*f_pthread_yield)
162 int (*f_pthread_setaffinity_np)
163 (pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset);
165 (const struct timespec *req, struct timespec *rem);
166 } _sys_pthread_funcs = {
167 .f_pthread_barrier_destroy = NULL,
172 * this macro obtains the loaded address of a library function
175 static void *__libc_dl_handle = RTLD_NEXT;
177 #define get_addr_of_loaded_symbol(name) do { \
179 _sys_pthread_funcs.f_##name = dlsym(__libc_dl_handle, (#name)); \
180 error_str = dlerror(); \
181 if (error_str != NULL) { \
182 fprintf(stderr, "%s\n", error_str); \
188 * The constructor function initialises the
189 * function pointers for pthread library functions
192 pthread_intercept_ctor(void)__attribute__((constructor));
194 pthread_intercept_ctor(void)
198 * Get the original functions
200 get_addr_of_loaded_symbol(pthread_barrier_destroy);
201 get_addr_of_loaded_symbol(pthread_barrier_init);
202 get_addr_of_loaded_symbol(pthread_barrier_wait);
203 get_addr_of_loaded_symbol(pthread_cond_broadcast);
204 get_addr_of_loaded_symbol(pthread_cond_destroy);
205 get_addr_of_loaded_symbol(pthread_cond_init);
206 get_addr_of_loaded_symbol(pthread_cond_signal);
207 get_addr_of_loaded_symbol(pthread_cond_timedwait);
208 get_addr_of_loaded_symbol(pthread_cond_wait);
209 get_addr_of_loaded_symbol(pthread_create);
210 get_addr_of_loaded_symbol(pthread_detach);
211 get_addr_of_loaded_symbol(pthread_equal);
212 get_addr_of_loaded_symbol(pthread_exit);
213 get_addr_of_loaded_symbol(pthread_getspecific);
214 get_addr_of_loaded_symbol(pthread_getcpuclockid);
215 get_addr_of_loaded_symbol(pthread_join);
216 get_addr_of_loaded_symbol(pthread_key_create);
217 get_addr_of_loaded_symbol(pthread_key_delete);
218 get_addr_of_loaded_symbol(pthread_mutex_destroy);
219 get_addr_of_loaded_symbol(pthread_mutex_init);
220 get_addr_of_loaded_symbol(pthread_mutex_lock);
221 get_addr_of_loaded_symbol(pthread_mutex_trylock);
222 get_addr_of_loaded_symbol(pthread_mutex_timedlock);
223 get_addr_of_loaded_symbol(pthread_mutex_unlock);
224 get_addr_of_loaded_symbol(pthread_once);
225 get_addr_of_loaded_symbol(pthread_rwlock_destroy);
226 get_addr_of_loaded_symbol(pthread_rwlock_init);
227 get_addr_of_loaded_symbol(pthread_rwlock_rdlock);
228 get_addr_of_loaded_symbol(pthread_rwlock_timedrdlock);
229 get_addr_of_loaded_symbol(pthread_rwlock_timedwrlock);
230 get_addr_of_loaded_symbol(pthread_rwlock_tryrdlock);
231 get_addr_of_loaded_symbol(pthread_rwlock_trywrlock);
232 get_addr_of_loaded_symbol(pthread_rwlock_unlock);
233 get_addr_of_loaded_symbol(pthread_rwlock_wrlock);
234 get_addr_of_loaded_symbol(pthread_self);
235 get_addr_of_loaded_symbol(pthread_setspecific);
236 get_addr_of_loaded_symbol(pthread_spin_init);
237 get_addr_of_loaded_symbol(pthread_spin_destroy);
238 get_addr_of_loaded_symbol(pthread_spin_lock);
239 get_addr_of_loaded_symbol(pthread_spin_trylock);
240 get_addr_of_loaded_symbol(pthread_spin_unlock);
241 get_addr_of_loaded_symbol(pthread_cancel);
242 get_addr_of_loaded_symbol(pthread_setcancelstate);
243 get_addr_of_loaded_symbol(pthread_setcanceltype);
244 get_addr_of_loaded_symbol(pthread_testcancel);
245 get_addr_of_loaded_symbol(pthread_getschedparam);
246 get_addr_of_loaded_symbol(pthread_setschedparam);
247 get_addr_of_loaded_symbol(pthread_yield);
248 get_addr_of_loaded_symbol(pthread_setaffinity_np);
249 get_addr_of_loaded_symbol(nanosleep);
254 * Enable/Disable pthread override
259 void pthread_override_set(int state)
266 * Return pthread override state
271 int pthread_override_get(void)
277 * This macro is used to catch and log
278 * invocation of stubs for unimplemented pthread
281 #define NOT_IMPLEMENTED do { \
285 "WARNING %s NOT IMPLEMENTED\n", \
291 * pthread API override functions follow
292 * Note in this example code only a subset of functions are
295 * The stub functions provided will issue a warning log
296 * message if an unimplemented function is invoked
300 int pthread_barrier_destroy(pthread_barrier_t *a)
303 return _sys_pthread_funcs.f_pthread_barrier_destroy(a);
307 pthread_barrier_init(pthread_barrier_t *a,
308 const pthread_barrierattr_t *b, unsigned c)
311 return _sys_pthread_funcs.f_pthread_barrier_init(a, b, c);
314 int pthread_barrier_wait(pthread_barrier_t *a)
317 return _sys_pthread_funcs.f_pthread_barrier_wait(a);
320 int pthread_cond_broadcast(pthread_cond_t *cond)
324 lthread_cond_broadcast(*(struct lthread_cond **)cond);
327 return _sys_pthread_funcs.f_pthread_cond_broadcast(cond);
330 int pthread_mutex_destroy(pthread_mutex_t *mutex)
333 return lthread_mutex_destroy(*(struct lthread_mutex **)mutex);
334 return _sys_pthread_funcs.f_pthread_mutex_destroy(mutex);
337 int pthread_cond_destroy(pthread_cond_t *cond)
340 return lthread_cond_destroy(*(struct lthread_cond **)cond);
341 return _sys_pthread_funcs.f_pthread_cond_destroy(cond);
344 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
347 return lthread_cond_init(NULL,
348 (struct lthread_cond **)cond,
349 (const struct lthread_condattr *) attr);
350 return _sys_pthread_funcs.f_pthread_cond_init(cond, attr);
353 int pthread_cond_signal(pthread_cond_t *cond)
356 lthread_cond_signal(*(struct lthread_cond **)cond);
359 return _sys_pthread_funcs.f_pthread_cond_signal(cond);
363 pthread_cond_timedwait(pthread_cond_t *__restrict cond,
364 pthread_mutex_t *__restrict mutex,
365 const struct timespec *__restrict time)
368 return _sys_pthread_funcs.f_pthread_cond_timedwait(cond, mutex, time);
371 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
374 pthread_mutex_unlock(mutex);
375 int rv = lthread_cond_wait(*(struct lthread_cond **)cond, 0);
377 pthread_mutex_lock(mutex);
380 return _sys_pthread_funcs.f_pthread_cond_wait(cond, mutex);
384 pthread_create(pthread_t *__restrict tid,
385 const pthread_attr_t *__restrict attr,
386 void *(func) (void *),
387 void *__restrict arg)
393 /* determine CPU being requested */
397 pthread_attr_getaffinity_np(attr,
401 if (CPU_COUNT(&cpuset) != 1)
402 return POSIX_ERRNO(EINVAL);
404 for (lcore = 0; lcore < LTHREAD_MAX_LCORES; lcore++) {
405 if (!CPU_ISSET(lcore, &cpuset))
410 return lthread_create((struct lthread **)tid, lcore,
411 (void (*)(void *))func, arg);
413 return _sys_pthread_funcs.f_pthread_create(tid, attr, func, arg);
416 int pthread_detach(pthread_t tid)
419 struct lthread *lt = (struct lthread *)tid;
421 if (lt == lthread_current())
426 return _sys_pthread_funcs.f_pthread_detach(tid);
429 int pthread_equal(pthread_t a, pthread_t b)
432 return _sys_pthread_funcs.f_pthread_equal(a, b);
435 void pthread_exit_override(void *v)
441 _sys_pthread_funcs.f_pthread_exit(v);
445 *pthread_getspecific(pthread_key_t key)
448 return lthread_getspecific((unsigned int) key);
449 return _sys_pthread_funcs.f_pthread_getspecific(key);
452 int pthread_getcpuclockid(pthread_t a, clockid_t *b)
455 return _sys_pthread_funcs.f_pthread_getcpuclockid(a, b);
458 int pthread_join(pthread_t tid, void **val)
461 return lthread_join((struct lthread *)tid, val);
462 return _sys_pthread_funcs.f_pthread_join(tid, val);
465 int pthread_key_create(pthread_key_t *keyptr, void (*dtor) (void *))
468 return lthread_key_create((unsigned int *)keyptr, dtor);
469 return _sys_pthread_funcs.f_pthread_key_create(keyptr, dtor);
472 int pthread_key_delete(pthread_key_t key)
475 lthread_key_delete((unsigned int) key);
478 return _sys_pthread_funcs.f_pthread_key_delete(key);
483 pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
486 return lthread_mutex_init(NULL,
487 (struct lthread_mutex **)mutex,
488 (const struct lthread_mutexattr *)attr);
489 return _sys_pthread_funcs.f_pthread_mutex_init(mutex, attr);
492 int pthread_mutex_lock(pthread_mutex_t *mutex)
495 return lthread_mutex_lock(*(struct lthread_mutex **)mutex);
496 return _sys_pthread_funcs.f_pthread_mutex_lock(mutex);
499 int pthread_mutex_trylock(pthread_mutex_t *mutex)
502 return lthread_mutex_trylock(*(struct lthread_mutex **)mutex);
503 return _sys_pthread_funcs.f_pthread_mutex_trylock(mutex);
506 int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *b)
509 return _sys_pthread_funcs.f_pthread_mutex_timedlock(mutex, b);
512 int pthread_mutex_unlock(pthread_mutex_t *mutex)
515 return lthread_mutex_unlock(*(struct lthread_mutex **)mutex);
516 return _sys_pthread_funcs.f_pthread_mutex_unlock(mutex);
519 int pthread_once(pthread_once_t *a, void (b) (void))
522 return _sys_pthread_funcs.f_pthread_once(a, b);
525 int pthread_rwlock_destroy(pthread_rwlock_t *a)
528 return _sys_pthread_funcs.f_pthread_rwlock_destroy(a);
531 int pthread_rwlock_init(pthread_rwlock_t *a, const pthread_rwlockattr_t *b)
534 return _sys_pthread_funcs.f_pthread_rwlock_init(a, b);
537 int pthread_rwlock_rdlock(pthread_rwlock_t *a)
540 return _sys_pthread_funcs.f_pthread_rwlock_rdlock(a);
543 int pthread_rwlock_timedrdlock(pthread_rwlock_t *a, const struct timespec *b)
546 return _sys_pthread_funcs.f_pthread_rwlock_timedrdlock(a, b);
549 int pthread_rwlock_timedwrlock(pthread_rwlock_t *a, const struct timespec *b)
552 return _sys_pthread_funcs.f_pthread_rwlock_timedwrlock(a, b);
555 int pthread_rwlock_tryrdlock(pthread_rwlock_t *a)
558 return _sys_pthread_funcs.f_pthread_rwlock_tryrdlock(a);
561 int pthread_rwlock_trywrlock(pthread_rwlock_t *a)
564 return _sys_pthread_funcs.f_pthread_rwlock_trywrlock(a);
567 int pthread_rwlock_unlock(pthread_rwlock_t *a)
570 return _sys_pthread_funcs.f_pthread_rwlock_unlock(a);
573 int pthread_rwlock_wrlock(pthread_rwlock_t *a)
576 return _sys_pthread_funcs.f_pthread_rwlock_wrlock(a);
579 int pthread_yield(void)
585 return _sys_pthread_funcs.f_pthread_yield();
589 pthread_t pthread_self(void)
592 return (pthread_t) lthread_current();
593 return _sys_pthread_funcs.f_pthread_self();
596 int pthread_setspecific(pthread_key_t key, const void *data)
599 int rv = lthread_setspecific((unsigned int)key, data);
602 return _sys_pthread_funcs.f_pthread_setspecific(key, data);
605 int pthread_spin_init(pthread_spinlock_t *a, int b)
608 return _sys_pthread_funcs.f_pthread_spin_init(a, b);
611 int pthread_spin_destroy(pthread_spinlock_t *a)
614 return _sys_pthread_funcs.f_pthread_spin_destroy(a);
617 int pthread_spin_lock(pthread_spinlock_t *a)
620 return _sys_pthread_funcs.f_pthread_spin_lock(a);
623 int pthread_spin_trylock(pthread_spinlock_t *a)
626 return _sys_pthread_funcs.f_pthread_spin_trylock(a);
629 int pthread_spin_unlock(pthread_spinlock_t *a)
632 return _sys_pthread_funcs.f_pthread_spin_unlock(a);
635 int pthread_cancel(pthread_t tid)
638 lthread_cancel(*(struct lthread **)tid);
641 return _sys_pthread_funcs.f_pthread_cancel(tid);
644 int pthread_setcancelstate(int a, int *b)
647 return _sys_pthread_funcs.f_pthread_setcancelstate(a, b);
650 int pthread_setcanceltype(int a, int *b)
653 return _sys_pthread_funcs.f_pthread_setcanceltype(a, b);
656 void pthread_testcancel(void)
659 return _sys_pthread_funcs.f_pthread_testcancel();
663 int pthread_getschedparam(pthread_t tid, int *a, struct sched_param *b)
666 return _sys_pthread_funcs.f_pthread_getschedparam(tid, a, b);
669 int pthread_setschedparam(pthread_t a, int b, const struct sched_param *c)
672 return _sys_pthread_funcs.f_pthread_setschedparam(a, b, c);
676 int nanosleep(const struct timespec *req, struct timespec *rem)
679 uint64_t ns = req->tv_sec * 1000000000 + req->tv_nsec;
684 return _sys_pthread_funcs.f_nanosleep(req, rem);
688 pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
689 const cpu_set_t *cpuset)
692 /* we only allow affinity with a single CPU */
693 if (CPU_COUNT(cpuset) != 1)
694 return POSIX_ERRNO(EINVAL);
696 /* we only allow the current thread to sets its own affinity */
697 struct lthread *lt = (struct lthread *)thread;
699 if (lthread_current() != lt)
700 return POSIX_ERRNO(EINVAL);
702 /* determine the CPU being requested */
705 for (i = 0; i < LTHREAD_MAX_LCORES; i++) {
706 if (!CPU_ISSET(i, cpuset))
710 /* check requested core is allowed */
711 if (i == LTHREAD_MAX_LCORES)
712 return POSIX_ERRNO(EINVAL);
714 /* finally we can set affinity to the requested lcore */
715 lthread_set_affinity(i);
718 return _sys_pthread_funcs.f_pthread_setaffinity_np(thread, cpusetsize,