spinlock.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #ifndef __LINUX_SPINLOCK_H
  2. #define __LINUX_SPINLOCK_H
  3. /*
  4. * include/linux/spinlock.h - generic spinlock/rwlock declarations
  5. *
  6. * here's the role of the various spinlock/rwlock related include files:
  7. *
  8. * on SMP builds:
  9. *
  10. * asm/spinlock_types.h: contains the arch_spinlock_t/raw_rwlock_t and the
  11. * initializers
  12. *
  13. * linux/spinlock_types.h:
  14. * defines the generic type and initializers
  15. *
  16. * asm/spinlock.h: contains the __raw_spin_*()/etc. lowlevel
  17. * implementations, mostly inline assembly code
  18. *
  19. * (also included on UP-debug builds:)
  20. *
  21. * linux/spinlock_api_smp.h:
  22. * contains the prototypes for the _spin_*() APIs.
  23. *
  24. * linux/spinlock.h: builds the final spin_*() APIs.
  25. *
  26. * on UP builds:
  27. *
  28. * linux/spinlock_type_up.h:
  29. * contains the generic, simplified UP spinlock type.
  30. * (which is an empty structure on non-debug builds)
  31. *
  32. * linux/spinlock_types.h:
  33. * defines the generic type and initializers
  34. *
  35. * linux/spinlock_up.h:
  36. * contains the __raw_spin_*()/etc. version of UP
  37. * builds. (which are NOPs on non-debug, non-preempt
  38. * builds)
  39. *
  40. * (included on UP-non-debug builds:)
  41. *
  42. * linux/spinlock_api_up.h:
  43. * builds the _spin_*() APIs.
  44. *
  45. * linux/spinlock.h: builds the final spin_*() APIs.
  46. */
  47. #include <linux/typecheck.h>
  48. #include <linux/preempt.h>
  49. #include <linux/linkage.h>
  50. #include <linux/compiler.h>
  51. #include <linux/thread_info.h>
  52. #include <linux/kernel.h>
  53. #include <linux/stringify.h>
  54. #include <linux/bottom_half.h>
  55. #include <asm/system.h>
  56. /*
  57. * Must define these before including other files, inline functions need them
  58. */
  59. #define LOCK_SECTION_NAME ".text.lock."KBUILD_BASENAME
  60. #define LOCK_SECTION_START(extra) \
  61. ".subsection 1\n\t" \
  62. extra \
  63. ".ifndef " LOCK_SECTION_NAME "\n\t" \
  64. LOCK_SECTION_NAME ":\n\t" \
  65. ".endif\n"
  66. #define LOCK_SECTION_END \
  67. ".previous\n\t"
  68. #define __lockfunc __attribute__((section(".spinlock.text")))
  69. /*
  70. * Pull the arch_spinlock_t and raw_rwlock_t definitions:
  71. */
  72. #include <linux/spinlock_types.h>
  73. /*
  74. * Pull the __raw*() functions/declarations (UP-nondebug doesnt need them):
  75. */
  76. #ifdef CONFIG_SMP
  77. # include <asm/spinlock.h>
  78. #else
  79. # include <linux/spinlock_up.h>
  80. #endif
  81. #ifdef CONFIG_DEBUG_SPINLOCK
  82. extern void __spin_lock_init(spinlock_t *lock, const char *name,
  83. struct lock_class_key *key);
  84. # define spin_lock_init(lock) \
  85. do { \
  86. static struct lock_class_key __key; \
  87. \
  88. __spin_lock_init((lock), #lock, &__key); \
  89. } while (0)
  90. #else
  91. # define spin_lock_init(lock) \
  92. do { *(lock) = __SPIN_LOCK_UNLOCKED(lock); } while (0)
  93. #endif
  94. #define spin_is_locked(lock) __raw_spin_is_locked(&(lock)->raw_lock)
  95. #ifdef CONFIG_GENERIC_LOCKBREAK
  96. #define spin_is_contended(lock) ((lock)->break_lock)
  97. #else
  98. #ifdef __raw_spin_is_contended
  99. #define spin_is_contended(lock) __raw_spin_is_contended(&(lock)->raw_lock)
  100. #else
  101. #define spin_is_contended(lock) (((void)(lock), 0))
  102. #endif /*__raw_spin_is_contended*/
  103. #endif
  104. /* The lock does not imply full memory barrier. */
  105. #ifndef ARCH_HAS_SMP_MB_AFTER_LOCK
  106. static inline void smp_mb__after_lock(void) { smp_mb(); }
  107. #endif
  108. /**
  109. * spin_unlock_wait - wait until the spinlock gets unlocked
  110. * @lock: the spinlock in question.
  111. */
  112. #define spin_unlock_wait(lock) __raw_spin_unlock_wait(&(lock)->raw_lock)
  113. #ifdef CONFIG_DEBUG_SPINLOCK
  114. extern void _raw_spin_lock(spinlock_t *lock);
  115. #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
  116. extern int _raw_spin_trylock(spinlock_t *lock);
  117. extern void _raw_spin_unlock(spinlock_t *lock);
  118. #else
  119. # define _raw_spin_lock(lock) __raw_spin_lock(&(lock)->raw_lock)
  120. # define _raw_spin_lock_flags(lock, flags) \
  121. __raw_spin_lock_flags(&(lock)->raw_lock, *(flags))
  122. # define _raw_spin_trylock(lock) __raw_spin_trylock(&(lock)->raw_lock)
  123. # define _raw_spin_unlock(lock) __raw_spin_unlock(&(lock)->raw_lock)
  124. #endif
  125. /*
  126. * Define the various spin_lock methods. Note we define these
  127. * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The
  128. * various methods are defined as nops in the case they are not
  129. * required.
  130. */
  131. #define spin_trylock(lock) __cond_lock(lock, _spin_trylock(lock))
  132. #define spin_lock(lock) _spin_lock(lock)
  133. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  134. # define spin_lock_nested(lock, subclass) _spin_lock_nested(lock, subclass)
  135. # define spin_lock_nest_lock(lock, nest_lock) \
  136. do { \
  137. typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\
  138. _spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  139. } while (0)
  140. #else
  141. # define spin_lock_nested(lock, subclass) _spin_lock(lock)
  142. # define spin_lock_nest_lock(lock, nest_lock) _spin_lock(lock)
  143. #endif
  144. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  145. #define spin_lock_irqsave(lock, flags) \
  146. do { \
  147. typecheck(unsigned long, flags); \
  148. flags = _spin_lock_irqsave(lock); \
  149. } while (0)
  150. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  151. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  152. do { \
  153. typecheck(unsigned long, flags); \
  154. flags = _spin_lock_irqsave_nested(lock, subclass); \
  155. } while (0)
  156. #else
  157. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  158. do { \
  159. typecheck(unsigned long, flags); \
  160. flags = _spin_lock_irqsave(lock); \
  161. } while (0)
  162. #endif
  163. #else
  164. #define spin_lock_irqsave(lock, flags) \
  165. do { \
  166. typecheck(unsigned long, flags); \
  167. _spin_lock_irqsave(lock, flags); \
  168. } while (0)
  169. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  170. spin_lock_irqsave(lock, flags)
  171. #endif
  172. #define spin_lock_irq(lock) _spin_lock_irq(lock)
  173. #define spin_lock_bh(lock) _spin_lock_bh(lock)
  174. #define spin_unlock(lock) _spin_unlock(lock)
  175. #define spin_unlock_irq(lock) _spin_unlock_irq(lock)
  176. #define spin_unlock_irqrestore(lock, flags) \
  177. do { \
  178. typecheck(unsigned long, flags); \
  179. _spin_unlock_irqrestore(lock, flags); \
  180. } while (0)
  181. #define spin_unlock_bh(lock) _spin_unlock_bh(lock)
  182. #define spin_trylock_bh(lock) __cond_lock(lock, _spin_trylock_bh(lock))
  183. #define spin_trylock_irq(lock) \
  184. ({ \
  185. local_irq_disable(); \
  186. spin_trylock(lock) ? \
  187. 1 : ({ local_irq_enable(); 0; }); \
  188. })
  189. #define spin_trylock_irqsave(lock, flags) \
  190. ({ \
  191. local_irq_save(flags); \
  192. spin_trylock(lock) ? \
  193. 1 : ({ local_irq_restore(flags); 0; }); \
  194. })
  195. /*
  196. * Pull the atomic_t declaration:
  197. * (asm-mips/atomic.h needs above definitions)
  198. */
  199. #include <asm/atomic.h>
  200. /**
  201. * atomic_dec_and_lock - lock on reaching reference count zero
  202. * @atomic: the atomic counter
  203. * @lock: the spinlock in question
  204. *
  205. * Decrements @atomic by 1. If the result is 0, returns true and locks
  206. * @lock. Returns false for all other cases.
  207. */
  208. extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  209. #define atomic_dec_and_lock(atomic, lock) \
  210. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  211. /**
  212. * spin_can_lock - would spin_trylock() succeed?
  213. * @lock: the spinlock in question.
  214. */
  215. #define spin_can_lock(lock) (!spin_is_locked(lock))
  216. /* Include rwlock functions */
  217. #include <linux/rwlock.h>
  218. /*
  219. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  220. */
  221. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  222. # include <linux/spinlock_api_smp.h>
  223. #else
  224. # include <linux/spinlock_api_up.h>
  225. #endif
  226. #endif /* __LINUX_SPINLOCK_H */