Xenomai API  2.6.5
seqlock.h
1 #ifndef __SEQLOCK_H
2 #define __SEQLOCK_H
3 
4 /* Originally from the linux kernel, adapted for userland and Xenomai */
5 
6 #include <asm/xenomai/atomic.h>
7 
8 typedef struct xnseqcount {
9  unsigned sequence;
10 } xnseqcount_t;
11 
12 #define XNSEQCNT_ZERO { 0 }
13 #define xnseqcount_init(x) do { *(x) = (xnseqcount_t) XNSEQCNT_ZERO; } while (0)
14 
15 /* Start of read using pointer to a sequence counter only. */
16 static inline unsigned xnread_seqcount_begin(const xnseqcount_t *s)
17 {
18  unsigned ret;
19 
20 repeat:
21  ret = s->sequence;
22  xnarch_read_memory_barrier();
23  if (unlikely(ret & 1)) {
24  cpu_relax();
25  goto repeat;
26  }
27  return ret;
28 }
29 
30 /*
31  * Test if reader processed invalid data because sequence number has changed.
32  */
33 static inline int xnread_seqcount_retry(const xnseqcount_t *s, unsigned start)
34 {
35  xnarch_read_memory_barrier();
36 
37  return s->sequence != start;
38 }
39 
40 
41 /*
42  * The sequence counter only protects readers from concurrent writers.
43  * Writers must use their own locking.
44  */
45 static inline void xnwrite_seqcount_begin(xnseqcount_t *s)
46 {
47  s->sequence++;
48  xnarch_write_memory_barrier();
49 }
50 
51 static inline void xnwrite_seqcount_end(xnseqcount_t *s)
52 {
53  xnarch_write_memory_barrier();
54  s->sequence++;
55 }
56 
57 #endif /* __SEQLOCK_H */