atomic.h 17 KB

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