spinlock.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 raw_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/preempt.h>
  48. #include <linux/linkage.h>
  49. #include <linux/compiler.h>
  50. #include <linux/thread_info.h>
  51. #include <linux/kernel.h>
  52. #include <linux/stringify.h>
  53. #include <linux/bottom_half.h>
  54. #include <asm/system.h>
  55. /*
  56. * Must define these before including other files, inline functions need them
  57. */
  58. #define LOCK_SECTION_NAME ".text.lock."KBUILD_BASENAME
  59. #define LOCK_SECTION_START(extra) \
  60. ".subsection 1\n\t" \
  61. extra \
  62. ".ifndef " LOCK_SECTION_NAME "\n\t" \
  63. LOCK_SECTION_NAME ":\n\t" \
  64. ".endif\n"
  65. #define LOCK_SECTION_END \
  66. ".previous\n\t"
  67. #define __lockfunc fastcall __attribute__((section(".spinlock.text")))
  68. /*
  69. * Pull the raw_spinlock_t and raw_rwlock_t definitions:
  70. */
  71. #include <linux/spinlock_types.h>
  72. extern int __lockfunc generic__raw_read_trylock(raw_rwlock_t *lock);
  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; } while (0)
  93. #endif
  94. #ifdef CONFIG_DEBUG_SPINLOCK
  95. extern void __rwlock_init(rwlock_t *lock, const char *name,
  96. struct lock_class_key *key);
  97. # define rwlock_init(lock) \
  98. do { \
  99. static struct lock_class_key __key; \
  100. \
  101. __rwlock_init((lock), #lock, &__key); \
  102. } while (0)
  103. #else
  104. # define rwlock_init(lock) \
  105. do { *(lock) = RW_LOCK_UNLOCKED; } while (0)
  106. #endif
  107. #define spin_is_locked(lock) __raw_spin_is_locked(&(lock)->raw_lock)
  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. /*
  114. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  115. */
  116. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  117. # include <linux/spinlock_api_smp.h>
  118. #else
  119. # include <linux/spinlock_api_up.h>
  120. #endif
  121. #ifdef CONFIG_DEBUG_SPINLOCK
  122. extern void _raw_spin_lock(spinlock_t *lock);
  123. #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
  124. extern int _raw_spin_trylock(spinlock_t *lock);
  125. extern void _raw_spin_unlock(spinlock_t *lock);
  126. extern void _raw_read_lock(rwlock_t *lock);
  127. extern int _raw_read_trylock(rwlock_t *lock);
  128. extern void _raw_read_unlock(rwlock_t *lock);
  129. extern void _raw_write_lock(rwlock_t *lock);
  130. extern int _raw_write_trylock(rwlock_t *lock);
  131. extern void _raw_write_unlock(rwlock_t *lock);
  132. #else
  133. # define _raw_spin_lock(lock) __raw_spin_lock(&(lock)->raw_lock)
  134. # define _raw_spin_lock_flags(lock, flags) \
  135. __raw_spin_lock_flags(&(lock)->raw_lock, *(flags))
  136. # define _raw_spin_trylock(lock) __raw_spin_trylock(&(lock)->raw_lock)
  137. # define _raw_spin_unlock(lock) __raw_spin_unlock(&(lock)->raw_lock)
  138. # define _raw_read_lock(rwlock) __raw_read_lock(&(rwlock)->raw_lock)
  139. # define _raw_read_trylock(rwlock) __raw_read_trylock(&(rwlock)->raw_lock)
  140. # define _raw_read_unlock(rwlock) __raw_read_unlock(&(rwlock)->raw_lock)
  141. # define _raw_write_lock(rwlock) __raw_write_lock(&(rwlock)->raw_lock)
  142. # define _raw_write_trylock(rwlock) __raw_write_trylock(&(rwlock)->raw_lock)
  143. # define _raw_write_unlock(rwlock) __raw_write_unlock(&(rwlock)->raw_lock)
  144. #endif
  145. #define read_can_lock(rwlock) __raw_read_can_lock(&(rwlock)->raw_lock)
  146. #define write_can_lock(rwlock) __raw_write_can_lock(&(rwlock)->raw_lock)
  147. /*
  148. * Define the various spin_lock and rw_lock methods. Note we define these
  149. * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
  150. * methods are defined as nops in the case they are not required.
  151. */
  152. #define spin_trylock(lock) __cond_lock(lock, _spin_trylock(lock))
  153. #define read_trylock(lock) __cond_lock(lock, _read_trylock(lock))
  154. #define write_trylock(lock) __cond_lock(lock, _write_trylock(lock))
  155. #define spin_lock(lock) _spin_lock(lock)
  156. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  157. # define spin_lock_nested(lock, subclass) _spin_lock_nested(lock, subclass)
  158. #else
  159. # define spin_lock_nested(lock, subclass) _spin_lock(lock)
  160. #endif
  161. #define write_lock(lock) _write_lock(lock)
  162. #define read_lock(lock) _read_lock(lock)
  163. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  164. #define spin_lock_irqsave(lock, flags) flags = _spin_lock_irqsave(lock)
  165. #define read_lock_irqsave(lock, flags) flags = _read_lock_irqsave(lock)
  166. #define write_lock_irqsave(lock, flags) flags = _write_lock_irqsave(lock)
  167. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  168. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  169. flags = _spin_lock_irqsave_nested(lock, subclass)
  170. #else
  171. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  172. flags = _spin_lock_irqsave(lock)
  173. #endif
  174. #else
  175. #define spin_lock_irqsave(lock, flags) _spin_lock_irqsave(lock, flags)
  176. #define read_lock_irqsave(lock, flags) _read_lock_irqsave(lock, flags)
  177. #define write_lock_irqsave(lock, flags) _write_lock_irqsave(lock, flags)
  178. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  179. spin_lock_irqsave(lock, flags)
  180. #endif
  181. #define spin_lock_irq(lock) _spin_lock_irq(lock)
  182. #define spin_lock_bh(lock) _spin_lock_bh(lock)
  183. #define read_lock_irq(lock) _read_lock_irq(lock)
  184. #define read_lock_bh(lock) _read_lock_bh(lock)
  185. #define write_lock_irq(lock) _write_lock_irq(lock)
  186. #define write_lock_bh(lock) _write_lock_bh(lock)
  187. /*
  188. * We inline the unlock functions in the nondebug case:
  189. */
  190. #if defined(CONFIG_DEBUG_SPINLOCK) || defined(CONFIG_PREEMPT) || \
  191. !defined(CONFIG_SMP)
  192. # define spin_unlock(lock) _spin_unlock(lock)
  193. # define read_unlock(lock) _read_unlock(lock)
  194. # define write_unlock(lock) _write_unlock(lock)
  195. # define spin_unlock_irq(lock) _spin_unlock_irq(lock)
  196. # define read_unlock_irq(lock) _read_unlock_irq(lock)
  197. # define write_unlock_irq(lock) _write_unlock_irq(lock)
  198. #else
  199. # define spin_unlock(lock) \
  200. do {__raw_spin_unlock(&(lock)->raw_lock); __release(lock); } while (0)
  201. # define read_unlock(lock) \
  202. do {__raw_read_unlock(&(lock)->raw_lock); __release(lock); } while (0)
  203. # define write_unlock(lock) \
  204. do {__raw_write_unlock(&(lock)->raw_lock); __release(lock); } while (0)
  205. # define spin_unlock_irq(lock) \
  206. do { \
  207. __raw_spin_unlock(&(lock)->raw_lock); \
  208. __release(lock); \
  209. local_irq_enable(); \
  210. } while (0)
  211. # define read_unlock_irq(lock) \
  212. do { \
  213. __raw_read_unlock(&(lock)->raw_lock); \
  214. __release(lock); \
  215. local_irq_enable(); \
  216. } while (0)
  217. # define write_unlock_irq(lock) \
  218. do { \
  219. __raw_write_unlock(&(lock)->raw_lock); \
  220. __release(lock); \
  221. local_irq_enable(); \
  222. } while (0)
  223. #endif
  224. #define spin_unlock_irqrestore(lock, flags) \
  225. _spin_unlock_irqrestore(lock, flags)
  226. #define spin_unlock_bh(lock) _spin_unlock_bh(lock)
  227. #define read_unlock_irqrestore(lock, flags) \
  228. _read_unlock_irqrestore(lock, flags)
  229. #define read_unlock_bh(lock) _read_unlock_bh(lock)
  230. #define write_unlock_irqrestore(lock, flags) \
  231. _write_unlock_irqrestore(lock, flags)
  232. #define write_unlock_bh(lock) _write_unlock_bh(lock)
  233. #define spin_trylock_bh(lock) __cond_lock(lock, _spin_trylock_bh(lock))
  234. #define spin_trylock_irq(lock) \
  235. ({ \
  236. local_irq_disable(); \
  237. spin_trylock(lock) ? \
  238. 1 : ({ local_irq_enable(); 0; }); \
  239. })
  240. #define spin_trylock_irqsave(lock, flags) \
  241. ({ \
  242. local_irq_save(flags); \
  243. spin_trylock(lock) ? \
  244. 1 : ({ local_irq_restore(flags); 0; }); \
  245. })
  246. /*
  247. * Locks two spinlocks l1 and l2.
  248. * l1_first indicates if spinlock l1 should be taken first.
  249. */
  250. static inline void double_spin_lock(spinlock_t *l1, spinlock_t *l2,
  251. bool l1_first)
  252. __acquires(l1)
  253. __acquires(l2)
  254. {
  255. if (l1_first) {
  256. spin_lock(l1);
  257. spin_lock(l2);
  258. } else {
  259. spin_lock(l2);
  260. spin_lock(l1);
  261. }
  262. }
  263. /*
  264. * Unlocks two spinlocks l1 and l2.
  265. * l1_taken_first indicates if spinlock l1 was taken first and therefore
  266. * should be released after spinlock l2.
  267. */
  268. static inline void double_spin_unlock(spinlock_t *l1, spinlock_t *l2,
  269. bool l1_taken_first)
  270. __releases(l1)
  271. __releases(l2)
  272. {
  273. if (l1_taken_first) {
  274. spin_unlock(l2);
  275. spin_unlock(l1);
  276. } else {
  277. spin_unlock(l1);
  278. spin_unlock(l2);
  279. }
  280. }
  281. /*
  282. * Pull the atomic_t declaration:
  283. * (asm-mips/atomic.h needs above definitions)
  284. */
  285. #include <asm/atomic.h>
  286. /**
  287. * atomic_dec_and_lock - lock on reaching reference count zero
  288. * @atomic: the atomic counter
  289. * @lock: the spinlock in question
  290. */
  291. extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  292. #define atomic_dec_and_lock(atomic, lock) \
  293. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  294. /**
  295. * spin_can_lock - would spin_trylock() succeed?
  296. * @lock: the spinlock in question.
  297. */
  298. #define spin_can_lock(lock) (!spin_is_locked(lock))
  299. #endif /* __LINUX_SPINLOCK_H */