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>
44 #include "lthread_api.h"
45 #include "pthread_shim.h"
47 #define RTE_LOGTYPE_PTHREAD_SHIM RTE_LOGTYPE_USER3
49 #define POSIX_ERRNO(x) (x)
52 * this flag determines at run time if we override pthread
53 * calls and map then to equivalent lthread calls
54 * or of we call the standard pthread function
56 static __thread int override;
60 * this structures contains function pointers that will be
61 * initialised to the loaded address of the real
62 * pthread library API functions
64 struct pthread_lib_funcs {
65 int (*f_pthread_barrier_destroy)
66 (pthread_barrier_t *);
67 int (*f_pthread_barrier_init)
68 (pthread_barrier_t *, const pthread_barrierattr_t *, unsigned);
69 int (*f_pthread_barrier_wait)
70 (pthread_barrier_t *);
71 int (*f_pthread_cond_broadcast)
73 int (*f_pthread_cond_destroy)
75 int (*f_pthread_cond_init)
76 (pthread_cond_t *, const pthread_condattr_t *);
77 int (*f_pthread_cond_signal)
79 int (*f_pthread_cond_timedwait)
80 (pthread_cond_t *, pthread_mutex_t *, const struct timespec *);
81 int (*f_pthread_cond_wait)
82 (pthread_cond_t *, pthread_mutex_t *);
83 int (*f_pthread_create)
84 (pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
85 int (*f_pthread_detach)
87 int (*f_pthread_equal)
88 (pthread_t, pthread_t);
89 void (*f_pthread_exit)
91 void * (*f_pthread_getspecific)
93 int (*f_pthread_getcpuclockid)
94 (pthread_t, clockid_t *);
97 int (*f_pthread_key_create)
98 (pthread_key_t *, void (*) (void *));
99 int (*f_pthread_key_delete)
101 int (*f_pthread_mutex_destroy)
102 (pthread_mutex_t *__mutex);
103 int (*f_pthread_mutex_init)
104 (pthread_mutex_t *__mutex, const pthread_mutexattr_t *);
105 int (*f_pthread_mutex_lock)
106 (pthread_mutex_t *__mutex);
107 int (*f_pthread_mutex_trylock)
108 (pthread_mutex_t *__mutex);
109 int (*f_pthread_mutex_timedlock)
110 (pthread_mutex_t *__mutex, const struct timespec *);
111 int (*f_pthread_mutex_unlock)
112 (pthread_mutex_t *__mutex);
113 int (*f_pthread_once)
114 (pthread_once_t *, void (*) (void));
115 int (*f_pthread_rwlock_destroy)
116 (pthread_rwlock_t *__rwlock);
117 int (*f_pthread_rwlock_init)
118 (pthread_rwlock_t *__rwlock, const pthread_rwlockattr_t *);
119 int (*f_pthread_rwlock_rdlock)
120 (pthread_rwlock_t *__rwlock);
121 int (*f_pthread_rwlock_timedrdlock)
122 (pthread_rwlock_t *__rwlock, const struct timespec *);
123 int (*f_pthread_rwlock_timedwrlock)
124 (pthread_rwlock_t *__rwlock, const struct timespec *);
125 int (*f_pthread_rwlock_tryrdlock)
126 (pthread_rwlock_t *__rwlock);
127 int (*f_pthread_rwlock_trywrlock)
128 (pthread_rwlock_t *__rwlock);
129 int (*f_pthread_rwlock_unlock)
130 (pthread_rwlock_t *__rwlock);
131 int (*f_pthread_rwlock_wrlock)
132 (pthread_rwlock_t *__rwlock);
133 pthread_t (*f_pthread_self)
135 int (*f_pthread_setspecific)
136 (pthread_key_t, const void *);
137 int (*f_pthread_spin_init)
138 (pthread_spinlock_t *__spin, int);
139 int (*f_pthread_spin_destroy)
140 (pthread_spinlock_t *__spin);
141 int (*f_pthread_spin_lock)
142 (pthread_spinlock_t *__spin);
143 int (*f_pthread_spin_trylock)
144 (pthread_spinlock_t *__spin);
145 int (*f_pthread_spin_unlock)
146 (pthread_spinlock_t *__spin);
147 int (*f_pthread_cancel)
149 int (*f_pthread_setcancelstate)
151 int (*f_pthread_setcanceltype)
153 void (*f_pthread_testcancel)
155 int (*f_pthread_getschedparam)
156 (pthread_t pthread, int *, struct sched_param *);
157 int (*f_pthread_setschedparam)
158 (pthread_t, int, const struct sched_param *);
159 int (*f_pthread_yield)
161 int (*f_pthread_setaffinity_np)
162 (pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset);
164 (const struct timespec *req, struct timespec *rem);
165 } _sys_pthread_funcs = {
166 .f_pthread_barrier_destroy = NULL,
171 * this macro obtains the loaded address of a library function
174 static void *__libc_dl_handle = RTLD_NEXT;
176 #define get_addr_of_loaded_symbol(name) do { \
178 _sys_pthread_funcs.f_##name = dlsym(__libc_dl_handle, (#name)); \
179 error_str = dlerror(); \
180 if (error_str != NULL) { \
181 fprintf(stderr, "%s\n", error_str); \
187 * The constructor function initialises the
188 * function pointers for pthread library functions
191 pthread_intercept_ctor(void)__attribute__((constructor));
193 pthread_intercept_ctor(void)
197 * Get the original functions
199 get_addr_of_loaded_symbol(pthread_barrier_destroy);
200 get_addr_of_loaded_symbol(pthread_barrier_init);
201 get_addr_of_loaded_symbol(pthread_barrier_wait);
202 get_addr_of_loaded_symbol(pthread_cond_broadcast);
203 get_addr_of_loaded_symbol(pthread_cond_destroy);
204 get_addr_of_loaded_symbol(pthread_cond_init);
205 get_addr_of_loaded_symbol(pthread_cond_signal);
206 get_addr_of_loaded_symbol(pthread_cond_timedwait);
207 get_addr_of_loaded_symbol(pthread_cond_wait);
208 get_addr_of_loaded_symbol(pthread_create);
209 get_addr_of_loaded_symbol(pthread_detach);
210 get_addr_of_loaded_symbol(pthread_equal);
211 get_addr_of_loaded_symbol(pthread_exit);
212 get_addr_of_loaded_symbol(pthread_getspecific);
213 get_addr_of_loaded_symbol(pthread_getcpuclockid);
214 get_addr_of_loaded_symbol(pthread_join);
215 get_addr_of_loaded_symbol(pthread_key_create);
216 get_addr_of_loaded_symbol(pthread_key_delete);
217 get_addr_of_loaded_symbol(pthread_mutex_destroy);
218 get_addr_of_loaded_symbol(pthread_mutex_init);
219 get_addr_of_loaded_symbol(pthread_mutex_lock);
220 get_addr_of_loaded_symbol(pthread_mutex_trylock);
221 get_addr_of_loaded_symbol(pthread_mutex_timedlock);
222 get_addr_of_loaded_symbol(pthread_mutex_unlock);
223 get_addr_of_loaded_symbol(pthread_once);
224 get_addr_of_loaded_symbol(pthread_rwlock_destroy);
225 get_addr_of_loaded_symbol(pthread_rwlock_init);
226 get_addr_of_loaded_symbol(pthread_rwlock_rdlock);
227 get_addr_of_loaded_symbol(pthread_rwlock_timedrdlock);
228 get_addr_of_loaded_symbol(pthread_rwlock_timedwrlock);
229 get_addr_of_loaded_symbol(pthread_rwlock_tryrdlock);
230 get_addr_of_loaded_symbol(pthread_rwlock_trywrlock);
231 get_addr_of_loaded_symbol(pthread_rwlock_unlock);
232 get_addr_of_loaded_symbol(pthread_rwlock_wrlock);
233 get_addr_of_loaded_symbol(pthread_self);
234 get_addr_of_loaded_symbol(pthread_setspecific);
235 get_addr_of_loaded_symbol(pthread_spin_init);
236 get_addr_of_loaded_symbol(pthread_spin_destroy);
237 get_addr_of_loaded_symbol(pthread_spin_lock);
238 get_addr_of_loaded_symbol(pthread_spin_trylock);
239 get_addr_of_loaded_symbol(pthread_spin_unlock);
240 get_addr_of_loaded_symbol(pthread_cancel);
241 get_addr_of_loaded_symbol(pthread_setcancelstate);
242 get_addr_of_loaded_symbol(pthread_setcanceltype);
243 get_addr_of_loaded_symbol(pthread_testcancel);
244 get_addr_of_loaded_symbol(pthread_getschedparam);
245 get_addr_of_loaded_symbol(pthread_setschedparam);
246 get_addr_of_loaded_symbol(pthread_yield);
247 get_addr_of_loaded_symbol(pthread_setaffinity_np);
248 get_addr_of_loaded_symbol(nanosleep);
253 * Enable/Disable pthread override
258 void pthread_override_set(int state)
265 * Return pthread override state
270 int pthread_override_get(void)
276 * This macro is used to catch and log
277 * invocation of stubs for unimplemented pthread
280 #define NOT_IMPLEMENTED do { \
284 "WARNING %s NOT IMPLEMENTED\n", \
290 * pthread API override functions follow
291 * Note in this example code only a subset of functions are
294 * The stub functions provided will issue a warning log
295 * message if an unimplemented function is invoked
299 int pthread_barrier_destroy(pthread_barrier_t *a)
302 return _sys_pthread_funcs.f_pthread_barrier_destroy(a);
306 pthread_barrier_init(pthread_barrier_t *a,
307 const pthread_barrierattr_t *b, unsigned c)
310 return _sys_pthread_funcs.f_pthread_barrier_init(a, b, c);
313 int pthread_barrier_wait(pthread_barrier_t *a)
316 return _sys_pthread_funcs.f_pthread_barrier_wait(a);
319 int pthread_cond_broadcast(pthread_cond_t *cond)
323 lthread_cond_broadcast(*(struct lthread_cond **)cond);
326 return _sys_pthread_funcs.f_pthread_cond_broadcast(cond);
329 int pthread_mutex_destroy(pthread_mutex_t *mutex)
332 return lthread_mutex_destroy(*(struct lthread_mutex **)mutex);
333 return _sys_pthread_funcs.f_pthread_mutex_destroy(mutex);
336 int pthread_cond_destroy(pthread_cond_t *cond)
339 return lthread_cond_destroy(*(struct lthread_cond **)cond);
340 return _sys_pthread_funcs.f_pthread_cond_destroy(cond);
343 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
346 return lthread_cond_init(NULL,
347 (struct lthread_cond **)cond,
348 (const struct lthread_condattr *) attr);
349 return _sys_pthread_funcs.f_pthread_cond_init(cond, attr);
352 int pthread_cond_signal(pthread_cond_t *cond)
355 lthread_cond_signal(*(struct lthread_cond **)cond);
358 return _sys_pthread_funcs.f_pthread_cond_signal(cond);
362 pthread_cond_timedwait(pthread_cond_t *__restrict cond,
363 pthread_mutex_t *__restrict mutex,
364 const struct timespec *__restrict time)
367 return _sys_pthread_funcs.f_pthread_cond_timedwait(cond, mutex, time);
370 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
373 pthread_mutex_unlock(mutex);
374 int rv = lthread_cond_wait(*(struct lthread_cond **)cond, 0);
376 pthread_mutex_lock(mutex);
379 return _sys_pthread_funcs.f_pthread_cond_wait(cond, mutex);
383 pthread_create(pthread_t *__restrict tid,
384 const pthread_attr_t *__restrict attr,
385 void *(func) (void *),
386 void *__restrict arg)
392 /* determine CPU being requested */
396 pthread_attr_getaffinity_np(attr,
400 if (CPU_COUNT(&cpuset) != 1)
401 return POSIX_ERRNO(EINVAL);
403 for (lcore = 0; lcore < LTHREAD_MAX_LCORES; lcore++) {
404 if (!CPU_ISSET(lcore, &cpuset))
409 return lthread_create((struct lthread **)tid, lcore,
410 (void (*)(void *))func, arg);
412 return _sys_pthread_funcs.f_pthread_create(tid, attr, func, arg);
415 int pthread_detach(pthread_t tid)
418 struct lthread *lt = (struct lthread *)tid;
420 if (lt == lthread_current())
425 return _sys_pthread_funcs.f_pthread_detach(tid);
428 int pthread_equal(pthread_t a, pthread_t b)
431 return _sys_pthread_funcs.f_pthread_equal(a, b);
434 void pthread_exit_override(void *v)
440 _sys_pthread_funcs.f_pthread_exit(v);
444 *pthread_getspecific(pthread_key_t key)
447 return lthread_getspecific((unsigned int) key);
448 return _sys_pthread_funcs.f_pthread_getspecific(key);
451 int pthread_getcpuclockid(pthread_t a, clockid_t *b)
454 return _sys_pthread_funcs.f_pthread_getcpuclockid(a, b);
457 int pthread_join(pthread_t tid, void **val)
460 return lthread_join((struct lthread *)tid, val);
461 return _sys_pthread_funcs.f_pthread_join(tid, val);
464 int pthread_key_create(pthread_key_t *keyptr, void (*dtor) (void *))
467 return lthread_key_create((unsigned int *)keyptr, dtor);
468 return _sys_pthread_funcs.f_pthread_key_create(keyptr, dtor);
471 int pthread_key_delete(pthread_key_t key)
474 lthread_key_delete((unsigned int) key);
477 return _sys_pthread_funcs.f_pthread_key_delete(key);
482 pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
485 return lthread_mutex_init(NULL,
486 (struct lthread_mutex **)mutex,
487 (const struct lthread_mutexattr *)attr);
488 return _sys_pthread_funcs.f_pthread_mutex_init(mutex, attr);
491 int pthread_mutex_lock(pthread_mutex_t *mutex)
494 return lthread_mutex_lock(*(struct lthread_mutex **)mutex);
495 return _sys_pthread_funcs.f_pthread_mutex_lock(mutex);
498 int pthread_mutex_trylock(pthread_mutex_t *mutex)
501 return lthread_mutex_trylock(*(struct lthread_mutex **)mutex);
502 return _sys_pthread_funcs.f_pthread_mutex_trylock(mutex);
505 int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *b)
508 return _sys_pthread_funcs.f_pthread_mutex_timedlock(mutex, b);
511 int pthread_mutex_unlock(pthread_mutex_t *mutex)
514 return lthread_mutex_unlock(*(struct lthread_mutex **)mutex);
515 return _sys_pthread_funcs.f_pthread_mutex_unlock(mutex);
518 int pthread_once(pthread_once_t *a, void (b) (void))
521 return _sys_pthread_funcs.f_pthread_once(a, b);
524 int pthread_rwlock_destroy(pthread_rwlock_t *a)
527 return _sys_pthread_funcs.f_pthread_rwlock_destroy(a);
530 int pthread_rwlock_init(pthread_rwlock_t *a, const pthread_rwlockattr_t *b)
533 return _sys_pthread_funcs.f_pthread_rwlock_init(a, b);
536 int pthread_rwlock_rdlock(pthread_rwlock_t *a)
539 return _sys_pthread_funcs.f_pthread_rwlock_rdlock(a);
542 int pthread_rwlock_timedrdlock(pthread_rwlock_t *a, const struct timespec *b)
545 return _sys_pthread_funcs.f_pthread_rwlock_timedrdlock(a, b);
548 int pthread_rwlock_timedwrlock(pthread_rwlock_t *a, const struct timespec *b)
551 return _sys_pthread_funcs.f_pthread_rwlock_timedwrlock(a, b);
554 int pthread_rwlock_tryrdlock(pthread_rwlock_t *a)
557 return _sys_pthread_funcs.f_pthread_rwlock_tryrdlock(a);
560 int pthread_rwlock_trywrlock(pthread_rwlock_t *a)
563 return _sys_pthread_funcs.f_pthread_rwlock_trywrlock(a);
566 int pthread_rwlock_unlock(pthread_rwlock_t *a)
569 return _sys_pthread_funcs.f_pthread_rwlock_unlock(a);
572 int pthread_rwlock_wrlock(pthread_rwlock_t *a)
575 return _sys_pthread_funcs.f_pthread_rwlock_wrlock(a);
578 int pthread_yield(void)
584 return _sys_pthread_funcs.f_pthread_yield();
588 pthread_t pthread_self(void)
591 return (pthread_t) lthread_current();
592 return _sys_pthread_funcs.f_pthread_self();
595 int pthread_setspecific(pthread_key_t key, const void *data)
598 int rv = lthread_setspecific((unsigned int)key, data);
601 return _sys_pthread_funcs.f_pthread_setspecific(key, data);
604 int pthread_spin_init(pthread_spinlock_t *a, int b)
607 return _sys_pthread_funcs.f_pthread_spin_init(a, b);
610 int pthread_spin_destroy(pthread_spinlock_t *a)
613 return _sys_pthread_funcs.f_pthread_spin_destroy(a);
616 int pthread_spin_lock(pthread_spinlock_t *a)
619 return _sys_pthread_funcs.f_pthread_spin_lock(a);
622 int pthread_spin_trylock(pthread_spinlock_t *a)
625 return _sys_pthread_funcs.f_pthread_spin_trylock(a);
628 int pthread_spin_unlock(pthread_spinlock_t *a)
631 return _sys_pthread_funcs.f_pthread_spin_unlock(a);
634 int pthread_cancel(pthread_t tid)
637 lthread_cancel(*(struct lthread **)tid);
640 return _sys_pthread_funcs.f_pthread_cancel(tid);
643 int pthread_setcancelstate(int a, int *b)
646 return _sys_pthread_funcs.f_pthread_setcancelstate(a, b);
649 int pthread_setcanceltype(int a, int *b)
652 return _sys_pthread_funcs.f_pthread_setcanceltype(a, b);
655 void pthread_testcancel(void)
658 return _sys_pthread_funcs.f_pthread_testcancel();
662 int pthread_getschedparam(pthread_t tid, int *a, struct sched_param *b)
665 return _sys_pthread_funcs.f_pthread_getschedparam(tid, a, b);
668 int pthread_setschedparam(pthread_t a, int b, const struct sched_param *c)
671 return _sys_pthread_funcs.f_pthread_setschedparam(a, b, c);
675 int nanosleep(const struct timespec *req, struct timespec *rem)
678 uint64_t ns = req->tv_sec * 1000000000 + req->tv_nsec;
683 return _sys_pthread_funcs.f_nanosleep(req, rem);
687 pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
688 const cpu_set_t *cpuset)
691 /* we only allow affinity with a single CPU */
692 if (CPU_COUNT(cpuset) != 1)
693 return POSIX_ERRNO(EINVAL);
695 /* we only allow the current thread to sets its own affinity */
696 struct lthread *lt = (struct lthread *)thread;
698 if (lthread_current() != lt)
699 return POSIX_ERRNO(EINVAL);
701 /* determine the CPU being requested */
704 for (i = 0; i < LTHREAD_MAX_LCORES; i++) {
705 if (!CPU_ISSET(i, cpuset))
709 /* check requested core is allowed */
710 if (i == LTHREAD_MAX_LCORES)
711 return POSIX_ERRNO(EINVAL);
713 /* finally we can set affinity to the requested lcore */
714 lthread_set_affinity(i);
717 return _sys_pthread_funcs.f_pthread_setaffinity_np(thread, cpusetsize,