spinlock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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/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 raw_spinlock_t and raw_rwlock_t definitions:
  71. */
  72. #include <linux/spinlock_types.h>
  73. extern int __lockfunc generic__raw_read_trylock(raw_rwlock_t *lock);
  74. /*
  75. * Pull the __raw*() functions/declarations (UP-nondebug doesnt need them):
  76. */
  77. #ifdef CONFIG_SMP
  78. # include <asm/spinlock.h>
  79. #else
  80. # include <linux/spinlock_up.h>
  81. #endif
  82. #ifdef CONFIG_DEBUG_SPINLOCK
  83. extern void __spin_lock_init(spinlock_t *lock, const char *name,
  84. struct lock_class_key *key);
  85. # define spin_lock_init(lock) \
  86. do { \
  87. static struct lock_class_key __key; \
  88. \
  89. __spin_lock_init((lock), #lock, &__key); \
  90. } while (0)
  91. #else
  92. # define spin_lock_init(lock) \
  93. do { *(lock) = SPIN_LOCK_UNLOCKED; } while (0)
  94. #endif
  95. #ifdef CONFIG_DEBUG_SPINLOCK
  96. extern void __rwlock_init(rwlock_t *lock, const char *name,
  97. struct lock_class_key *key);
  98. # define rwlock_init(lock) \
  99. do { \
  100. static struct lock_class_key __key; \
  101. \
  102. __rwlock_init((lock), #lock, &__key); \
  103. } while (0)
  104. #else
  105. # define rwlock_init(lock) \
  106. do { *(lock) = RW_LOCK_UNLOCKED; } while (0)
  107. #endif
  108. #define spin_is_locked(lock) __raw_spin_is_locked(&(lock)->raw_lock)
  109. #ifdef CONFIG_GENERIC_LOCKBREAK
  110. #define spin_is_contended(lock) ((lock)->break_lock)
  111. #else
  112. #ifdef __raw_spin_is_contended
  113. #define spin_is_contended(lock) __raw_spin_is_contended(&(lock)->raw_lock)
  114. #else
  115. #define spin_is_contended(lock) (((void)(lock), 0))
  116. #endif /*__raw_spin_is_contended*/
  117. #endif
  118. /**
  119. * spin_unlock_wait - wait until the spinlock gets unlocked
  120. * @lock: the spinlock in question.
  121. */
  122. #define spin_unlock_wait(lock) __raw_spin_unlock_wait(&(lock)->raw_lock)
  123. /*
  124. * Pull the _spin_*()/_read_*()/_write_*() functions/declarations:
  125. */
  126. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  127. # include <linux/spinlock_api_smp.h>
  128. #else
  129. # include <linux/spinlock_api_up.h>
  130. #endif
  131. #ifdef CONFIG_DEBUG_SPINLOCK
  132. extern void _raw_spin_lock(spinlock_t *lock);
  133. #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
  134. extern int _raw_spin_trylock(spinlock_t *lock);
  135. extern void _raw_spin_unlock(spinlock_t *lock);
  136. extern void _raw_read_lock(rwlock_t *lock);
  137. extern int _raw_read_trylock(rwlock_t *lock);
  138. extern void _raw_read_unlock(rwlock_t *lock);
  139. extern void _raw_write_lock(rwlock_t *lock);
  140. extern int _raw_write_trylock(rwlock_t *lock);
  141. extern void _raw_write_unlock(rwlock_t *lock);
  142. #else
  143. # define _raw_spin_lock(lock) __raw_spin_lock(&(lock)->raw_lock)
  144. # define _raw_spin_lock_flags(lock, flags) \
  145. __raw_spin_lock_flags(&(lock)->raw_lock, *(flags))
  146. # define _raw_spin_trylock(lock) __raw_spin_trylock(&(lock)->raw_lock)
  147. # define _raw_spin_unlock(lock) __raw_spin_unlock(&(lock)->raw_lock)
  148. # define _raw_read_lock(rwlock) __raw_read_lock(&(rwlock)->raw_lock)
  149. # define _raw_read_trylock(rwlock) __raw_read_trylock(&(rwlock)->raw_lock)
  150. # define _raw_read_unlock(rwlock) __raw_read_unlock(&(rwlock)->raw_lock)
  151. # define _raw_write_lock(rwlock) __raw_write_lock(&(rwlock)->raw_lock)
  152. # define _raw_write_trylock(rwlock) __raw_write_trylock(&(rwlock)->raw_lock)
  153. # define _raw_write_unlock(rwlock) __raw_write_unlock(&(rwlock)->raw_lock)
  154. #endif
  155. #define read_can_lock(rwlock) __raw_read_can_lock(&(rwlock)->raw_lock)
  156. #define write_can_lock(rwlock) __raw_write_can_lock(&(rwlock)->raw_lock)
  157. /*
  158. * Define the various spin_lock and rw_lock methods. Note we define these
  159. * regardless of whether CONFIG_SMP or CONFIG_PREEMPT are set. The various
  160. * methods are defined as nops in the case they are not required.
  161. */
  162. #define spin_trylock(lock) __cond_lock(lock, _spin_trylock(lock))
  163. #define read_trylock(lock) __cond_lock(lock, _read_trylock(lock))
  164. #define write_trylock(lock) __cond_lock(lock, _write_trylock(lock))
  165. #define spin_lock(lock) _spin_lock(lock)
  166. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  167. # define spin_lock_nested(lock, subclass) _spin_lock_nested(lock, subclass)
  168. # define spin_lock_nest_lock(lock, nest_lock) \
  169. do { \
  170. typecheck(struct lockdep_map *, &(nest_lock)->dep_map);\
  171. _spin_lock_nest_lock(lock, &(nest_lock)->dep_map); \
  172. } while (0)
  173. #else
  174. # define spin_lock_nested(lock, subclass) _spin_lock(lock)
  175. # define spin_lock_nest_lock(lock, nest_lock) _spin_lock(lock)
  176. #endif
  177. #define write_lock(lock) _write_lock(lock)
  178. #define read_lock(lock) _read_lock(lock)
  179. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
  180. #define spin_lock_irqsave(lock, flags) \
  181. do { \
  182. typecheck(unsigned long, flags); \
  183. flags = _spin_lock_irqsave(lock); \
  184. } while (0)
  185. #define read_lock_irqsave(lock, flags) \
  186. do { \
  187. typecheck(unsigned long, flags); \
  188. flags = _read_lock_irqsave(lock); \
  189. } while (0)
  190. #define write_lock_irqsave(lock, flags) \
  191. do { \
  192. typecheck(unsigned long, flags); \
  193. flags = _write_lock_irqsave(lock); \
  194. } while (0)
  195. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  196. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  197. do { \
  198. typecheck(unsigned long, flags); \
  199. flags = _spin_lock_irqsave_nested(lock, subclass); \
  200. } while (0)
  201. #else
  202. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  203. do { \
  204. typecheck(unsigned long, flags); \
  205. flags = _spin_lock_irqsave(lock); \
  206. } while (0)
  207. #endif
  208. #else
  209. #define spin_lock_irqsave(lock, flags) \
  210. do { \
  211. typecheck(unsigned long, flags); \
  212. _spin_lock_irqsave(lock, flags); \
  213. } while (0)
  214. #define read_lock_irqsave(lock, flags) \
  215. do { \
  216. typecheck(unsigned long, flags); \
  217. _read_lock_irqsave(lock, flags); \
  218. } while (0)
  219. #define write_lock_irqsave(lock, flags) \
  220. do { \
  221. typecheck(unsigned long, flags); \
  222. _write_lock_irqsave(lock, flags); \
  223. } while (0)
  224. #define spin_lock_irqsave_nested(lock, flags, subclass) \
  225. spin_lock_irqsave(lock, flags)
  226. #endif
  227. #define spin_lock_irq(lock) _spin_lock_irq(lock)
  228. #define spin_lock_bh(lock) _spin_lock_bh(lock)
  229. #define read_lock_irq(lock) _read_lock_irq(lock)
  230. #define read_lock_bh(lock) _read_lock_bh(lock)
  231. #define write_lock_irq(lock) _write_lock_irq(lock)
  232. #define write_lock_bh(lock) _write_lock_bh(lock)
  233. /*
  234. * We inline the unlock functions in the nondebug case:
  235. */
  236. #if defined(CONFIG_DEBUG_SPINLOCK) || defined(CONFIG_PREEMPT) || \
  237. !defined(CONFIG_SMP)
  238. # define spin_unlock(lock) _spin_unlock(lock)
  239. # define read_unlock(lock) _read_unlock(lock)
  240. # define write_unlock(lock) _write_unlock(lock)
  241. # define spin_unlock_irq(lock) _spin_unlock_irq(lock)
  242. # define read_unlock_irq(lock) _read_unlock_irq(lock)
  243. # define write_unlock_irq(lock) _write_unlock_irq(lock)
  244. #else
  245. # define spin_unlock(lock) \
  246. do {__raw_spin_unlock(&(lock)->raw_lock); __release(lock); } while (0)
  247. # define read_unlock(lock) \
  248. do {__raw_read_unlock(&(lock)->raw_lock); __release(lock); } while (0)
  249. # define write_unlock(lock) \
  250. do {__raw_write_unlock(&(lock)->raw_lock); __release(lock); } while (0)
  251. # define spin_unlock_irq(lock) \
  252. do { \
  253. __raw_spin_unlock(&(lock)->raw_lock); \
  254. __release(lock); \
  255. local_irq_enable(); \
  256. } while (0)
  257. # define read_unlock_irq(lock) \
  258. do { \
  259. __raw_read_unlock(&(lock)->raw_lock); \
  260. __release(lock); \
  261. local_irq_enable(); \
  262. } while (0)
  263. # define write_unlock_irq(lock) \
  264. do { \
  265. __raw_write_unlock(&(lock)->raw_lock); \
  266. __release(lock); \
  267. local_irq_enable(); \
  268. } while (0)
  269. #endif
  270. #define spin_unlock_irqrestore(lock, flags) \
  271. do { \
  272. typecheck(unsigned long, flags); \
  273. _spin_unlock_irqrestore(lock, flags); \
  274. } while (0)
  275. #define spin_unlock_bh(lock) _spin_unlock_bh(lock)
  276. #define read_unlock_irqrestore(lock, flags) \
  277. do { \
  278. typecheck(unsigned long, flags); \
  279. _read_unlock_irqrestore(lock, flags); \
  280. } while (0)
  281. #define read_unlock_bh(lock) _read_unlock_bh(lock)
  282. #define write_unlock_irqrestore(lock, flags) \
  283. do { \
  284. typecheck(unsigned long, flags); \
  285. _write_unlock_irqrestore(lock, flags); \
  286. } while (0)
  287. #define write_unlock_bh(lock) _write_unlock_bh(lock)
  288. #define spin_trylock_bh(lock) __cond_lock(lock, _spin_trylock_bh(lock))
  289. #define spin_trylock_irq(lock) \
  290. ({ \
  291. local_irq_disable(); \
  292. spin_trylock(lock) ? \
  293. 1 : ({ local_irq_enable(); 0; }); \
  294. })
  295. #define spin_trylock_irqsave(lock, flags) \
  296. ({ \
  297. local_irq_save(flags); \
  298. spin_trylock(lock) ? \
  299. 1 : ({ local_irq_restore(flags); 0; }); \
  300. })
  301. #define write_trylock_irqsave(lock, flags) \
  302. ({ \
  303. local_irq_save(flags); \
  304. write_trylock(lock) ? \
  305. 1 : ({ local_irq_restore(flags); 0; }); \
  306. })
  307. /*
  308. * Pull the atomic_t declaration:
  309. * (asm-mips/atomic.h needs above definitions)
  310. */
  311. #include <asm/atomic.h>
  312. /**
  313. * atomic_dec_and_lock - lock on reaching reference count zero
  314. * @atomic: the atomic counter
  315. * @lock: the spinlock in question
  316. *
  317. * Decrements @atomic by 1. If the result is 0, returns true and locks
  318. * @lock. Returns false for all other cases.
  319. */
  320. extern int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
  321. #define atomic_dec_and_lock(atomic, lock) \
  322. __cond_lock(lock, _atomic_dec_and_lock(atomic, lock))
  323. /**
  324. * spin_can_lock - would spin_trylock() succeed?
  325. * @lock: the spinlock in question.
  326. */
  327. #define spin_can_lock(lock) (!spin_is_locked(lock))
  328. #endif /* __LINUX_SPINLOCK_H */