1*49cdfc7eSAndroid Build Coastguard Worker /* SPDX-License-Identifier: GPL-2.0-or-later 2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved. 3*49cdfc7eSAndroid Build Coastguard Worker */ 4*49cdfc7eSAndroid Build Coastguard Worker 5*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_SAFE_PTHREAD_H__ 6*49cdfc7eSAndroid Build Coastguard Worker #define TST_SAFE_PTHREAD_H__ 7*49cdfc7eSAndroid Build Coastguard Worker 8*49cdfc7eSAndroid Build Coastguard Worker /* 9*49cdfc7eSAndroid Build Coastguard Worker * Macro to use for making functions called only once in 10*49cdfc7eSAndroid Build Coastguard Worker * multi-threaded tests such as init or cleanup function. 11*49cdfc7eSAndroid Build Coastguard Worker * The first call to @name_fn function by any thread shall 12*49cdfc7eSAndroid Build Coastguard Worker * call the @exec_fn. Subsequent calls shall not call @exec_fn. 13*49cdfc7eSAndroid Build Coastguard Worker * *_fn functions must not take any arguments. 14*49cdfc7eSAndroid Build Coastguard Worker */ 15*49cdfc7eSAndroid Build Coastguard Worker #define TST_DECLARE_ONCE_FN(name_fn, exec_fn) \ 16*49cdfc7eSAndroid Build Coastguard Worker void name_fn(void) \ 17*49cdfc7eSAndroid Build Coastguard Worker { \ 18*49cdfc7eSAndroid Build Coastguard Worker static pthread_once_t ltp_once = PTHREAD_ONCE_INIT; \ 19*49cdfc7eSAndroid Build Coastguard Worker pthread_once(<p_once, exec_fn); \ 20*49cdfc7eSAndroid Build Coastguard Worker } 21*49cdfc7eSAndroid Build Coastguard Worker 22*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_create(const char *file, const int lineno, 23*49cdfc7eSAndroid Build Coastguard Worker pthread_t *thread_id, const pthread_attr_t *attr, 24*49cdfc7eSAndroid Build Coastguard Worker void *(*thread_fn)(void *), void *arg); 25*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_CREATE(thread_id, attr, thread_fn, arg) \ 26*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_create(__FILE__, __LINE__, thread_id, attr, thread_fn, arg) 27*49cdfc7eSAndroid Build Coastguard Worker 28*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_join(const char *file, const int lineno, 29*49cdfc7eSAndroid Build Coastguard Worker pthread_t thread_id, void **retval); 30*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_JOIN(thread_id, retval) \ 31*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_join(__FILE__, __LINE__, thread_id, retval) 32*49cdfc7eSAndroid Build Coastguard Worker 33*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_barrier_wait(const char *file, const int lineno, 34*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_t *barrier); 35*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_BARRIER_WAIT(barrier) \ 36*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_barrier_wait(__FILE__, __LINE__, barrier); 37*49cdfc7eSAndroid Build Coastguard Worker 38*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_barrier_destroy(const char *file, const int lineno, 39*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_t *barrier); 40*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_BARRIER_DESTROY(barrier) \ 41*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_barrier_destroy(__FILE__, __LINE__, barrier); 42*49cdfc7eSAndroid Build Coastguard Worker 43*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_barrier_init(const char *file, const int lineno, 44*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_t *barrier, 45*49cdfc7eSAndroid Build Coastguard Worker const pthread_barrierattr_t *attr, 46*49cdfc7eSAndroid Build Coastguard Worker unsigned count); 47*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_BARRIER_INIT(barrier, attr, count) \ 48*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_barrier_init(__FILE__, __LINE__, barrier, attr, count); 49*49cdfc7eSAndroid Build Coastguard Worker 50*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_cancel(const char *file, const int lineno, 51*49cdfc7eSAndroid Build Coastguard Worker pthread_t thread_id); 52*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_CANCEL(thread_id) \ 53*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_cancel(__FILE__, __LINE__, thread_id); 54*49cdfc7eSAndroid Build Coastguard Worker 55*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutexattr_init(const char *file, const int lineno, 56*49cdfc7eSAndroid Build Coastguard Worker pthread_mutexattr_t *attr); 57*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEXATTR_INIT(attr) \ 58*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutexattr_init(__FILE__, __LINE__, (attr)) 59*49cdfc7eSAndroid Build Coastguard Worker 60*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutexattr_destroy(const char *file, const int lineno, 61*49cdfc7eSAndroid Build Coastguard Worker pthread_mutexattr_t *attr); 62*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEXATTR_DESTROY(attr) \ 63*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutexattr_destroy(__FILE__, __LINE__, (attr)) 64*49cdfc7eSAndroid Build Coastguard Worker 65*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutexattr_settype(const char *file, const int lineno, 66*49cdfc7eSAndroid Build Coastguard Worker pthread_mutexattr_t *attr, int type); 67*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEXATTR_SETTYPE(attr, type) \ 68*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutexattr_settype(__FILE__, __LINE__, (attr), (type)) 69*49cdfc7eSAndroid Build Coastguard Worker 70*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_init(const char *file, const int lineno, 71*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); 72*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEX_INIT(mutex, attr) \ 73*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutex_init(__FILE__, __LINE__, (mutex), (attr)) 74*49cdfc7eSAndroid Build Coastguard Worker 75*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_destroy(const char *file, const int lineno, 76*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex); 77*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEX_DESTROY(mutex) \ 78*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutex_destroy(__FILE__, __LINE__, (mutex)) 79*49cdfc7eSAndroid Build Coastguard Worker 80*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_lock(const char *file, const int lineno, 81*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex); 82*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEX_LOCK(mutex) \ 83*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutex_lock(__FILE__, __LINE__, (mutex)) 84*49cdfc7eSAndroid Build Coastguard Worker 85*49cdfc7eSAndroid Build Coastguard Worker /* Terminates the test on any error other than EBUSY */ 86*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_trylock(const char *file, const int lineno, 87*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex); 88*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEX_TRYLOCK(mutex) \ 89*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutex_trylock(__FILE__, __LINE__, (mutex)) 90*49cdfc7eSAndroid Build Coastguard Worker 91*49cdfc7eSAndroid Build Coastguard Worker /* Terminates the test on any error other than ETIMEDOUT */ 92*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_timedlock(const char *file, const int lineno, 93*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex, const struct timespec *abstime); 94*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEX_TIMEDLOCK(mutex, abstime) \ 95*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutex_timedlock(__FILE__, __LINE__, (mutex), (abstime)) 96*49cdfc7eSAndroid Build Coastguard Worker 97*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_unlock(const char *file, const int lineno, 98*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex); 99*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_MUTEX_UNLOCK(mutex) \ 100*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_mutex_unlock(__FILE__, __LINE__, (mutex)) 101*49cdfc7eSAndroid Build Coastguard Worker 102*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_kill(const char *file, const int lineno, 103*49cdfc7eSAndroid Build Coastguard Worker pthread_t thread, int sig); 104*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PTHREAD_KILL(thread, sig) \ 105*49cdfc7eSAndroid Build Coastguard Worker safe_pthread_kill(__FILE__, __LINE__, (thread), (sig)) 106*49cdfc7eSAndroid Build Coastguard Worker 107*49cdfc7eSAndroid Build Coastguard Worker #endif /* TST_SAFE_PTHREAD_H__ */ 108