atomic.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Atomic operations that C can't guarantee us. Useful for
  3. * resource counting etc..
  4. *
  5. * But use these as seldom as possible since they are much more slower
  6. * than regular operations.
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. *
  12. * Copyright (C) 1996, 97, 99, 2000, 03, 04, 06 by Ralf Baechle
  13. */
  14. #ifndef _ASM_ATOMIC_H
  15. #define _ASM_ATOMIC_H
  16. #include <linux/irqflags.h>
  17. #include <linux/types.h>
  18. #include <asm/barrier.h>
  19. #include <asm/cpu-features.h>
  20. #include <asm/war.h>
  21. #include <asm/system.h>
  22. #define ATOMIC_INIT(i) { (i) }
  23. /*
  24. * atomic_read - read atomic variable
  25. * @v: pointer of type atomic_t
  26. *
  27. * Atomically reads the value of @v.
  28. */
  29. #define atomic_read(v) (*(volatile int *)&(v)->counter)
  30. /*
  31. * atomic_set - set atomic variable
  32. * @v: pointer of type atomic_t
  33. * @i: required value
  34. *
  35. * Atomically sets the value of @v to @i.
  36. */
  37. #define atomic_set(v, i) ((v)->counter = (i))
  38. /*
  39. * atomic_add - add integer to atomic variable
  40. * @i: integer value to add
  41. * @v: pointer of type atomic_t
  42. *
  43. * Atomically adds @i to @v.
  44. */
  45. static __inline__ void atomic_add(int i, atomic_t * v)
  46. {
  47. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  48. int temp;
  49. __asm__ __volatile__(
  50. " .set mips3 \n"
  51. "1: ll %0, %1 # atomic_add \n"
  52. " addu %0, %2 \n"
  53. " sc %0, %1 \n"
  54. " beqzl %0, 1b \n"
  55. " .set mips0 \n"
  56. : "=&r" (temp), "=m" (v->counter)
  57. : "Ir" (i), "m" (v->counter));
  58. } else if (kernel_uses_llsc) {
  59. int temp;
  60. do {
  61. __asm__ __volatile__(
  62. " .set mips3 \n"
  63. " ll %0, %1 # atomic_add \n"
  64. " addu %0, %2 \n"
  65. " sc %0, %1 \n"
  66. " .set mips0 \n"
  67. : "=&r" (temp), "=m" (v->counter)
  68. : "Ir" (i), "m" (v->counter));
  69. } while (unlikely(!temp));
  70. } else {
  71. unsigned long flags;
  72. raw_local_irq_save(flags);
  73. v->counter += i;
  74. raw_local_irq_restore(flags);
  75. }
  76. }
  77. /*
  78. * atomic_sub - subtract the atomic variable
  79. * @i: integer value to subtract
  80. * @v: pointer of type atomic_t
  81. *
  82. * Atomically subtracts @i from @v.
  83. */
  84. static __inline__ void atomic_sub(int i, atomic_t * v)
  85. {
  86. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  87. int temp;
  88. __asm__ __volatile__(
  89. " .set mips3 \n"
  90. "1: ll %0, %1 # atomic_sub \n"
  91. " subu %0, %2 \n"
  92. " sc %0, %1 \n"
  93. " beqzl %0, 1b \n"
  94. " .set mips0 \n"
  95. : "=&r" (temp), "=m" (v->counter)
  96. : "Ir" (i), "m" (v->counter));
  97. } else if (kernel_uses_llsc) {
  98. int temp;
  99. do {
  100. __asm__ __volatile__(
  101. " .set mips3 \n"
  102. " ll %0, %1 # atomic_sub \n"
  103. " subu %0, %2 \n"
  104. " sc %0, %1 \n"
  105. " .set mips0 \n"
  106. : "=&r" (temp), "=m" (v->counter)
  107. : "Ir" (i), "m" (v->counter));
  108. } while (unlikely(!temp));
  109. } else {
  110. unsigned long flags;
  111. raw_local_irq_save(flags);
  112. v->counter -= i;
  113. raw_local_irq_restore(flags);
  114. }
  115. }
  116. /*
  117. * Same as above, but return the result value
  118. */
  119. static __inline__ int atomic_add_return(int i, atomic_t * v)
  120. {
  121. int result;
  122. smp_mb__before_llsc();
  123. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  124. int temp;
  125. __asm__ __volatile__(
  126. " .set mips3 \n"
  127. "1: ll %1, %2 # atomic_add_return \n"
  128. " addu %0, %1, %3 \n"
  129. " sc %0, %2 \n"
  130. " beqzl %0, 1b \n"
  131. " addu %0, %1, %3 \n"
  132. " .set mips0 \n"
  133. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  134. : "Ir" (i), "m" (v->counter)
  135. : "memory");
  136. } else if (kernel_uses_llsc) {
  137. int temp;
  138. do {
  139. __asm__ __volatile__(
  140. " .set mips3 \n"
  141. " ll %1, %2 # atomic_add_return \n"
  142. " addu %0, %1, %3 \n"
  143. " sc %0, %2 \n"
  144. " .set mips0 \n"
  145. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  146. : "Ir" (i), "m" (v->counter)
  147. : "memory");
  148. } while (unlikely(!result));
  149. result = temp + i;
  150. } else {
  151. unsigned long flags;
  152. raw_local_irq_save(flags);
  153. result = v->counter;
  154. result += i;
  155. v->counter = result;
  156. raw_local_irq_restore(flags);
  157. }
  158. smp_llsc_mb();
  159. return result;
  160. }
  161. static __inline__ int atomic_sub_return(int i, atomic_t * v)
  162. {
  163. int result;
  164. smp_mb__before_llsc();
  165. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  166. int temp;
  167. __asm__ __volatile__(
  168. " .set mips3 \n"
  169. "1: ll %1, %2 # atomic_sub_return \n"
  170. " subu %0, %1, %3 \n"
  171. " sc %0, %2 \n"
  172. " beqzl %0, 1b \n"
  173. " subu %0, %1, %3 \n"
  174. " .set mips0 \n"
  175. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  176. : "Ir" (i), "m" (v->counter)
  177. : "memory");
  178. result = temp - i;
  179. } else if (kernel_uses_llsc) {
  180. int temp;
  181. do {
  182. __asm__ __volatile__(
  183. " .set mips3 \n"
  184. " ll %1, %2 # atomic_sub_return \n"
  185. " subu %0, %1, %3 \n"
  186. " sc %0, %2 \n"
  187. " .set mips0 \n"
  188. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  189. : "Ir" (i), "m" (v->counter)
  190. : "memory");
  191. } while (unlikely(!result));
  192. result = temp - i;
  193. } else {
  194. unsigned long flags;
  195. raw_local_irq_save(flags);
  196. result = v->counter;
  197. result -= i;
  198. v->counter = result;
  199. raw_local_irq_restore(flags);
  200. }
  201. smp_llsc_mb();
  202. return result;
  203. }
  204. /*
  205. * atomic_sub_if_positive - conditionally subtract integer from atomic variable
  206. * @i: integer value to subtract
  207. * @v: pointer of type atomic_t
  208. *
  209. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  210. * The function returns the old value of @v minus @i.
  211. */
  212. static __inline__ int atomic_sub_if_positive(int i, atomic_t * v)
  213. {
  214. int result;
  215. smp_mb__before_llsc();
  216. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  217. int temp;
  218. __asm__ __volatile__(
  219. " .set mips3 \n"
  220. "1: ll %1, %2 # atomic_sub_if_positive\n"
  221. " subu %0, %1, %3 \n"
  222. " bltz %0, 1f \n"
  223. " sc %0, %2 \n"
  224. " .set noreorder \n"
  225. " beqzl %0, 1b \n"
  226. " subu %0, %1, %3 \n"
  227. " .set reorder \n"
  228. "1: \n"
  229. " .set mips0 \n"
  230. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  231. : "Ir" (i), "m" (v->counter)
  232. : "memory");
  233. } else if (kernel_uses_llsc) {
  234. int temp;
  235. __asm__ __volatile__(
  236. " .set mips3 \n"
  237. "1: ll %1, %2 # atomic_sub_if_positive\n"
  238. " subu %0, %1, %3 \n"
  239. " bltz %0, 1f \n"
  240. " sc %0, %2 \n"
  241. " .set noreorder \n"
  242. " beqz %0, 1b \n"
  243. " subu %0, %1, %3 \n"
  244. " .set reorder \n"
  245. "1: \n"
  246. " .set mips0 \n"
  247. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  248. : "Ir" (i), "m" (v->counter)
  249. : "memory");
  250. } else {
  251. unsigned long flags;
  252. raw_local_irq_save(flags);
  253. result = v->counter;
  254. result -= i;
  255. if (result >= 0)
  256. v->counter = result;
  257. raw_local_irq_restore(flags);
  258. }
  259. smp_llsc_mb();
  260. return result;
  261. }
  262. #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
  263. #define atomic_xchg(v, new) (xchg(&((v)->counter), (new)))
  264. /**
  265. * __atomic_add_unless - add unless the number is a given value
  266. * @v: pointer of type atomic_t
  267. * @a: the amount to add to v...
  268. * @u: ...unless v is equal to u.
  269. *
  270. * Atomically adds @a to @v, so long as it was not @u.
  271. * Returns the old value of @v.
  272. */
  273. static __inline__ int __atomic_add_unless(atomic_t *v, int a, int u)
  274. {
  275. int c, old;
  276. c = atomic_read(v);
  277. for (;;) {
  278. if (unlikely(c == (u)))
  279. break;
  280. old = atomic_cmpxchg((v), c, c + (a));
  281. if (likely(old == c))
  282. break;
  283. c = old;
  284. }
  285. return c;
  286. }
  287. #define atomic_dec_return(v) atomic_sub_return(1, (v))
  288. #define atomic_inc_return(v) atomic_add_return(1, (v))
  289. /*
  290. * atomic_sub_and_test - subtract value from variable and test result
  291. * @i: integer value to subtract
  292. * @v: pointer of type atomic_t
  293. *
  294. * Atomically subtracts @i from @v and returns
  295. * true if the result is zero, or false for all
  296. * other cases.
  297. */
  298. #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
  299. /*
  300. * atomic_inc_and_test - increment and test
  301. * @v: pointer of type atomic_t
  302. *
  303. * Atomically increments @v by 1
  304. * and returns true if the result is zero, or false for all
  305. * other cases.
  306. */
  307. #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
  308. /*
  309. * atomic_dec_and_test - decrement by 1 and test
  310. * @v: pointer of type atomic_t
  311. *
  312. * Atomically decrements @v by 1 and
  313. * returns true if the result is 0, or false for all other
  314. * cases.
  315. */
  316. #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
  317. /*
  318. * atomic_dec_if_positive - decrement by 1 if old value positive
  319. * @v: pointer of type atomic_t
  320. */
  321. #define atomic_dec_if_positive(v) atomic_sub_if_positive(1, v)
  322. /*
  323. * atomic_inc - increment atomic variable
  324. * @v: pointer of type atomic_t
  325. *
  326. * Atomically increments @v by 1.
  327. */
  328. #define atomic_inc(v) atomic_add(1, (v))
  329. /*
  330. * atomic_dec - decrement and test
  331. * @v: pointer of type atomic_t
  332. *
  333. * Atomically decrements @v by 1.
  334. */
  335. #define atomic_dec(v) atomic_sub(1, (v))
  336. /*
  337. * atomic_add_negative - add and test if negative
  338. * @v: pointer of type atomic_t
  339. * @i: integer value to add
  340. *
  341. * Atomically adds @i to @v and returns true
  342. * if the result is negative, or false when
  343. * result is greater than or equal to zero.
  344. */
  345. #define atomic_add_negative(i, v) (atomic_add_return(i, (v)) < 0)
  346. #ifdef CONFIG_64BIT
  347. #define ATOMIC64_INIT(i) { (i) }
  348. /*
  349. * atomic64_read - read atomic variable
  350. * @v: pointer of type atomic64_t
  351. *
  352. */
  353. #define atomic64_read(v) (*(volatile long *)&(v)->counter)
  354. /*
  355. * atomic64_set - set atomic variable
  356. * @v: pointer of type atomic64_t
  357. * @i: required value
  358. */
  359. #define atomic64_set(v, i) ((v)->counter = (i))
  360. /*
  361. * atomic64_add - add integer to atomic variable
  362. * @i: integer value to add
  363. * @v: pointer of type atomic64_t
  364. *
  365. * Atomically adds @i to @v.
  366. */
  367. static __inline__ void atomic64_add(long i, atomic64_t * v)
  368. {
  369. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  370. long temp;
  371. __asm__ __volatile__(
  372. " .set mips3 \n"
  373. "1: lld %0, %1 # atomic64_add \n"
  374. " daddu %0, %2 \n"
  375. " scd %0, %1 \n"
  376. " beqzl %0, 1b \n"
  377. " .set mips0 \n"
  378. : "=&r" (temp), "=m" (v->counter)
  379. : "Ir" (i), "m" (v->counter));
  380. } else if (kernel_uses_llsc) {
  381. long temp;
  382. do {
  383. __asm__ __volatile__(
  384. " .set mips3 \n"
  385. " lld %0, %1 # atomic64_add \n"
  386. " daddu %0, %2 \n"
  387. " scd %0, %1 \n"
  388. " .set mips0 \n"
  389. : "=&r" (temp), "=m" (v->counter)
  390. : "Ir" (i), "m" (v->counter));
  391. } while (unlikely(!temp));
  392. } else {
  393. unsigned long flags;
  394. raw_local_irq_save(flags);
  395. v->counter += i;
  396. raw_local_irq_restore(flags);
  397. }
  398. }
  399. /*
  400. * atomic64_sub - subtract the atomic variable
  401. * @i: integer value to subtract
  402. * @v: pointer of type atomic64_t
  403. *
  404. * Atomically subtracts @i from @v.
  405. */
  406. static __inline__ void atomic64_sub(long i, atomic64_t * v)
  407. {
  408. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  409. long temp;
  410. __asm__ __volatile__(
  411. " .set mips3 \n"
  412. "1: lld %0, %1 # atomic64_sub \n"
  413. " dsubu %0, %2 \n"
  414. " scd %0, %1 \n"
  415. " beqzl %0, 1b \n"
  416. " .set mips0 \n"
  417. : "=&r" (temp), "=m" (v->counter)
  418. : "Ir" (i), "m" (v->counter));
  419. } else if (kernel_uses_llsc) {
  420. long temp;
  421. do {
  422. __asm__ __volatile__(
  423. " .set mips3 \n"
  424. " lld %0, %1 # atomic64_sub \n"
  425. " dsubu %0, %2 \n"
  426. " scd %0, %1 \n"
  427. " .set mips0 \n"
  428. : "=&r" (temp), "=m" (v->counter)
  429. : "Ir" (i), "m" (v->counter));
  430. } while (unlikely(!temp));
  431. } else {
  432. unsigned long flags;
  433. raw_local_irq_save(flags);
  434. v->counter -= i;
  435. raw_local_irq_restore(flags);
  436. }
  437. }
  438. /*
  439. * Same as above, but return the result value
  440. */
  441. static __inline__ long atomic64_add_return(long i, atomic64_t * v)
  442. {
  443. long result;
  444. smp_mb__before_llsc();
  445. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  446. long temp;
  447. __asm__ __volatile__(
  448. " .set mips3 \n"
  449. "1: lld %1, %2 # atomic64_add_return \n"
  450. " daddu %0, %1, %3 \n"
  451. " scd %0, %2 \n"
  452. " beqzl %0, 1b \n"
  453. " daddu %0, %1, %3 \n"
  454. " .set mips0 \n"
  455. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  456. : "Ir" (i), "m" (v->counter)
  457. : "memory");
  458. } else if (kernel_uses_llsc) {
  459. long temp;
  460. do {
  461. __asm__ __volatile__(
  462. " .set mips3 \n"
  463. " lld %1, %2 # atomic64_add_return \n"
  464. " daddu %0, %1, %3 \n"
  465. " scd %0, %2 \n"
  466. " .set mips0 \n"
  467. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  468. : "Ir" (i), "m" (v->counter)
  469. : "memory");
  470. } while (unlikely(!result));
  471. result = temp + i;
  472. } else {
  473. unsigned long flags;
  474. raw_local_irq_save(flags);
  475. result = v->counter;
  476. result += i;
  477. v->counter = result;
  478. raw_local_irq_restore(flags);
  479. }
  480. smp_llsc_mb();
  481. return result;
  482. }
  483. static __inline__ long atomic64_sub_return(long i, atomic64_t * v)
  484. {
  485. long result;
  486. smp_mb__before_llsc();
  487. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  488. long temp;
  489. __asm__ __volatile__(
  490. " .set mips3 \n"
  491. "1: lld %1, %2 # atomic64_sub_return \n"
  492. " dsubu %0, %1, %3 \n"
  493. " scd %0, %2 \n"
  494. " beqzl %0, 1b \n"
  495. " dsubu %0, %1, %3 \n"
  496. " .set mips0 \n"
  497. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  498. : "Ir" (i), "m" (v->counter)
  499. : "memory");
  500. } else if (kernel_uses_llsc) {
  501. long temp;
  502. do {
  503. __asm__ __volatile__(
  504. " .set mips3 \n"
  505. " lld %1, %2 # atomic64_sub_return \n"
  506. " dsubu %0, %1, %3 \n"
  507. " scd %0, %2 \n"
  508. " .set mips0 \n"
  509. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  510. : "Ir" (i), "m" (v->counter)
  511. : "memory");
  512. } while (unlikely(!result));
  513. result = temp - i;
  514. } else {
  515. unsigned long flags;
  516. raw_local_irq_save(flags);
  517. result = v->counter;
  518. result -= i;
  519. v->counter = result;
  520. raw_local_irq_restore(flags);
  521. }
  522. smp_llsc_mb();
  523. return result;
  524. }
  525. /*
  526. * atomic64_sub_if_positive - conditionally subtract integer from atomic variable
  527. * @i: integer value to subtract
  528. * @v: pointer of type atomic64_t
  529. *
  530. * Atomically test @v and subtract @i if @v is greater or equal than @i.
  531. * The function returns the old value of @v minus @i.
  532. */
  533. static __inline__ long atomic64_sub_if_positive(long i, atomic64_t * v)
  534. {
  535. long result;
  536. smp_mb__before_llsc();
  537. if (kernel_uses_llsc && R10000_LLSC_WAR) {
  538. long temp;
  539. __asm__ __volatile__(
  540. " .set mips3 \n"
  541. "1: lld %1, %2 # atomic64_sub_if_positive\n"
  542. " dsubu %0, %1, %3 \n"
  543. " bltz %0, 1f \n"
  544. " scd %0, %2 \n"
  545. " .set noreorder \n"
  546. " beqzl %0, 1b \n"
  547. " dsubu %0, %1, %3 \n"
  548. " .set reorder \n"
  549. "1: \n"
  550. " .set mips0 \n"
  551. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  552. : "Ir" (i), "m" (v->counter)
  553. : "memory");
  554. } else if (kernel_uses_llsc) {
  555. long temp;
  556. __asm__ __volatile__(
  557. " .set mips3 \n"
  558. "1: lld %1, %2 # atomic64_sub_if_positive\n"
  559. " dsubu %0, %1, %3 \n"
  560. " bltz %0, 1f \n"
  561. " scd %0, %2 \n"
  562. " .set noreorder \n"
  563. " beqz %0, 1b \n"
  564. " dsubu %0, %1, %3 \n"
  565. " .set reorder \n"
  566. "1: \n"
  567. " .set mips0 \n"
  568. : "=&r" (result), "=&r" (temp), "=m" (v->counter)
  569. : "Ir" (i), "m" (v->counter)
  570. : "memory");
  571. } else {
  572. unsigned long flags;
  573. raw_local_irq_save(flags);
  574. result = v->counter;
  575. result -= i;
  576. if (result >= 0)
  577. v->counter = result;
  578. raw_local_irq_restore(flags);
  579. }
  580. smp_llsc_mb();
  581. return result;
  582. }
  583. #define atomic64_cmpxchg(v, o, n) \
  584. ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
  585. #define atomic64_xchg(v, new) (xchg(&((v)->counter), (new)))
  586. /**
  587. * atomic64_add_unless - add unless the number is a given value
  588. * @v: pointer of type atomic64_t
  589. * @a: the amount to add to v...
  590. * @u: ...unless v is equal to u.
  591. *
  592. * Atomically adds @a to @v, so long as it was not @u.
  593. * Returns the old value of @v.
  594. */
  595. static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u)
  596. {
  597. long c, old;
  598. c = atomic64_read(v);
  599. for (;;) {
  600. if (unlikely(c == (u)))
  601. break;
  602. old = atomic64_cmpxchg((v), c, c + (a));
  603. if (likely(old == c))
  604. break;
  605. c = old;
  606. }
  607. return c != (u);
  608. }
  609. #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
  610. #define atomic64_dec_return(v) atomic64_sub_return(1, (v))
  611. #define atomic64_inc_return(v) atomic64_add_return(1, (v))
  612. /*
  613. * atomic64_sub_and_test - subtract value from variable and test result
  614. * @i: integer value to subtract
  615. * @v: pointer of type atomic64_t
  616. *
  617. * Atomically subtracts @i from @v and returns
  618. * true if the result is zero, or false for all
  619. * other cases.
  620. */
  621. #define atomic64_sub_and_test(i, v) (atomic64_sub_return((i), (v)) == 0)
  622. /*
  623. * atomic64_inc_and_test - increment and test
  624. * @v: pointer of type atomic64_t
  625. *
  626. * Atomically increments @v by 1
  627. * and returns true if the result is zero, or false for all
  628. * other cases.
  629. */
  630. #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
  631. /*
  632. * atomic64_dec_and_test - decrement by 1 and test
  633. * @v: pointer of type atomic64_t
  634. *
  635. * Atomically decrements @v by 1 and
  636. * returns true if the result is 0, or false for all other
  637. * cases.
  638. */
  639. #define atomic64_dec_and_test(v) (atomic64_sub_return(1, (v)) == 0)
  640. /*
  641. * atomic64_dec_if_positive - decrement by 1 if old value positive
  642. * @v: pointer of type atomic64_t
  643. */
  644. #define atomic64_dec_if_positive(v) atomic64_sub_if_positive(1, v)
  645. /*
  646. * atomic64_inc - increment atomic variable
  647. * @v: pointer of type atomic64_t
  648. *
  649. * Atomically increments @v by 1.
  650. */
  651. #define atomic64_inc(v) atomic64_add(1, (v))
  652. /*
  653. * atomic64_dec - decrement and test
  654. * @v: pointer of type atomic64_t
  655. *
  656. * Atomically decrements @v by 1.
  657. */
  658. #define atomic64_dec(v) atomic64_sub(1, (v))
  659. /*
  660. * atomic64_add_negative - add and test if negative
  661. * @v: pointer of type atomic64_t
  662. * @i: integer value to add
  663. *
  664. * Atomically adds @i to @v and returns true
  665. * if the result is negative, or false when
  666. * result is greater than or equal to zero.
  667. */
  668. #define atomic64_add_negative(i, v) (atomic64_add_return(i, (v)) < 0)
  669. #endif /* CONFIG_64BIT */
  670. /*
  671. * atomic*_return operations are serializing but not the non-*_return
  672. * versions.
  673. */
  674. #define smp_mb__before_atomic_dec() smp_mb__before_llsc()
  675. #define smp_mb__after_atomic_dec() smp_llsc_mb()
  676. #define smp_mb__before_atomic_inc() smp_mb__before_llsc()
  677. #define smp_mb__after_atomic_inc() smp_llsc_mb()
  678. #endif /* _ASM_ATOMIC_H */