1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later 2*49cdfc7eSAndroid Build Coastguard Worker /* 3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2016 Cyril Hrubis <[email protected]> 4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2013 Stanislav Kholmanskikh <[email protected]> 5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2010 Ngie Cooper <[email protected]> 6*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2008 Mike Frysinger <[email protected]> 7*49cdfc7eSAndroid Build Coastguard Worker */ 8*49cdfc7eSAndroid Build Coastguard Worker 9*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_COMMON_H__ 10*49cdfc7eSAndroid Build Coastguard Worker #define TST_COMMON_H__ 11*49cdfc7eSAndroid Build Coastguard Worker 12*49cdfc7eSAndroid Build Coastguard Worker #define LTP_ATTRIBUTE_NORETURN __attribute__((noreturn)) 13*49cdfc7eSAndroid Build Coastguard Worker #define LTP_ATTRIBUTE_UNUSED __attribute__((unused)) 14*49cdfc7eSAndroid Build Coastguard Worker #define LTP_ATTRIBUTE_UNUSED_RESULT __attribute__((warn_unused_result)) 15*49cdfc7eSAndroid Build Coastguard Worker 16*49cdfc7eSAndroid Build Coastguard Worker #ifndef ARRAY_SIZE 17*49cdfc7eSAndroid Build Coastguard Worker # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 18*49cdfc7eSAndroid Build Coastguard Worker #endif 19*49cdfc7eSAndroid Build Coastguard Worker 20*49cdfc7eSAndroid Build Coastguard Worker /* Round x to the next multiple of a. 21*49cdfc7eSAndroid Build Coastguard Worker * a should be a power of 2. 22*49cdfc7eSAndroid Build Coastguard Worker */ 23*49cdfc7eSAndroid Build Coastguard Worker #define LTP_ALIGN(x, a) __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1) 24*49cdfc7eSAndroid Build Coastguard Worker #define __LTP_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) 25*49cdfc7eSAndroid Build Coastguard Worker 26*49cdfc7eSAndroid Build Coastguard Worker /** 27*49cdfc7eSAndroid Build Coastguard Worker * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay. 28*49cdfc7eSAndroid Build Coastguard Worker * @FUNC - The function which will be retried 29*49cdfc7eSAndroid Build Coastguard Worker * @ECHCK - Function/macro for validating @FUNC return value 30*49cdfc7eSAndroid Build Coastguard Worker * 31*49cdfc7eSAndroid Build Coastguard Worker * This macro will call @FUNC in a loop with a delay between retries. 32*49cdfc7eSAndroid Build Coastguard Worker * If ECHCK(ret) evaluates to non-zero, the loop ends. The delay between 33*49cdfc7eSAndroid Build Coastguard Worker * retries starts at one microsecond and is then doubled each iteration until 34*49cdfc7eSAndroid Build Coastguard Worker * it exceeds one second (the total time sleeping will be approximately one 35*49cdfc7eSAndroid Build Coastguard Worker * second as well). When the delay exceeds one second, the loop will end. 36*49cdfc7eSAndroid Build Coastguard Worker * The TST_RETRY_FUNC() macro returns the last value returned by @FUNC. 37*49cdfc7eSAndroid Build Coastguard Worker */ 38*49cdfc7eSAndroid Build Coastguard Worker #define TST_RETRY_FUNC(FUNC, ECHCK) \ 39*49cdfc7eSAndroid Build Coastguard Worker TST_RETRY_FN_EXP_BACKOFF(FUNC, ECHCK, 1) 40*49cdfc7eSAndroid Build Coastguard Worker 41*49cdfc7eSAndroid Build Coastguard Worker #define TST_RETRY_FN_EXP_BACKOFF(FUNC, ECHCK, MAX_DELAY) \ 42*49cdfc7eSAndroid Build Coastguard Worker ({ unsigned int tst_delay_, tst_max_delay_; \ 43*49cdfc7eSAndroid Build Coastguard Worker typeof(FUNC) tst_ret_; \ 44*49cdfc7eSAndroid Build Coastguard Worker tst_delay_ = 1; \ 45*49cdfc7eSAndroid Build Coastguard Worker tst_max_delay_ = tst_multiply_timeout(MAX_DELAY * 1000000); \ 46*49cdfc7eSAndroid Build Coastguard Worker for (;;) { \ 47*49cdfc7eSAndroid Build Coastguard Worker errno = 0; \ 48*49cdfc7eSAndroid Build Coastguard Worker tst_ret_ = FUNC; \ 49*49cdfc7eSAndroid Build Coastguard Worker if (ECHCK(tst_ret_)) \ 50*49cdfc7eSAndroid Build Coastguard Worker break; \ 51*49cdfc7eSAndroid Build Coastguard Worker if (tst_delay_ < tst_max_delay_) { \ 52*49cdfc7eSAndroid Build Coastguard Worker usleep(tst_delay_); \ 53*49cdfc7eSAndroid Build Coastguard Worker tst_delay_ *= 2; \ 54*49cdfc7eSAndroid Build Coastguard Worker } else { \ 55*49cdfc7eSAndroid Build Coastguard Worker break; \ 56*49cdfc7eSAndroid Build Coastguard Worker } \ 57*49cdfc7eSAndroid Build Coastguard Worker } \ 58*49cdfc7eSAndroid Build Coastguard Worker tst_ret_; \ 59*49cdfc7eSAndroid Build Coastguard Worker }) 60*49cdfc7eSAndroid Build Coastguard Worker 61*49cdfc7eSAndroid Build Coastguard Worker /* 62*49cdfc7eSAndroid Build Coastguard Worker * Return value validation macros for TST_RETRY_FUNC(): 63*49cdfc7eSAndroid Build Coastguard Worker * TST_RETVAL_EQ0() - Check that value is equal to zero 64*49cdfc7eSAndroid Build Coastguard Worker */ 65*49cdfc7eSAndroid Build Coastguard Worker #define TST_RETVAL_EQ0(x) (!(x)) 66*49cdfc7eSAndroid Build Coastguard Worker 67*49cdfc7eSAndroid Build Coastguard Worker /* 68*49cdfc7eSAndroid Build Coastguard Worker * TST_RETVAL_NOTNULL() - Check that value is not equal to zero/NULL 69*49cdfc7eSAndroid Build Coastguard Worker */ 70*49cdfc7eSAndroid Build Coastguard Worker #define TST_RETVAL_NOTNULL(x) (!!(x)) 71*49cdfc7eSAndroid Build Coastguard Worker 72*49cdfc7eSAndroid Build Coastguard Worker /* 73*49cdfc7eSAndroid Build Coastguard Worker * TST_RETVAL_GE0() - Check that value is greater than or equal to zero 74*49cdfc7eSAndroid Build Coastguard Worker */ 75*49cdfc7eSAndroid Build Coastguard Worker #define TST_RETVAL_GE0(x) ((x) >= 0) 76*49cdfc7eSAndroid Build Coastguard Worker 77*49cdfc7eSAndroid Build Coastguard Worker #define TST_BUILD_BUG_ON(condition) \ 78*49cdfc7eSAndroid Build Coastguard Worker do { ((void)sizeof(char[1 - 2 * !!(condition)])); } while (0) 79*49cdfc7eSAndroid Build Coastguard Worker 80*49cdfc7eSAndroid Build Coastguard Worker #define TST_BRK_SUPPORTS_ONLY_TCONF_TBROK(condition) \ 81*49cdfc7eSAndroid Build Coastguard Worker TST_BUILD_BUG_ON(condition) 82*49cdfc7eSAndroid Build Coastguard Worker 83*49cdfc7eSAndroid Build Coastguard Worker #define TST_RES_SUPPORTS_TCONF_TDEBUG_TFAIL_TINFO_TPASS_TWARN(condition) \ 84*49cdfc7eSAndroid Build Coastguard Worker TST_BUILD_BUG_ON(condition) 85*49cdfc7eSAndroid Build Coastguard Worker 86*49cdfc7eSAndroid Build Coastguard Worker /* stringification */ 87*49cdfc7eSAndroid Build Coastguard Worker #define TST_TO_STR_(s) #s 88*49cdfc7eSAndroid Build Coastguard Worker #define TST_TO_STR(s) TST_TO_STR_(s) 89*49cdfc7eSAndroid Build Coastguard Worker 90*49cdfc7eSAndroid Build Coastguard Worker #endif /* TST_COMMON_H__ */ 91