atomic.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Atomic operations usable in machine independent code */
  3. #ifndef _LINUX_ATOMIC_H
  4. #define _LINUX_ATOMIC_H
  5. #include <linux/types.h>
  6. #include <asm/atomic.h>
  7. #include <asm/barrier.h>
  8. /*
  9. * Relaxed variants of xchg, cmpxchg and some atomic operations.
  10. *
  11. * We support four variants:
  12. *
  13. * - Fully ordered: The default implementation, no suffix required.
  14. * - Acquire: Provides ACQUIRE semantics, _acquire suffix.
  15. * - Release: Provides RELEASE semantics, _release suffix.
  16. * - Relaxed: No ordering guarantees, _relaxed suffix.
  17. *
  18. * For compound atomics performing both a load and a store, ACQUIRE
  19. * semantics apply only to the load and RELEASE semantics only to the
  20. * store portion of the operation. Note that a failed cmpxchg_acquire
  21. * does -not- imply any memory ordering constraints.
  22. *
  23. * See Documentation/memory-barriers.txt for ACQUIRE/RELEASE definitions.
  24. */
  25. #ifndef atomic_read_acquire
  26. #define atomic_read_acquire(v) smp_load_acquire(&(v)->counter)
  27. #endif
  28. #ifndef atomic_set_release
  29. #define atomic_set_release(v, i) smp_store_release(&(v)->counter, (i))
  30. #endif
  31. /*
  32. * The idea here is to build acquire/release variants by adding explicit
  33. * barriers on top of the relaxed variant. In the case where the relaxed
  34. * variant is already fully ordered, no additional barriers are needed.
  35. *
  36. * Besides, if an arch has a special barrier for acquire/release, it could
  37. * implement its own __atomic_op_* and use the same framework for building
  38. * variants
  39. *
  40. * If an architecture overrides __atomic_op_acquire() it will probably want
  41. * to define smp_mb__after_spinlock().
  42. */
  43. #ifndef __atomic_op_acquire
  44. #define __atomic_op_acquire(op, args...) \
  45. ({ \
  46. typeof(op##_relaxed(args)) __ret = op##_relaxed(args); \
  47. smp_mb__after_atomic(); \
  48. __ret; \
  49. })
  50. #endif
  51. #ifndef __atomic_op_release
  52. #define __atomic_op_release(op, args...) \
  53. ({ \
  54. smp_mb__before_atomic(); \
  55. op##_relaxed(args); \
  56. })
  57. #endif
  58. #ifndef __atomic_op_fence
  59. #define __atomic_op_fence(op, args...) \
  60. ({ \
  61. typeof(op##_relaxed(args)) __ret; \
  62. smp_mb__before_atomic(); \
  63. __ret = op##_relaxed(args); \
  64. smp_mb__after_atomic(); \
  65. __ret; \
  66. })
  67. #endif
  68. /* atomic_add_return_relaxed */
  69. #ifndef atomic_add_return_relaxed
  70. #define atomic_add_return_relaxed atomic_add_return
  71. #define atomic_add_return_acquire atomic_add_return
  72. #define atomic_add_return_release atomic_add_return
  73. #else /* atomic_add_return_relaxed */
  74. #ifndef atomic_add_return_acquire
  75. #define atomic_add_return_acquire(...) \
  76. __atomic_op_acquire(atomic_add_return, __VA_ARGS__)
  77. #endif
  78. #ifndef atomic_add_return_release
  79. #define atomic_add_return_release(...) \
  80. __atomic_op_release(atomic_add_return, __VA_ARGS__)
  81. #endif
  82. #ifndef atomic_add_return
  83. #define atomic_add_return(...) \
  84. __atomic_op_fence(atomic_add_return, __VA_ARGS__)
  85. #endif
  86. #endif /* atomic_add_return_relaxed */
  87. /* atomic_inc_return_relaxed */
  88. #ifndef atomic_inc_return_relaxed
  89. #define atomic_inc_return_relaxed atomic_inc_return
  90. #define atomic_inc_return_acquire atomic_inc_return
  91. #define atomic_inc_return_release atomic_inc_return
  92. #else /* atomic_inc_return_relaxed */
  93. #ifndef atomic_inc_return_acquire
  94. #define atomic_inc_return_acquire(...) \
  95. __atomic_op_acquire(atomic_inc_return, __VA_ARGS__)
  96. #endif
  97. #ifndef atomic_inc_return_release
  98. #define atomic_inc_return_release(...) \
  99. __atomic_op_release(atomic_inc_return, __VA_ARGS__)
  100. #endif
  101. #ifndef atomic_inc_return
  102. #define atomic_inc_return(...) \
  103. __atomic_op_fence(atomic_inc_return, __VA_ARGS__)
  104. #endif
  105. #endif /* atomic_inc_return_relaxed */
  106. /* atomic_sub_return_relaxed */
  107. #ifndef atomic_sub_return_relaxed
  108. #define atomic_sub_return_relaxed atomic_sub_return
  109. #define atomic_sub_return_acquire atomic_sub_return
  110. #define atomic_sub_return_release atomic_sub_return
  111. #else /* atomic_sub_return_relaxed */
  112. #ifndef atomic_sub_return_acquire
  113. #define atomic_sub_return_acquire(...) \
  114. __atomic_op_acquire(atomic_sub_return, __VA_ARGS__)
  115. #endif
  116. #ifndef atomic_sub_return_release
  117. #define atomic_sub_return_release(...) \
  118. __atomic_op_release(atomic_sub_return, __VA_ARGS__)
  119. #endif
  120. #ifndef atomic_sub_return
  121. #define atomic_sub_return(...) \
  122. __atomic_op_fence(atomic_sub_return, __VA_ARGS__)
  123. #endif
  124. #endif /* atomic_sub_return_relaxed */
  125. /* atomic_dec_return_relaxed */
  126. #ifndef atomic_dec_return_relaxed
  127. #define atomic_dec_return_relaxed atomic_dec_return
  128. #define atomic_dec_return_acquire atomic_dec_return
  129. #define atomic_dec_return_release atomic_dec_return
  130. #else /* atomic_dec_return_relaxed */
  131. #ifndef atomic_dec_return_acquire
  132. #define atomic_dec_return_acquire(...) \
  133. __atomic_op_acquire(atomic_dec_return, __VA_ARGS__)
  134. #endif
  135. #ifndef atomic_dec_return_release
  136. #define atomic_dec_return_release(...) \
  137. __atomic_op_release(atomic_dec_return, __VA_ARGS__)
  138. #endif
  139. #ifndef atomic_dec_return
  140. #define atomic_dec_return(...) \
  141. __atomic_op_fence(atomic_dec_return, __VA_ARGS__)
  142. #endif
  143. #endif /* atomic_dec_return_relaxed */
  144. /* atomic_fetch_add_relaxed */
  145. #ifndef atomic_fetch_add_relaxed
  146. #define atomic_fetch_add_relaxed atomic_fetch_add
  147. #define atomic_fetch_add_acquire atomic_fetch_add
  148. #define atomic_fetch_add_release atomic_fetch_add
  149. #else /* atomic_fetch_add_relaxed */
  150. #ifndef atomic_fetch_add_acquire
  151. #define atomic_fetch_add_acquire(...) \
  152. __atomic_op_acquire(atomic_fetch_add, __VA_ARGS__)
  153. #endif
  154. #ifndef atomic_fetch_add_release
  155. #define atomic_fetch_add_release(...) \
  156. __atomic_op_release(atomic_fetch_add, __VA_ARGS__)
  157. #endif
  158. #ifndef atomic_fetch_add
  159. #define atomic_fetch_add(...) \
  160. __atomic_op_fence(atomic_fetch_add, __VA_ARGS__)
  161. #endif
  162. #endif /* atomic_fetch_add_relaxed */
  163. /* atomic_fetch_inc_relaxed */
  164. #ifndef atomic_fetch_inc_relaxed
  165. #ifndef atomic_fetch_inc
  166. #define atomic_fetch_inc(v) atomic_fetch_add(1, (v))
  167. #define atomic_fetch_inc_relaxed(v) atomic_fetch_add_relaxed(1, (v))
  168. #define atomic_fetch_inc_acquire(v) atomic_fetch_add_acquire(1, (v))
  169. #define atomic_fetch_inc_release(v) atomic_fetch_add_release(1, (v))
  170. #else /* atomic_fetch_inc */
  171. #define atomic_fetch_inc_relaxed atomic_fetch_inc
  172. #define atomic_fetch_inc_acquire atomic_fetch_inc
  173. #define atomic_fetch_inc_release atomic_fetch_inc
  174. #endif /* atomic_fetch_inc */
  175. #else /* atomic_fetch_inc_relaxed */
  176. #ifndef atomic_fetch_inc_acquire
  177. #define atomic_fetch_inc_acquire(...) \
  178. __atomic_op_acquire(atomic_fetch_inc, __VA_ARGS__)
  179. #endif
  180. #ifndef atomic_fetch_inc_release
  181. #define atomic_fetch_inc_release(...) \
  182. __atomic_op_release(atomic_fetch_inc, __VA_ARGS__)
  183. #endif
  184. #ifndef atomic_fetch_inc
  185. #define atomic_fetch_inc(...) \
  186. __atomic_op_fence(atomic_fetch_inc, __VA_ARGS__)
  187. #endif
  188. #endif /* atomic_fetch_inc_relaxed */
  189. /* atomic_fetch_sub_relaxed */
  190. #ifndef atomic_fetch_sub_relaxed
  191. #define atomic_fetch_sub_relaxed atomic_fetch_sub
  192. #define atomic_fetch_sub_acquire atomic_fetch_sub
  193. #define atomic_fetch_sub_release atomic_fetch_sub
  194. #else /* atomic_fetch_sub_relaxed */
  195. #ifndef atomic_fetch_sub_acquire
  196. #define atomic_fetch_sub_acquire(...) \
  197. __atomic_op_acquire(atomic_fetch_sub, __VA_ARGS__)
  198. #endif
  199. #ifndef atomic_fetch_sub_release
  200. #define atomic_fetch_sub_release(...) \
  201. __atomic_op_release(atomic_fetch_sub, __VA_ARGS__)
  202. #endif
  203. #ifndef atomic_fetch_sub
  204. #define atomic_fetch_sub(...) \
  205. __atomic_op_fence(atomic_fetch_sub, __VA_ARGS__)
  206. #endif
  207. #endif /* atomic_fetch_sub_relaxed */
  208. /* atomic_fetch_dec_relaxed */
  209. #ifndef atomic_fetch_dec_relaxed
  210. #ifndef atomic_fetch_dec
  211. #define atomic_fetch_dec(v) atomic_fetch_sub(1, (v))
  212. #define atomic_fetch_dec_relaxed(v) atomic_fetch_sub_relaxed(1, (v))
  213. #define atomic_fetch_dec_acquire(v) atomic_fetch_sub_acquire(1, (v))
  214. #define atomic_fetch_dec_release(v) atomic_fetch_sub_release(1, (v))
  215. #else /* atomic_fetch_dec */
  216. #define atomic_fetch_dec_relaxed atomic_fetch_dec
  217. #define atomic_fetch_dec_acquire atomic_fetch_dec
  218. #define atomic_fetch_dec_release atomic_fetch_dec
  219. #endif /* atomic_fetch_dec */
  220. #else /* atomic_fetch_dec_relaxed */
  221. #ifndef atomic_fetch_dec_acquire
  222. #define atomic_fetch_dec_acquire(...) \
  223. __atomic_op_acquire(atomic_fetch_dec, __VA_ARGS__)
  224. #endif
  225. #ifndef atomic_fetch_dec_release
  226. #define atomic_fetch_dec_release(...) \
  227. __atomic_op_release(atomic_fetch_dec, __VA_ARGS__)
  228. #endif
  229. #ifndef atomic_fetch_dec
  230. #define atomic_fetch_dec(...) \
  231. __atomic_op_fence(atomic_fetch_dec, __VA_ARGS__)
  232. #endif
  233. #endif /* atomic_fetch_dec_relaxed */
  234. /* atomic_fetch_or_relaxed */
  235. #ifndef atomic_fetch_or_relaxed
  236. #define atomic_fetch_or_relaxed atomic_fetch_or
  237. #define atomic_fetch_or_acquire atomic_fetch_or
  238. #define atomic_fetch_or_release atomic_fetch_or
  239. #else /* atomic_fetch_or_relaxed */
  240. #ifndef atomic_fetch_or_acquire
  241. #define atomic_fetch_or_acquire(...) \
  242. __atomic_op_acquire(atomic_fetch_or, __VA_ARGS__)
  243. #endif
  244. #ifndef atomic_fetch_or_release
  245. #define atomic_fetch_or_release(...) \
  246. __atomic_op_release(atomic_fetch_or, __VA_ARGS__)
  247. #endif
  248. #ifndef atomic_fetch_or
  249. #define atomic_fetch_or(...) \
  250. __atomic_op_fence(atomic_fetch_or, __VA_ARGS__)
  251. #endif
  252. #endif /* atomic_fetch_or_relaxed */
  253. /* atomic_fetch_and_relaxed */
  254. #ifndef atomic_fetch_and_relaxed
  255. #define atomic_fetch_and_relaxed atomic_fetch_and
  256. #define atomic_fetch_and_acquire atomic_fetch_and
  257. #define atomic_fetch_and_release atomic_fetch_and
  258. #else /* atomic_fetch_and_relaxed */
  259. #ifndef atomic_fetch_and_acquire
  260. #define atomic_fetch_and_acquire(...) \
  261. __atomic_op_acquire(atomic_fetch_and, __VA_ARGS__)
  262. #endif
  263. #ifndef atomic_fetch_and_release
  264. #define atomic_fetch_and_release(...) \
  265. __atomic_op_release(atomic_fetch_and, __VA_ARGS__)
  266. #endif
  267. #ifndef atomic_fetch_and
  268. #define atomic_fetch_and(...) \
  269. __atomic_op_fence(atomic_fetch_and, __VA_ARGS__)
  270. #endif
  271. #endif /* atomic_fetch_and_relaxed */
  272. #ifdef atomic_andnot
  273. /* atomic_fetch_andnot_relaxed */
  274. #ifndef atomic_fetch_andnot_relaxed
  275. #define atomic_fetch_andnot_relaxed atomic_fetch_andnot
  276. #define atomic_fetch_andnot_acquire atomic_fetch_andnot
  277. #define atomic_fetch_andnot_release atomic_fetch_andnot
  278. #else /* atomic_fetch_andnot_relaxed */
  279. #ifndef atomic_fetch_andnot_acquire
  280. #define atomic_fetch_andnot_acquire(...) \
  281. __atomic_op_acquire(atomic_fetch_andnot, __VA_ARGS__)
  282. #endif
  283. #ifndef atomic_fetch_andnot_release
  284. #define atomic_fetch_andnot_release(...) \
  285. __atomic_op_release(atomic_fetch_andnot, __VA_ARGS__)
  286. #endif
  287. #ifndef atomic_fetch_andnot
  288. #define atomic_fetch_andnot(...) \
  289. __atomic_op_fence(atomic_fetch_andnot, __VA_ARGS__)
  290. #endif
  291. #endif /* atomic_fetch_andnot_relaxed */
  292. #endif /* atomic_andnot */
  293. /* atomic_fetch_xor_relaxed */
  294. #ifndef atomic_fetch_xor_relaxed
  295. #define atomic_fetch_xor_relaxed atomic_fetch_xor
  296. #define atomic_fetch_xor_acquire atomic_fetch_xor
  297. #define atomic_fetch_xor_release atomic_fetch_xor
  298. #else /* atomic_fetch_xor_relaxed */
  299. #ifndef atomic_fetch_xor_acquire
  300. #define atomic_fetch_xor_acquire(...) \
  301. __atomic_op_acquire(atomic_fetch_xor, __VA_ARGS__)
  302. #endif
  303. #ifndef atomic_fetch_xor_release
  304. #define atomic_fetch_xor_release(...) \
  305. __atomic_op_release(atomic_fetch_xor, __VA_ARGS__)
  306. #endif
  307. #ifndef atomic_fetch_xor
  308. #define atomic_fetch_xor(...) \
  309. __atomic_op_fence(atomic_fetch_xor, __VA_ARGS__)
  310. #endif
  311. #endif /* atomic_fetch_xor_relaxed */
  312. /* atomic_xchg_relaxed */
  313. #ifndef atomic_xchg_relaxed
  314. #define atomic_xchg_relaxed atomic_xchg
  315. #define atomic_xchg_acquire atomic_xchg
  316. #define atomic_xchg_release atomic_xchg
  317. #else /* atomic_xchg_relaxed */
  318. #ifndef atomic_xchg_acquire
  319. #define atomic_xchg_acquire(...) \
  320. __atomic_op_acquire(atomic_xchg, __VA_ARGS__)
  321. #endif
  322. #ifndef atomic_xchg_release
  323. #define atomic_xchg_release(...) \
  324. __atomic_op_release(atomic_xchg, __VA_ARGS__)
  325. #endif
  326. #ifndef atomic_xchg
  327. #define atomic_xchg(...) \
  328. __atomic_op_fence(atomic_xchg, __VA_ARGS__)
  329. #endif
  330. #endif /* atomic_xchg_relaxed */
  331. /* atomic_cmpxchg_relaxed */
  332. #ifndef atomic_cmpxchg_relaxed
  333. #define atomic_cmpxchg_relaxed atomic_cmpxchg
  334. #define atomic_cmpxchg_acquire atomic_cmpxchg
  335. #define atomic_cmpxchg_release atomic_cmpxchg
  336. #else /* atomic_cmpxchg_relaxed */
  337. #ifndef atomic_cmpxchg_acquire
  338. #define atomic_cmpxchg_acquire(...) \
  339. __atomic_op_acquire(atomic_cmpxchg, __VA_ARGS__)
  340. #endif
  341. #ifndef atomic_cmpxchg_release
  342. #define atomic_cmpxchg_release(...) \
  343. __atomic_op_release(atomic_cmpxchg, __VA_ARGS__)
  344. #endif
  345. #ifndef atomic_cmpxchg
  346. #define atomic_cmpxchg(...) \
  347. __atomic_op_fence(atomic_cmpxchg, __VA_ARGS__)
  348. #endif
  349. #endif /* atomic_cmpxchg_relaxed */
  350. #ifndef atomic_try_cmpxchg
  351. #define __atomic_try_cmpxchg(type, _p, _po, _n) \
  352. ({ \
  353. typeof(_po) __po = (_po); \
  354. typeof(*(_po)) __r, __o = *__po; \
  355. __r = atomic_cmpxchg##type((_p), __o, (_n)); \
  356. if (unlikely(__r != __o)) \
  357. *__po = __r; \
  358. likely(__r == __o); \
  359. })
  360. #define atomic_try_cmpxchg(_p, _po, _n) __atomic_try_cmpxchg(, _p, _po, _n)
  361. #define atomic_try_cmpxchg_relaxed(_p, _po, _n) __atomic_try_cmpxchg(_relaxed, _p, _po, _n)
  362. #define atomic_try_cmpxchg_acquire(_p, _po, _n) __atomic_try_cmpxchg(_acquire, _p, _po, _n)
  363. #define atomic_try_cmpxchg_release(_p, _po, _n) __atomic_try_cmpxchg(_release, _p, _po, _n)
  364. #else /* atomic_try_cmpxchg */
  365. #define atomic_try_cmpxchg_relaxed atomic_try_cmpxchg
  366. #define atomic_try_cmpxchg_acquire atomic_try_cmpxchg
  367. #define atomic_try_cmpxchg_release atomic_try_cmpxchg
  368. #endif /* atomic_try_cmpxchg */
  369. /* cmpxchg_relaxed */
  370. #ifndef cmpxchg_relaxed
  371. #define cmpxchg_relaxed cmpxchg
  372. #define cmpxchg_acquire cmpxchg
  373. #define cmpxchg_release cmpxchg
  374. #else /* cmpxchg_relaxed */
  375. #ifndef cmpxchg_acquire
  376. #define cmpxchg_acquire(...) \
  377. __atomic_op_acquire(cmpxchg, __VA_ARGS__)
  378. #endif
  379. #ifndef cmpxchg_release
  380. #define cmpxchg_release(...) \
  381. __atomic_op_release(cmpxchg, __VA_ARGS__)
  382. #endif
  383. #ifndef cmpxchg
  384. #define cmpxchg(...) \
  385. __atomic_op_fence(cmpxchg, __VA_ARGS__)
  386. #endif
  387. #endif /* cmpxchg_relaxed */
  388. /* cmpxchg64_relaxed */
  389. #ifndef cmpxchg64_relaxed
  390. #define cmpxchg64_relaxed cmpxchg64
  391. #define cmpxchg64_acquire cmpxchg64
  392. #define cmpxchg64_release cmpxchg64
  393. #else /* cmpxchg64_relaxed */
  394. #ifndef cmpxchg64_acquire
  395. #define cmpxchg64_acquire(...) \
  396. __atomic_op_acquire(cmpxchg64, __VA_ARGS__)
  397. #endif
  398. #ifndef cmpxchg64_release
  399. #define cmpxchg64_release(...) \
  400. __atomic_op_release(cmpxchg64, __VA_ARGS__)
  401. #endif
  402. #ifndef cmpxchg64
  403. #define cmpxchg64(...) \
  404. __atomic_op_fence(cmpxchg64, __VA_ARGS__)
  405. #endif
  406. #endif /* cmpxchg64_relaxed */
  407. /* xchg_relaxed */
  408. #ifndef xchg_relaxed
  409. #define xchg_relaxed xchg
  410. #define xchg_acquire xchg
  411. #define xchg_release xchg
  412. #else /* xchg_relaxed */
  413. #ifndef xchg_acquire
  414. #define xchg_acquire(...) __atomic_op_acquire(xchg, __VA_ARGS__)
  415. #endif
  416. #ifndef xchg_release
  417. #define xchg_release(...) __atomic_op_release(xchg, __VA_ARGS__)
  418. #endif
  419. #ifndef xchg
  420. #define xchg(...) __atomic_op_fence(xchg, __VA_ARGS__)
  421. #endif
  422. #endif /* xchg_relaxed */
  423. /**
  424. * atomic_fetch_add_unless - add unless the number is already a given value
  425. * @v: pointer of type atomic_t
  426. * @a: the amount to add to v...
  427. * @u: ...unless v is equal to u.
  428. *
  429. * Atomically adds @a to @v, if @v was not already @u.
  430. * Returns the original value of @v.
  431. */
  432. #ifndef atomic_fetch_add_unless
  433. static inline int atomic_fetch_add_unless(atomic_t *v, int a, int u)
  434. {
  435. int c = atomic_read(v);
  436. do {
  437. if (unlikely(c == u))
  438. break;
  439. } while (!atomic_try_cmpxchg(v, &c, c + a));
  440. return c;
  441. }
  442. #endif
  443. /**
  444. * atomic_add_unless - add unless the number is already a given value
  445. * @v: pointer of type atomic_t
  446. * @a: the amount to add to v...
  447. * @u: ...unless v is equal to u.
  448. *
  449. * Atomically adds @a to @v, if @v was not already @u.
  450. * Returns true if the addition was done.
  451. */
  452. static inline bool atomic_add_unless(atomic_t *v, int a, int u)
  453. {
  454. return atomic_fetch_add_unless(v, a, u) != u;
  455. }
  456. /**
  457. * atomic_inc_not_zero - increment unless the number is zero
  458. * @v: pointer of type atomic_t
  459. *
  460. * Atomically increments @v by 1, if @v is non-zero.
  461. * Returns true if the increment was done.
  462. */
  463. #ifndef atomic_inc_not_zero
  464. #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
  465. #endif
  466. /**
  467. * atomic_inc_and_test - increment and test
  468. * @v: pointer of type atomic_t
  469. *
  470. * Atomically increments @v by 1
  471. * and returns true if the result is zero, or false for all
  472. * other cases.
  473. */
  474. #ifndef atomic_inc_and_test
  475. static inline bool atomic_inc_and_test(atomic_t *v)
  476. {
  477. return atomic_inc_return(v) == 0;
  478. }
  479. #endif
  480. /**
  481. * atomic_dec_and_test - decrement and test
  482. * @v: pointer of type atomic_t
  483. *
  484. * Atomically decrements @v by 1 and
  485. * returns true if the result is 0, or false for all other
  486. * cases.
  487. */
  488. #ifndef atomic_dec_and_test
  489. static inline bool atomic_dec_and_test(atomic_t *v)
  490. {
  491. return atomic_dec_return(v) == 0;
  492. }
  493. #endif
  494. /**
  495. * atomic_sub_and_test - subtract value from variable and test result
  496. * @i: integer value to subtract
  497. * @v: pointer of type atomic_t
  498. *
  499. * Atomically subtracts @i from @v and returns
  500. * true if the result is zero, or false for all
  501. * other cases.
  502. */
  503. #ifndef atomic_sub_and_test
  504. static inline bool atomic_sub_and_test(int i, atomic_t *v)
  505. {
  506. return atomic_sub_return(i, v) == 0;
  507. }
  508. #endif
  509. /**
  510. * atomic_add_negative - add and test if negative
  511. * @i: integer value to add
  512. * @v: pointer of type atomic_t
  513. *
  514. * Atomically adds @i to @v and returns true
  515. * if the result is negative, or false when
  516. * result is greater than or equal to zero.
  517. */
  518. #ifndef atomic_add_negative
  519. static inline bool atomic_add_negative(int i, atomic_t *v)
  520. {
  521. return atomic_add_return(i, v) < 0;
  522. }
  523. #endif
  524. #ifndef atomic_andnot
  525. static inline void atomic_andnot(int i, atomic_t *v)
  526. {
  527. atomic_and(~i, v);
  528. }
  529. static inline int atomic_fetch_andnot(int i, atomic_t *v)
  530. {
  531. return atomic_fetch_and(~i, v);
  532. }
  533. static inline int atomic_fetch_andnot_relaxed(int i, atomic_t *v)
  534. {
  535. return atomic_fetch_and_relaxed(~i, v);
  536. }
  537. static inline int atomic_fetch_andnot_acquire(int i, atomic_t *v)
  538. {
  539. return atomic_fetch_and_acquire(~i, v);
  540. }
  541. static inline int atomic_fetch_andnot_release(int i, atomic_t *v)
  542. {
  543. return atomic_fetch_and_release(~i, v);
  544. }
  545. #endif
  546. #ifndef atomic_inc_unless_negative
  547. static inline bool atomic_inc_unless_negative(atomic_t *p)
  548. {
  549. int v, v1;
  550. for (v = 0; v >= 0; v = v1) {
  551. v1 = atomic_cmpxchg(p, v, v + 1);
  552. if (likely(v1 == v))
  553. return true;
  554. }
  555. return false;
  556. }
  557. #endif
  558. #ifndef atomic_dec_unless_positive
  559. static inline bool atomic_dec_unless_positive(atomic_t *p)
  560. {
  561. int v, v1;
  562. for (v = 0; v <= 0; v = v1) {
  563. v1 = atomic_cmpxchg(p, v, v - 1);
  564. if (likely(v1 == v))
  565. return true;
  566. }
  567. return false;
  568. }
  569. #endif
  570. /*
  571. * atomic_dec_if_positive - decrement by 1 if old value positive
  572. * @v: pointer of type atomic_t
  573. *
  574. * The function returns the old value of *v minus 1, even if
  575. * the atomic variable, v, was not decremented.
  576. */
  577. #ifndef atomic_dec_if_positive
  578. static inline int atomic_dec_if_positive(atomic_t *v)
  579. {
  580. int c, old, dec;
  581. c = atomic_read(v);
  582. for (;;) {
  583. dec = c - 1;
  584. if (unlikely(dec < 0))
  585. break;
  586. old = atomic_cmpxchg((v), c, dec);
  587. if (likely(old == c))
  588. break;
  589. c = old;
  590. }
  591. return dec;
  592. }
  593. #endif
  594. #define atomic_cond_read_relaxed(v, c) smp_cond_load_relaxed(&(v)->counter, (c))
  595. #define atomic_cond_read_acquire(v, c) smp_cond_load_acquire(&(v)->counter, (c))
  596. #ifdef CONFIG_GENERIC_ATOMIC64
  597. #include <asm-generic/atomic64.h>
  598. #endif
  599. #ifndef atomic64_read_acquire
  600. #define atomic64_read_acquire(v) smp_load_acquire(&(v)->counter)
  601. #endif
  602. #ifndef atomic64_set_release
  603. #define atomic64_set_release(v, i) smp_store_release(&(v)->counter, (i))
  604. #endif
  605. /* atomic64_add_return_relaxed */
  606. #ifndef atomic64_add_return_relaxed
  607. #define atomic64_add_return_relaxed atomic64_add_return
  608. #define atomic64_add_return_acquire atomic64_add_return
  609. #define atomic64_add_return_release atomic64_add_return
  610. #else /* atomic64_add_return_relaxed */
  611. #ifndef atomic64_add_return_acquire
  612. #define atomic64_add_return_acquire(...) \
  613. __atomic_op_acquire(atomic64_add_return, __VA_ARGS__)
  614. #endif
  615. #ifndef atomic64_add_return_release
  616. #define atomic64_add_return_release(...) \
  617. __atomic_op_release(atomic64_add_return, __VA_ARGS__)
  618. #endif
  619. #ifndef atomic64_add_return
  620. #define atomic64_add_return(...) \
  621. __atomic_op_fence(atomic64_add_return, __VA_ARGS__)
  622. #endif
  623. #endif /* atomic64_add_return_relaxed */
  624. /* atomic64_inc_return_relaxed */
  625. #ifndef atomic64_inc_return_relaxed
  626. #define atomic64_inc_return_relaxed atomic64_inc_return
  627. #define atomic64_inc_return_acquire atomic64_inc_return
  628. #define atomic64_inc_return_release atomic64_inc_return
  629. #else /* atomic64_inc_return_relaxed */
  630. #ifndef atomic64_inc_return_acquire
  631. #define atomic64_inc_return_acquire(...) \
  632. __atomic_op_acquire(atomic64_inc_return, __VA_ARGS__)
  633. #endif
  634. #ifndef atomic64_inc_return_release
  635. #define atomic64_inc_return_release(...) \
  636. __atomic_op_release(atomic64_inc_return, __VA_ARGS__)
  637. #endif
  638. #ifndef atomic64_inc_return
  639. #define atomic64_inc_return(...) \
  640. __atomic_op_fence(atomic64_inc_return, __VA_ARGS__)
  641. #endif
  642. #endif /* atomic64_inc_return_relaxed */
  643. /* atomic64_sub_return_relaxed */
  644. #ifndef atomic64_sub_return_relaxed
  645. #define atomic64_sub_return_relaxed atomic64_sub_return
  646. #define atomic64_sub_return_acquire atomic64_sub_return
  647. #define atomic64_sub_return_release atomic64_sub_return
  648. #else /* atomic64_sub_return_relaxed */
  649. #ifndef atomic64_sub_return_acquire
  650. #define atomic64_sub_return_acquire(...) \
  651. __atomic_op_acquire(atomic64_sub_return, __VA_ARGS__)
  652. #endif
  653. #ifndef atomic64_sub_return_release
  654. #define atomic64_sub_return_release(...) \
  655. __atomic_op_release(atomic64_sub_return, __VA_ARGS__)
  656. #endif
  657. #ifndef atomic64_sub_return
  658. #define atomic64_sub_return(...) \
  659. __atomic_op_fence(atomic64_sub_return, __VA_ARGS__)
  660. #endif
  661. #endif /* atomic64_sub_return_relaxed */
  662. /* atomic64_dec_return_relaxed */
  663. #ifndef atomic64_dec_return_relaxed
  664. #define atomic64_dec_return_relaxed atomic64_dec_return
  665. #define atomic64_dec_return_acquire atomic64_dec_return
  666. #define atomic64_dec_return_release atomic64_dec_return
  667. #else /* atomic64_dec_return_relaxed */
  668. #ifndef atomic64_dec_return_acquire
  669. #define atomic64_dec_return_acquire(...) \
  670. __atomic_op_acquire(atomic64_dec_return, __VA_ARGS__)
  671. #endif
  672. #ifndef atomic64_dec_return_release
  673. #define atomic64_dec_return_release(...) \
  674. __atomic_op_release(atomic64_dec_return, __VA_ARGS__)
  675. #endif
  676. #ifndef atomic64_dec_return
  677. #define atomic64_dec_return(...) \
  678. __atomic_op_fence(atomic64_dec_return, __VA_ARGS__)
  679. #endif
  680. #endif /* atomic64_dec_return_relaxed */
  681. /* atomic64_fetch_add_relaxed */
  682. #ifndef atomic64_fetch_add_relaxed
  683. #define atomic64_fetch_add_relaxed atomic64_fetch_add
  684. #define atomic64_fetch_add_acquire atomic64_fetch_add
  685. #define atomic64_fetch_add_release atomic64_fetch_add
  686. #else /* atomic64_fetch_add_relaxed */
  687. #ifndef atomic64_fetch_add_acquire
  688. #define atomic64_fetch_add_acquire(...) \
  689. __atomic_op_acquire(atomic64_fetch_add, __VA_ARGS__)
  690. #endif
  691. #ifndef atomic64_fetch_add_release
  692. #define atomic64_fetch_add_release(...) \
  693. __atomic_op_release(atomic64_fetch_add, __VA_ARGS__)
  694. #endif
  695. #ifndef atomic64_fetch_add
  696. #define atomic64_fetch_add(...) \
  697. __atomic_op_fence(atomic64_fetch_add, __VA_ARGS__)
  698. #endif
  699. #endif /* atomic64_fetch_add_relaxed */
  700. /* atomic64_fetch_inc_relaxed */
  701. #ifndef atomic64_fetch_inc_relaxed
  702. #ifndef atomic64_fetch_inc
  703. #define atomic64_fetch_inc(v) atomic64_fetch_add(1, (v))
  704. #define atomic64_fetch_inc_relaxed(v) atomic64_fetch_add_relaxed(1, (v))
  705. #define atomic64_fetch_inc_acquire(v) atomic64_fetch_add_acquire(1, (v))
  706. #define atomic64_fetch_inc_release(v) atomic64_fetch_add_release(1, (v))
  707. #else /* atomic64_fetch_inc */
  708. #define atomic64_fetch_inc_relaxed atomic64_fetch_inc
  709. #define atomic64_fetch_inc_acquire atomic64_fetch_inc
  710. #define atomic64_fetch_inc_release atomic64_fetch_inc
  711. #endif /* atomic64_fetch_inc */
  712. #else /* atomic64_fetch_inc_relaxed */
  713. #ifndef atomic64_fetch_inc_acquire
  714. #define atomic64_fetch_inc_acquire(...) \
  715. __atomic_op_acquire(atomic64_fetch_inc, __VA_ARGS__)
  716. #endif
  717. #ifndef atomic64_fetch_inc_release
  718. #define atomic64_fetch_inc_release(...) \
  719. __atomic_op_release(atomic64_fetch_inc, __VA_ARGS__)
  720. #endif
  721. #ifndef atomic64_fetch_inc
  722. #define atomic64_fetch_inc(...) \
  723. __atomic_op_fence(atomic64_fetch_inc, __VA_ARGS__)
  724. #endif
  725. #endif /* atomic64_fetch_inc_relaxed */
  726. /* atomic64_fetch_sub_relaxed */
  727. #ifndef atomic64_fetch_sub_relaxed
  728. #define atomic64_fetch_sub_relaxed atomic64_fetch_sub
  729. #define atomic64_fetch_sub_acquire atomic64_fetch_sub
  730. #define atomic64_fetch_sub_release atomic64_fetch_sub
  731. #else /* atomic64_fetch_sub_relaxed */
  732. #ifndef atomic64_fetch_sub_acquire
  733. #define atomic64_fetch_sub_acquire(...) \
  734. __atomic_op_acquire(atomic64_fetch_sub, __VA_ARGS__)
  735. #endif
  736. #ifndef atomic64_fetch_sub_release
  737. #define atomic64_fetch_sub_release(...) \
  738. __atomic_op_release(atomic64_fetch_sub, __VA_ARGS__)
  739. #endif
  740. #ifndef atomic64_fetch_sub
  741. #define atomic64_fetch_sub(...) \
  742. __atomic_op_fence(atomic64_fetch_sub, __VA_ARGS__)
  743. #endif
  744. #endif /* atomic64_fetch_sub_relaxed */
  745. /* atomic64_fetch_dec_relaxed */
  746. #ifndef atomic64_fetch_dec_relaxed
  747. #ifndef atomic64_fetch_dec
  748. #define atomic64_fetch_dec(v) atomic64_fetch_sub(1, (v))
  749. #define atomic64_fetch_dec_relaxed(v) atomic64_fetch_sub_relaxed(1, (v))
  750. #define atomic64_fetch_dec_acquire(v) atomic64_fetch_sub_acquire(1, (v))
  751. #define atomic64_fetch_dec_release(v) atomic64_fetch_sub_release(1, (v))
  752. #else /* atomic64_fetch_dec */
  753. #define atomic64_fetch_dec_relaxed atomic64_fetch_dec
  754. #define atomic64_fetch_dec_acquire atomic64_fetch_dec
  755. #define atomic64_fetch_dec_release atomic64_fetch_dec
  756. #endif /* atomic64_fetch_dec */
  757. #else /* atomic64_fetch_dec_relaxed */
  758. #ifndef atomic64_fetch_dec_acquire
  759. #define atomic64_fetch_dec_acquire(...) \
  760. __atomic_op_acquire(atomic64_fetch_dec, __VA_ARGS__)
  761. #endif
  762. #ifndef atomic64_fetch_dec_release
  763. #define atomic64_fetch_dec_release(...) \
  764. __atomic_op_release(atomic64_fetch_dec, __VA_ARGS__)
  765. #endif
  766. #ifndef atomic64_fetch_dec
  767. #define atomic64_fetch_dec(...) \
  768. __atomic_op_fence(atomic64_fetch_dec, __VA_ARGS__)
  769. #endif
  770. #endif /* atomic64_fetch_dec_relaxed */
  771. /* atomic64_fetch_or_relaxed */
  772. #ifndef atomic64_fetch_or_relaxed
  773. #define atomic64_fetch_or_relaxed atomic64_fetch_or
  774. #define atomic64_fetch_or_acquire atomic64_fetch_or
  775. #define atomic64_fetch_or_release atomic64_fetch_or
  776. #else /* atomic64_fetch_or_relaxed */
  777. #ifndef atomic64_fetch_or_acquire
  778. #define atomic64_fetch_or_acquire(...) \
  779. __atomic_op_acquire(atomic64_fetch_or, __VA_ARGS__)
  780. #endif
  781. #ifndef atomic64_fetch_or_release
  782. #define atomic64_fetch_or_release(...) \
  783. __atomic_op_release(atomic64_fetch_or, __VA_ARGS__)
  784. #endif
  785. #ifndef atomic64_fetch_or
  786. #define atomic64_fetch_or(...) \
  787. __atomic_op_fence(atomic64_fetch_or, __VA_ARGS__)
  788. #endif
  789. #endif /* atomic64_fetch_or_relaxed */
  790. /* atomic64_fetch_and_relaxed */
  791. #ifndef atomic64_fetch_and_relaxed
  792. #define atomic64_fetch_and_relaxed atomic64_fetch_and
  793. #define atomic64_fetch_and_acquire atomic64_fetch_and
  794. #define atomic64_fetch_and_release atomic64_fetch_and
  795. #else /* atomic64_fetch_and_relaxed */
  796. #ifndef atomic64_fetch_and_acquire
  797. #define atomic64_fetch_and_acquire(...) \
  798. __atomic_op_acquire(atomic64_fetch_and, __VA_ARGS__)
  799. #endif
  800. #ifndef atomic64_fetch_and_release
  801. #define atomic64_fetch_and_release(...) \
  802. __atomic_op_release(atomic64_fetch_and, __VA_ARGS__)
  803. #endif
  804. #ifndef atomic64_fetch_and
  805. #define atomic64_fetch_and(...) \
  806. __atomic_op_fence(atomic64_fetch_and, __VA_ARGS__)
  807. #endif
  808. #endif /* atomic64_fetch_and_relaxed */
  809. #ifdef atomic64_andnot
  810. /* atomic64_fetch_andnot_relaxed */
  811. #ifndef atomic64_fetch_andnot_relaxed
  812. #define atomic64_fetch_andnot_relaxed atomic64_fetch_andnot
  813. #define atomic64_fetch_andnot_acquire atomic64_fetch_andnot
  814. #define atomic64_fetch_andnot_release atomic64_fetch_andnot
  815. #else /* atomic64_fetch_andnot_relaxed */
  816. #ifndef atomic64_fetch_andnot_acquire
  817. #define atomic64_fetch_andnot_acquire(...) \
  818. __atomic_op_acquire(atomic64_fetch_andnot, __VA_ARGS__)
  819. #endif
  820. #ifndef atomic64_fetch_andnot_release
  821. #define atomic64_fetch_andnot_release(...) \
  822. __atomic_op_release(atomic64_fetch_andnot, __VA_ARGS__)
  823. #endif
  824. #ifndef atomic64_fetch_andnot
  825. #define atomic64_fetch_andnot(...) \
  826. __atomic_op_fence(atomic64_fetch_andnot, __VA_ARGS__)
  827. #endif
  828. #endif /* atomic64_fetch_andnot_relaxed */
  829. #endif /* atomic64_andnot */
  830. /* atomic64_fetch_xor_relaxed */
  831. #ifndef atomic64_fetch_xor_relaxed
  832. #define atomic64_fetch_xor_relaxed atomic64_fetch_xor
  833. #define atomic64_fetch_xor_acquire atomic64_fetch_xor
  834. #define atomic64_fetch_xor_release atomic64_fetch_xor
  835. #else /* atomic64_fetch_xor_relaxed */
  836. #ifndef atomic64_fetch_xor_acquire
  837. #define atomic64_fetch_xor_acquire(...) \
  838. __atomic_op_acquire(atomic64_fetch_xor, __VA_ARGS__)
  839. #endif
  840. #ifndef atomic64_fetch_xor_release
  841. #define atomic64_fetch_xor_release(...) \
  842. __atomic_op_release(atomic64_fetch_xor, __VA_ARGS__)
  843. #endif
  844. #ifndef atomic64_fetch_xor
  845. #define atomic64_fetch_xor(...) \
  846. __atomic_op_fence(atomic64_fetch_xor, __VA_ARGS__)
  847. #endif
  848. #endif /* atomic64_fetch_xor_relaxed */
  849. /* atomic64_xchg_relaxed */
  850. #ifndef atomic64_xchg_relaxed
  851. #define atomic64_xchg_relaxed atomic64_xchg
  852. #define atomic64_xchg_acquire atomic64_xchg
  853. #define atomic64_xchg_release atomic64_xchg
  854. #else /* atomic64_xchg_relaxed */
  855. #ifndef atomic64_xchg_acquire
  856. #define atomic64_xchg_acquire(...) \
  857. __atomic_op_acquire(atomic64_xchg, __VA_ARGS__)
  858. #endif
  859. #ifndef atomic64_xchg_release
  860. #define atomic64_xchg_release(...) \
  861. __atomic_op_release(atomic64_xchg, __VA_ARGS__)
  862. #endif
  863. #ifndef atomic64_xchg
  864. #define atomic64_xchg(...) \
  865. __atomic_op_fence(atomic64_xchg, __VA_ARGS__)
  866. #endif
  867. #endif /* atomic64_xchg_relaxed */
  868. /* atomic64_cmpxchg_relaxed */
  869. #ifndef atomic64_cmpxchg_relaxed
  870. #define atomic64_cmpxchg_relaxed atomic64_cmpxchg
  871. #define atomic64_cmpxchg_acquire atomic64_cmpxchg
  872. #define atomic64_cmpxchg_release atomic64_cmpxchg
  873. #else /* atomic64_cmpxchg_relaxed */
  874. #ifndef atomic64_cmpxchg_acquire
  875. #define atomic64_cmpxchg_acquire(...) \
  876. __atomic_op_acquire(atomic64_cmpxchg, __VA_ARGS__)
  877. #endif
  878. #ifndef atomic64_cmpxchg_release
  879. #define atomic64_cmpxchg_release(...) \
  880. __atomic_op_release(atomic64_cmpxchg, __VA_ARGS__)
  881. #endif
  882. #ifndef atomic64_cmpxchg
  883. #define atomic64_cmpxchg(...) \
  884. __atomic_op_fence(atomic64_cmpxchg, __VA_ARGS__)
  885. #endif
  886. #endif /* atomic64_cmpxchg_relaxed */
  887. #ifndef atomic64_try_cmpxchg
  888. #define __atomic64_try_cmpxchg(type, _p, _po, _n) \
  889. ({ \
  890. typeof(_po) __po = (_po); \
  891. typeof(*(_po)) __r, __o = *__po; \
  892. __r = atomic64_cmpxchg##type((_p), __o, (_n)); \
  893. if (unlikely(__r != __o)) \
  894. *__po = __r; \
  895. likely(__r == __o); \
  896. })
  897. #define atomic64_try_cmpxchg(_p, _po, _n) __atomic64_try_cmpxchg(, _p, _po, _n)
  898. #define atomic64_try_cmpxchg_relaxed(_p, _po, _n) __atomic64_try_cmpxchg(_relaxed, _p, _po, _n)
  899. #define atomic64_try_cmpxchg_acquire(_p, _po, _n) __atomic64_try_cmpxchg(_acquire, _p, _po, _n)
  900. #define atomic64_try_cmpxchg_release(_p, _po, _n) __atomic64_try_cmpxchg(_release, _p, _po, _n)
  901. #else /* atomic64_try_cmpxchg */
  902. #define atomic64_try_cmpxchg_relaxed atomic64_try_cmpxchg
  903. #define atomic64_try_cmpxchg_acquire atomic64_try_cmpxchg
  904. #define atomic64_try_cmpxchg_release atomic64_try_cmpxchg
  905. #endif /* atomic64_try_cmpxchg */
  906. /**
  907. * atomic64_fetch_add_unless - add unless the number is already a given value
  908. * @v: pointer of type atomic64_t
  909. * @a: the amount to add to v...
  910. * @u: ...unless v is equal to u.
  911. *
  912. * Atomically adds @a to @v, if @v was not already @u.
  913. * Returns the original value of @v.
  914. */
  915. #ifndef atomic64_fetch_add_unless
  916. static inline long long atomic64_fetch_add_unless(atomic64_t *v, long long a,
  917. long long u)
  918. {
  919. long long c = atomic64_read(v);
  920. do {
  921. if (unlikely(c == u))
  922. break;
  923. } while (!atomic64_try_cmpxchg(v, &c, c + a));
  924. return c;
  925. }
  926. #endif
  927. /**
  928. * atomic64_add_unless - add unless the number is already a given value
  929. * @v: pointer of type atomic_t
  930. * @a: the amount to add to v...
  931. * @u: ...unless v is equal to u.
  932. *
  933. * Atomically adds @a to @v, if @v was not already @u.
  934. * Returns true if the addition was done.
  935. */
  936. static inline bool atomic64_add_unless(atomic64_t *v, long long a, long long u)
  937. {
  938. return atomic64_fetch_add_unless(v, a, u) != u;
  939. }
  940. /**
  941. * atomic64_inc_not_zero - increment unless the number is zero
  942. * @v: pointer of type atomic64_t
  943. *
  944. * Atomically increments @v by 1, if @v is non-zero.
  945. * Returns true if the increment was done.
  946. */
  947. #ifndef atomic64_inc_not_zero
  948. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  949. #endif
  950. /**
  951. * atomic64_inc_and_test - increment and test
  952. * @v: pointer of type atomic64_t
  953. *
  954. * Atomically increments @v by 1
  955. * and returns true if the result is zero, or false for all
  956. * other cases.
  957. */
  958. #ifndef atomic64_inc_and_test
  959. static inline bool atomic64_inc_and_test(atomic64_t *v)
  960. {
  961. return atomic64_inc_return(v) == 0;
  962. }
  963. #endif
  964. /**
  965. * atomic64_dec_and_test - decrement and test
  966. * @v: pointer of type atomic64_t
  967. *
  968. * Atomically decrements @v by 1 and
  969. * returns true if the result is 0, or false for all other
  970. * cases.
  971. */
  972. #ifndef atomic64_dec_and_test
  973. static inline bool atomic64_dec_and_test(atomic64_t *v)
  974. {
  975. return atomic64_dec_return(v) == 0;
  976. }
  977. #endif
  978. /**
  979. * atomic64_sub_and_test - subtract value from variable and test result
  980. * @i: integer value to subtract
  981. * @v: pointer of type atomic64_t
  982. *
  983. * Atomically subtracts @i from @v and returns
  984. * true if the result is zero, or false for all
  985. * other cases.
  986. */
  987. #ifndef atomic64_sub_and_test
  988. static inline bool atomic64_sub_and_test(long long i, atomic64_t *v)
  989. {
  990. return atomic64_sub_return(i, v) == 0;
  991. }
  992. #endif
  993. /**
  994. * atomic64_add_negative - add and test if negative
  995. * @i: integer value to add
  996. * @v: pointer of type atomic64_t
  997. *
  998. * Atomically adds @i to @v and returns true
  999. * if the result is negative, or false when
  1000. * result is greater than or equal to zero.
  1001. */
  1002. #ifndef atomic64_add_negative
  1003. static inline bool atomic64_add_negative(long long i, atomic64_t *v)
  1004. {
  1005. return atomic64_add_return(i, v) < 0;
  1006. }
  1007. #endif
  1008. #ifndef atomic64_andnot
  1009. static inline void atomic64_andnot(long long i, atomic64_t *v)
  1010. {
  1011. atomic64_and(~i, v);
  1012. }
  1013. static inline long long atomic64_fetch_andnot(long long i, atomic64_t *v)
  1014. {
  1015. return atomic64_fetch_and(~i, v);
  1016. }
  1017. static inline long long atomic64_fetch_andnot_relaxed(long long i, atomic64_t *v)
  1018. {
  1019. return atomic64_fetch_and_relaxed(~i, v);
  1020. }
  1021. static inline long long atomic64_fetch_andnot_acquire(long long i, atomic64_t *v)
  1022. {
  1023. return atomic64_fetch_and_acquire(~i, v);
  1024. }
  1025. static inline long long atomic64_fetch_andnot_release(long long i, atomic64_t *v)
  1026. {
  1027. return atomic64_fetch_and_release(~i, v);
  1028. }
  1029. #endif
  1030. #define atomic64_cond_read_relaxed(v, c) smp_cond_load_relaxed(&(v)->counter, (c))
  1031. #define atomic64_cond_read_acquire(v, c) smp_cond_load_acquire(&(v)->counter, (c))
  1032. #include <asm-generic/atomic-long.h>
  1033. #endif /* _LINUX_ATOMIC_H */