• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
3  * Copyright (c) 2013 Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
4  * Copyright (c) 2010 Ngie Cooper <yaneurabeya@gmail.com>
5  * Copyright (c) 2008 Mike Frysinger <vapier@gmail.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef TST_COMMON_H__
22 #define TST_COMMON_H__
23 
24 #define LTP_ATTRIBUTE_NORETURN		__attribute__((noreturn))
25 #define LTP_ATTRIBUTE_UNUSED		__attribute__((unused))
26 #define LTP_ATTRIBUTE_UNUSED_RESULT	__attribute__((warn_unused_result))
27 
28 #ifndef ARRAY_SIZE
29 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
30 #endif
31 
32 /* Round x to the next multiple of a.
33  * a should be a power of 2.
34  */
35 #define LTP_ALIGN(x, a)    __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1)
36 #define __LTP_ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
37 
38 /**
39  * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay.
40  * @FUNC - The function which will be retried
41  * @ERET - The value returned from @FUNC on success
42  *
43  * This macro will call @FUNC in a loop with a delay between retries. If @FUNC
44  * returns @ERET then the loop exits. The delay between retries starts at one
45  * micro second and is then doubled each iteration until it exceeds one second
46  * (the total time sleeping will be aproximately one second as well). When the
47  * delay exceeds one second tst_brk() is called.
48  */
49 #define TST_RETRY_FUNC(FUNC, ERET) \
50 	TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, 1)
51 
52 #define TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, MAX_DELAY)	\
53 ({	int tst_delay_ = 1;						\
54 	for (;;) {							\
55 		typeof(FUNC) tst_ret_ = FUNC;				\
56 		if (tst_ret_ == ERET)					\
57 			break;						\
58 		if (tst_delay_ < MAX_DELAY * 1000000) {			\
59 			usleep(tst_delay_);				\
60 			tst_delay_ *= 2;				\
61 		} else {						\
62 			tst_brk(TBROK, #FUNC" timed out");		\
63 		}							\
64 	}								\
65 	ERET;								\
66 })
67 
68 #define TST_BRK_SUPPORTS_ONLY_TCONF_TBROK(condition) \
69 	do { ((void)sizeof(char[1 - 2 * !!(condition)])); } while (0)
70 
71 #endif /* TST_COMMON_H__ */
72