seqlock.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef __LINUX_SEQLOCK_H
  2. #define __LINUX_SEQLOCK_H
  3. /*
  4. * Reader/writer consistent mechanism without starving writers. This type of
  5. * lock for data where the reader wants a consitent set of information
  6. * and is willing to retry if the information changes. Readers never
  7. * block but they may have to retry if a writer is in
  8. * progress. Writers do not wait for readers.
  9. *
  10. * This is not as cache friendly as brlock. Also, this will not work
  11. * for data that contains pointers, because any writer could
  12. * invalidate a pointer that a reader was following.
  13. *
  14. * Expected reader usage:
  15. * do {
  16. * seq = read_seqbegin(&foo);
  17. * ...
  18. * } while (read_seqretry(&foo, seq));
  19. *
  20. *
  21. * On non-SMP the spin locks disappear but the writer still needs
  22. * to increment the sequence variables because an interrupt routine could
  23. * change the state of the data.
  24. *
  25. * Based on x86_64 vsyscall gettimeofday
  26. * by Keith Owens and Andrea Arcangeli
  27. */
  28. #include <linux/spinlock.h>
  29. #include <linux/preempt.h>
  30. typedef struct {
  31. unsigned sequence;
  32. spinlock_t lock;
  33. } seqlock_t;
  34. /*
  35. * These macros triggered gcc-3.x compile-time problems. We think these are
  36. * OK now. Be cautious.
  37. */
  38. #define __SEQLOCK_UNLOCKED(lockname) \
  39. { 0, __SPIN_LOCK_UNLOCKED(lockname) }
  40. #define SEQLOCK_UNLOCKED \
  41. __SEQLOCK_UNLOCKED(old_style_seqlock_init)
  42. #define seqlock_init(x) \
  43. do { *(x) = (seqlock_t) __SEQLOCK_UNLOCKED(x); } while (0)
  44. #define DEFINE_SEQLOCK(x) \
  45. seqlock_t x = __SEQLOCK_UNLOCKED(x)
  46. /* Lock out other writers and update the count.
  47. * Acts like a normal spin_lock/unlock.
  48. * Don't need preempt_disable() because that is in the spin_lock already.
  49. */
  50. static inline void write_seqlock(seqlock_t *sl)
  51. {
  52. spin_lock(&sl->lock);
  53. ++sl->sequence;
  54. smp_wmb();
  55. }
  56. static inline void write_sequnlock(seqlock_t *sl)
  57. {
  58. smp_wmb();
  59. sl->sequence++;
  60. spin_unlock(&sl->lock);
  61. }
  62. static inline int write_tryseqlock(seqlock_t *sl)
  63. {
  64. int ret = spin_trylock(&sl->lock);
  65. if (ret) {
  66. ++sl->sequence;
  67. smp_wmb();
  68. }
  69. return ret;
  70. }
  71. /* Start of read calculation -- fetch last complete writer token */
  72. static __always_inline unsigned read_seqbegin(const seqlock_t *sl)
  73. {
  74. unsigned ret = sl->sequence;
  75. smp_rmb();
  76. return ret;
  77. }
  78. /* Test if reader processed invalid data.
  79. * If initial values is odd,
  80. * then writer had already started when section was entered
  81. * If sequence value changed
  82. * then writer changed data while in section
  83. *
  84. * Using xor saves one conditional branch.
  85. */
  86. static __always_inline int read_seqretry(const seqlock_t *sl, unsigned iv)
  87. {
  88. smp_rmb();
  89. return (iv & 1) | (sl->sequence ^ iv);
  90. }
  91. /*
  92. * Version using sequence counter only.
  93. * This can be used when code has its own mutex protecting the
  94. * updating starting before the write_seqcountbeqin() and ending
  95. * after the write_seqcount_end().
  96. */
  97. typedef struct seqcount {
  98. unsigned sequence;
  99. } seqcount_t;
  100. #define SEQCNT_ZERO { 0 }
  101. #define seqcount_init(x) do { *(x) = (seqcount_t) SEQCNT_ZERO; } while (0)
  102. /* Start of read using pointer to a sequence counter only. */
  103. static inline unsigned read_seqcount_begin(const seqcount_t *s)
  104. {
  105. unsigned ret = s->sequence;
  106. smp_rmb();
  107. return ret;
  108. }
  109. /* Test if reader processed invalid data.
  110. * Equivalent to: iv is odd or sequence number has changed.
  111. * (iv & 1) || (*s != iv)
  112. * Using xor saves one conditional branch.
  113. */
  114. static inline int read_seqcount_retry(const seqcount_t *s, unsigned iv)
  115. {
  116. smp_rmb();
  117. return (iv & 1) | (s->sequence ^ iv);
  118. }
  119. /*
  120. * Sequence counter only version assumes that callers are using their
  121. * own mutexing.
  122. */
  123. static inline void write_seqcount_begin(seqcount_t *s)
  124. {
  125. s->sequence++;
  126. smp_wmb();
  127. }
  128. static inline void write_seqcount_end(seqcount_t *s)
  129. {
  130. smp_wmb();
  131. s->sequence++;
  132. }
  133. /*
  134. * Possible sw/hw IRQ protected versions of the interfaces.
  135. */
  136. #define write_seqlock_irqsave(lock, flags) \
  137. do { local_irq_save(flags); write_seqlock(lock); } while (0)
  138. #define write_seqlock_irq(lock) \
  139. do { local_irq_disable(); write_seqlock(lock); } while (0)
  140. #define write_seqlock_bh(lock) \
  141. do { local_bh_disable(); write_seqlock(lock); } while (0)
  142. #define write_sequnlock_irqrestore(lock, flags) \
  143. do { write_sequnlock(lock); local_irq_restore(flags); } while(0)
  144. #define write_sequnlock_irq(lock) \
  145. do { write_sequnlock(lock); local_irq_enable(); } while(0)
  146. #define write_sequnlock_bh(lock) \
  147. do { write_sequnlock(lock); local_bh_enable(); } while(0)
  148. #define read_seqbegin_irqsave(lock, flags) \
  149. ({ local_irq_save(flags); read_seqbegin(lock); })
  150. #define read_seqretry_irqrestore(lock, iv, flags) \
  151. ({ \
  152. int ret = read_seqretry(lock, iv); \
  153. local_irq_restore(flags); \
  154. ret; \
  155. })
  156. #endif /* __LINUX_SEQLOCK_H */