xref: /minix/minix/include/minix/spin.h (revision 83133719)
1 /* Prototypes for condition spinning helper functions (part of libsys). */
2 #ifndef _MINIX_SPIN_H
3 #define _MINIX_SPIN_H
4 
5 /* Opaque spin state structure. */
6 typedef struct {
7 	int s_state;
8 	u32_t s_usecs;
9 	u64_t s_base_tsc;
10 	clock_t s_base_uptime;
11 	int s_timeout;
12 } spin_t;
13 
14 /* Functions. */
15 void spin_init(spin_t *s, u32_t usecs);
16 int spin_check(spin_t *s);
17 
18 /* Macros. */
19 
20 /* Execute a loop for at least 'u' microseconds, using spin object 's'.
21  * The body of the loop is guaranteed to be executed at least once.
22  */
23 #define SPIN_FOR(s,u)							\
24 	for (spin_init((s), (u)); spin_check((s)); )
25 
26 /* Return whether spin object 's' timed out after a loop. */
27 #define SPIN_TIMEOUT(s) ((s)->s_timeout)
28 
29 /* Spin until the given condition becomes true, or 'u' microseconds expired.
30  * The condition is guaranteed to be checked at least once.
31  */
32 #define SPIN_UNTIL(c,u) do {						\
33 	spin_t s;							\
34 	SPIN_FOR(&s,(u))						\
35 		if (c) break;						\
36 } while (0)
37 
38 #endif /* _MINIX_SPIN_H */
39