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