1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz> 4 * Copyright (c) 2013 Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com> 5 * Copyright (c) 2010 Ngie Cooper <yaneurabeya@gmail.com> 6 * Copyright (c) 2008 Mike Frysinger <vapier@gmail.com> 7 */ 8 9 #ifndef TST_COMMON_H__ 10 #define TST_COMMON_H__ 11 12 #define LTP_ATTRIBUTE_NORETURN __attribute__((noreturn)) 13 #define LTP_ATTRIBUTE_UNUSED __attribute__((unused)) 14 #define LTP_ATTRIBUTE_UNUSED_RESULT __attribute__((warn_unused_result)) 15 16 #ifndef ARRAY_SIZE 17 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 18 #endif 19 20 /* Round x to the next multiple of a. 21 * a should be a power of 2. 22 */ 23 #define LTP_ALIGN(x, a) __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1) 24 #define __LTP_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) 25 26 /** 27 * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay. 28 * @FUNC - The function which will be retried 29 * @ERET - The value returned from @FUNC on success 30 * 31 * This macro will call @FUNC in a loop with a delay between retries. If @FUNC 32 * returns @ERET then the loop exits. The delay between retries starts at one 33 * micro second and is then doubled each iteration until it exceeds one second 34 * (the total time sleeping will be approximately one second as well). When the 35 * delay exceeds one second tst_brk() is called. 36 */ 37 #define TST_RETRY_FUNC(FUNC, ERET) \ 38 TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, 1) 39 40 #define TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, MAX_DELAY) \ 41 ({ unsigned int tst_delay_, tst_max_delay_; \ 42 tst_delay_ = 1; \ 43 tst_max_delay_ = tst_multiply_timeout(MAX_DELAY * 1000000); \ 44 for (;;) { \ 45 typeof(FUNC) tst_ret_ = FUNC; \ 46 if (tst_ret_ == ERET) \ 47 break; \ 48 if (tst_delay_ < tst_max_delay_) { \ 49 usleep(tst_delay_); \ 50 tst_delay_ *= 2; \ 51 } else { \ 52 tst_brk(TBROK, #FUNC" timed out"); \ 53 } \ 54 } \ 55 ERET; \ 56 }) 57 58 #define TST_BRK_SUPPORTS_ONLY_TCONF_TBROK(condition) \ 59 do { ((void)sizeof(char[1 - 2 * !!(condition)])); } while (0) 60 61 #endif /* TST_COMMON_H__ */ 62