seqlock.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/config.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/preempt.h>
  31. typedef struct {
  32. unsigned sequence;
  33. spinlock_t lock;
  34. } seqlock_t;
  35. /*
  36. * These macros triggered gcc-3.x compile-time problems. We think these are
  37. * OK now. Be cautious.
  38. */
  39. #define SEQLOCK_UNLOCKED { 0, SPIN_LOCK_UNLOCKED }
  40. #define seqlock_init(x) do { *(x) = (seqlock_t) SEQLOCK_UNLOCKED; } while (0)
  41. /* Lock out other writers and update the count.
  42. * Acts like a normal spin_lock/unlock.
  43. * Don't need preempt_disable() because that is in the spin_lock already.
  44. */
  45. static inline void write_seqlock(seqlock_t *sl)
  46. {
  47. spin_lock(&sl->lock);
  48. ++sl->sequence;
  49. smp_wmb();
  50. }
  51. static inline void write_sequnlock(seqlock_t *sl)
  52. {
  53. smp_wmb();
  54. sl->sequence++;
  55. spin_unlock(&sl->lock);
  56. }
  57. static inline int write_tryseqlock(seqlock_t *sl)
  58. {
  59. int ret = spin_trylock(&sl->lock);
  60. if (ret) {
  61. ++sl->sequence;
  62. smp_wmb();
  63. }
  64. return ret;
  65. }
  66. /* Start of read calculation -- fetch last complete writer token */
  67. static inline unsigned read_seqbegin(const seqlock_t *sl)
  68. {
  69. unsigned ret = sl->sequence;
  70. smp_rmb();
  71. return ret;
  72. }
  73. /* Test if reader processed invalid data.
  74. * If initial values is odd,
  75. * then writer had already started when section was entered
  76. * If sequence value changed
  77. * then writer changed data while in section
  78. *
  79. * Using xor saves one conditional branch.
  80. */
  81. static inline int read_seqretry(const seqlock_t *sl, unsigned iv)
  82. {
  83. smp_rmb();
  84. return (iv & 1) | (sl->sequence ^ iv);
  85. }
  86. /*
  87. * Version using sequence counter only.
  88. * This can be used when code has its own mutex protecting the
  89. * updating starting before the write_seqcountbeqin() and ending
  90. * after the write_seqcount_end().
  91. */
  92. typedef struct seqcount {
  93. unsigned sequence;
  94. } seqcount_t;
  95. #define SEQCNT_ZERO { 0 }
  96. #define seqcount_init(x) do { *(x) = (seqcount_t) SEQCNT_ZERO; } while (0)
  97. /* Start of read using pointer to a sequence counter only. */
  98. static inline unsigned read_seqcount_begin(const seqcount_t *s)
  99. {
  100. unsigned ret = s->sequence;
  101. smp_rmb();
  102. return ret;
  103. }
  104. /* Test if reader processed invalid data.
  105. * Equivalent to: iv is odd or sequence number has changed.
  106. * (iv & 1) || (*s != iv)
  107. * Using xor saves one conditional branch.
  108. */
  109. static inline int read_seqcount_retry(const seqcount_t *s, unsigned iv)
  110. {
  111. smp_rmb();
  112. return (iv & 1) | (s->sequence ^ iv);
  113. }
  114. /*
  115. * Sequence counter only version assumes that callers are using their
  116. * own mutexing.
  117. */
  118. static inline void write_seqcount_begin(seqcount_t *s)
  119. {
  120. s->sequence++;
  121. smp_wmb();
  122. }
  123. static inline void write_seqcount_end(seqcount_t *s)
  124. {
  125. smp_wmb();
  126. s->sequence++;
  127. }
  128. /*
  129. * Possible sw/hw IRQ protected versions of the interfaces.
  130. */
  131. #define write_seqlock_irqsave(lock, flags) \
  132. do { local_irq_save(flags); write_seqlock(lock); } while (0)
  133. #define write_seqlock_irq(lock) \
  134. do { local_irq_disable(); write_seqlock(lock); } while (0)
  135. #define write_seqlock_bh(lock) \
  136. do { local_bh_disable(); write_seqlock(lock); } while (0)
  137. #define write_sequnlock_irqrestore(lock, flags) \
  138. do { write_sequnlock(lock); local_irq_restore(flags); } while(0)
  139. #define write_sequnlock_irq(lock) \
  140. do { write_sequnlock(lock); local_irq_enable(); } while(0)
  141. #define write_sequnlock_bh(lock) \
  142. do { write_sequnlock(lock); local_bh_enable(); } while(0)
  143. #define read_seqbegin_irqsave(lock, flags) \
  144. ({ local_irq_save(flags); read_seqbegin(lock); })
  145. #define read_seqretry_irqrestore(lock, iv, flags) \
  146. ({ \
  147. int ret = read_seqretry(lock, iv); \
  148. local_irq_restore(flags); \
  149. ret; \
  150. })
  151. #endif /* __LINUX_SEQLOCK_H */