New upstream version 18.02
[deb_dpdk.git] / lib / librte_eal / common / include / generic / rte_atomic.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_ATOMIC_H_
6 #define _RTE_ATOMIC_H_
7
8 /**
9  * @file
10  * Atomic Operations
11  *
12  * This file defines a generic API for atomic operations.
13  */
14
15 #include <stdint.h>
16 #include <rte_common.h>
17
18 #ifdef __DOXYGEN__
19
20 /** @name Memory Barrier
21  */
22 ///@{
23 /**
24  * General memory barrier.
25  *
26  * Guarantees that the LOAD and STORE operations generated before the
27  * barrier occur before the LOAD and STORE operations generated after.
28  * This function is architecture dependent.
29  */
30 static inline void rte_mb(void);
31
32 /**
33  * Write memory barrier.
34  *
35  * Guarantees that the STORE operations generated before the barrier
36  * occur before the STORE operations generated after.
37  * This function is architecture dependent.
38  */
39 static inline void rte_wmb(void);
40
41 /**
42  * Read memory barrier.
43  *
44  * Guarantees that the LOAD operations generated before the barrier
45  * occur before the LOAD operations generated after.
46  * This function is architecture dependent.
47  */
48 static inline void rte_rmb(void);
49 ///@}
50
51 /** @name SMP Memory Barrier
52  */
53 ///@{
54 /**
55  * General memory barrier between lcores
56  *
57  * Guarantees that the LOAD and STORE operations that precede the
58  * rte_smp_mb() call are globally visible across the lcores
59  * before the LOAD and STORE operations that follows it.
60  */
61 static inline void rte_smp_mb(void);
62
63 /**
64  * Write memory barrier between lcores
65  *
66  * Guarantees that the STORE operations that precede the
67  * rte_smp_wmb() call are globally visible across the lcores
68  * before the STORE operations that follows it.
69  */
70 static inline void rte_smp_wmb(void);
71
72 /**
73  * Read memory barrier between lcores
74  *
75  * Guarantees that the LOAD operations that precede the
76  * rte_smp_rmb() call are globally visible across the lcores
77  * before the LOAD operations that follows it.
78  */
79 static inline void rte_smp_rmb(void);
80 ///@}
81
82 /** @name I/O Memory Barrier
83  */
84 ///@{
85 /**
86  * General memory barrier for I/O device
87  *
88  * Guarantees that the LOAD and STORE operations that precede the
89  * rte_io_mb() call are visible to I/O device or CPU before the
90  * LOAD and STORE operations that follow it.
91  */
92 static inline void rte_io_mb(void);
93
94 /**
95  * Write memory barrier for I/O device
96  *
97  * Guarantees that the STORE operations that precede the
98  * rte_io_wmb() call are visible to I/O device before the STORE
99  * operations that follow it.
100  */
101 static inline void rte_io_wmb(void);
102
103 /**
104  * Read memory barrier for IO device
105  *
106  * Guarantees that the LOAD operations on I/O device that precede the
107  * rte_io_rmb() call are visible to CPU before the LOAD
108  * operations that follow it.
109  */
110 static inline void rte_io_rmb(void);
111 ///@}
112
113 /** @name Coherent I/O Memory Barrier
114  *
115  * Coherent I/O memory barrier is a lightweight version of I/O memory
116  * barriers which are system-wide data synchronization barriers. This
117  * is for only coherent memory domain between lcore and I/O device but
118  * it is same as the I/O memory barriers in most of architectures.
119  * However, some architecture provides even lighter barriers which are
120  * somewhere in between I/O memory barriers and SMP memory barriers.
121  * For example, in case of ARMv8, DMB(data memory barrier) instruction
122  * can have different shareability domains - inner-shareable and
123  * outer-shareable. And inner-shareable DMB fits for SMP memory
124  * barriers and outer-shareable DMB for coherent I/O memory barriers,
125  * which acts on coherent memory.
126  *
127  * In most cases, I/O memory barriers are safer but if operations are
128  * on coherent memory instead of incoherent MMIO region of a device,
129  * then coherent I/O memory barriers can be used and this could bring
130  * performance gain depending on architectures.
131  */
132 ///@{
133 /**
134  * Write memory barrier for coherent memory between lcore and I/O device
135  *
136  * Guarantees that the STORE operations on coherent memory that
137  * precede the rte_cio_wmb() call are visible to I/O device before the
138  * STORE operations that follow it.
139  */
140 static inline void rte_cio_wmb(void);
141
142 /**
143  * Read memory barrier for coherent memory between lcore and I/O device
144  *
145  * Guarantees that the LOAD operations on coherent memory updated by
146  * I/O device that precede the rte_cio_rmb() call are visible to CPU
147  * before the LOAD operations that follow it.
148  */
149 static inline void rte_cio_rmb(void);
150 ///@}
151
152 #endif /* __DOXYGEN__ */
153
154 /**
155  * Compiler barrier.
156  *
157  * Guarantees that operation reordering does not occur at compile time
158  * for operations directly before and after the barrier.
159  */
160 #define rte_compiler_barrier() do {             \
161         asm volatile ("" : : : "memory");       \
162 } while(0)
163
164 /*------------------------- 16 bit atomic operations -------------------------*/
165
166 /**
167  * Atomic compare and set.
168  *
169  * (atomic) equivalent to:
170  *   if (*dst == exp)
171  *     *dst = src (all 16-bit words)
172  *
173  * @param dst
174  *   The destination location into which the value will be written.
175  * @param exp
176  *   The expected value.
177  * @param src
178  *   The new value.
179  * @return
180  *   Non-zero on success; 0 on failure.
181  */
182 static inline int
183 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src);
184
185 #ifdef RTE_FORCE_INTRINSICS
186 static inline int
187 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
188 {
189         return __sync_bool_compare_and_swap(dst, exp, src);
190 }
191 #endif
192
193 /**
194  * The atomic counter structure.
195  */
196 typedef struct {
197         volatile int16_t cnt; /**< An internal counter value. */
198 } rte_atomic16_t;
199
200 /**
201  * Static initializer for an atomic counter.
202  */
203 #define RTE_ATOMIC16_INIT(val) { (val) }
204
205 /**
206  * Initialize an atomic counter.
207  *
208  * @param v
209  *   A pointer to the atomic counter.
210  */
211 static inline void
212 rte_atomic16_init(rte_atomic16_t *v)
213 {
214         v->cnt = 0;
215 }
216
217 /**
218  * Atomically read a 16-bit value from a counter.
219  *
220  * @param v
221  *   A pointer to the atomic counter.
222  * @return
223  *   The value of the counter.
224  */
225 static inline int16_t
226 rte_atomic16_read(const rte_atomic16_t *v)
227 {
228         return v->cnt;
229 }
230
231 /**
232  * Atomically set a counter to a 16-bit value.
233  *
234  * @param v
235  *   A pointer to the atomic counter.
236  * @param new_value
237  *   The new value for the counter.
238  */
239 static inline void
240 rte_atomic16_set(rte_atomic16_t *v, int16_t new_value)
241 {
242         v->cnt = new_value;
243 }
244
245 /**
246  * Atomically add a 16-bit value to an atomic counter.
247  *
248  * @param v
249  *   A pointer to the atomic counter.
250  * @param inc
251  *   The value to be added to the counter.
252  */
253 static inline void
254 rte_atomic16_add(rte_atomic16_t *v, int16_t inc)
255 {
256         __sync_fetch_and_add(&v->cnt, inc);
257 }
258
259 /**
260  * Atomically subtract a 16-bit value from an atomic counter.
261  *
262  * @param v
263  *   A pointer to the atomic counter.
264  * @param dec
265  *   The value to be subtracted from the counter.
266  */
267 static inline void
268 rte_atomic16_sub(rte_atomic16_t *v, int16_t dec)
269 {
270         __sync_fetch_and_sub(&v->cnt, dec);
271 }
272
273 /**
274  * Atomically increment a counter by one.
275  *
276  * @param v
277  *   A pointer to the atomic counter.
278  */
279 static inline void
280 rte_atomic16_inc(rte_atomic16_t *v);
281
282 #ifdef RTE_FORCE_INTRINSICS
283 static inline void
284 rte_atomic16_inc(rte_atomic16_t *v)
285 {
286         rte_atomic16_add(v, 1);
287 }
288 #endif
289
290 /**
291  * Atomically decrement a counter by one.
292  *
293  * @param v
294  *   A pointer to the atomic counter.
295  */
296 static inline void
297 rte_atomic16_dec(rte_atomic16_t *v);
298
299 #ifdef RTE_FORCE_INTRINSICS
300 static inline void
301 rte_atomic16_dec(rte_atomic16_t *v)
302 {
303         rte_atomic16_sub(v, 1);
304 }
305 #endif
306
307 /**
308  * Atomically add a 16-bit value to a counter and return the result.
309  *
310  * Atomically adds the 16-bits value (inc) to the atomic counter (v) and
311  * returns the value of v after addition.
312  *
313  * @param v
314  *   A pointer to the atomic counter.
315  * @param inc
316  *   The value to be added to the counter.
317  * @return
318  *   The value of v after the addition.
319  */
320 static inline int16_t
321 rte_atomic16_add_return(rte_atomic16_t *v, int16_t inc)
322 {
323         return __sync_add_and_fetch(&v->cnt, inc);
324 }
325
326 /**
327  * Atomically subtract a 16-bit value from a counter and return
328  * the result.
329  *
330  * Atomically subtracts the 16-bit value (inc) from the atomic counter
331  * (v) and returns the value of v after the subtraction.
332  *
333  * @param v
334  *   A pointer to the atomic counter.
335  * @param dec
336  *   The value to be subtracted from the counter.
337  * @return
338  *   The value of v after the subtraction.
339  */
340 static inline int16_t
341 rte_atomic16_sub_return(rte_atomic16_t *v, int16_t dec)
342 {
343         return __sync_sub_and_fetch(&v->cnt, dec);
344 }
345
346 /**
347  * Atomically increment a 16-bit counter by one and test.
348  *
349  * Atomically increments the atomic counter (v) by one and returns true if
350  * the result is 0, or false in all other cases.
351  *
352  * @param v
353  *   A pointer to the atomic counter.
354  * @return
355  *   True if the result after the increment operation is 0; false otherwise.
356  */
357 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v);
358
359 #ifdef RTE_FORCE_INTRINSICS
360 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
361 {
362         return __sync_add_and_fetch(&v->cnt, 1) == 0;
363 }
364 #endif
365
366 /**
367  * Atomically decrement a 16-bit counter by one and test.
368  *
369  * Atomically decrements the atomic counter (v) by one and returns true if
370  * the result is 0, or false in all other cases.
371  *
372  * @param v
373  *   A pointer to the atomic counter.
374  * @return
375  *   True if the result after the decrement operation is 0; false otherwise.
376  */
377 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v);
378
379 #ifdef RTE_FORCE_INTRINSICS
380 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
381 {
382         return __sync_sub_and_fetch(&v->cnt, 1) == 0;
383 }
384 #endif
385
386 /**
387  * Atomically test and set a 16-bit atomic counter.
388  *
389  * If the counter value is already set, return 0 (failed). Otherwise, set
390  * the counter value to 1 and return 1 (success).
391  *
392  * @param v
393  *   A pointer to the atomic counter.
394  * @return
395  *   0 if failed; else 1, success.
396  */
397 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v);
398
399 #ifdef RTE_FORCE_INTRINSICS
400 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
401 {
402         return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
403 }
404 #endif
405
406 /**
407  * Atomically set a 16-bit counter to 0.
408  *
409  * @param v
410  *   A pointer to the atomic counter.
411  */
412 static inline void rte_atomic16_clear(rte_atomic16_t *v)
413 {
414         v->cnt = 0;
415 }
416
417 /*------------------------- 32 bit atomic operations -------------------------*/
418
419 /**
420  * Atomic compare and set.
421  *
422  * (atomic) equivalent to:
423  *   if (*dst == exp)
424  *     *dst = src (all 32-bit words)
425  *
426  * @param dst
427  *   The destination location into which the value will be written.
428  * @param exp
429  *   The expected value.
430  * @param src
431  *   The new value.
432  * @return
433  *   Non-zero on success; 0 on failure.
434  */
435 static inline int
436 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src);
437
438 #ifdef RTE_FORCE_INTRINSICS
439 static inline int
440 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
441 {
442         return __sync_bool_compare_and_swap(dst, exp, src);
443 }
444 #endif
445
446 /**
447  * The atomic counter structure.
448  */
449 typedef struct {
450         volatile int32_t cnt; /**< An internal counter value. */
451 } rte_atomic32_t;
452
453 /**
454  * Static initializer for an atomic counter.
455  */
456 #define RTE_ATOMIC32_INIT(val) { (val) }
457
458 /**
459  * Initialize an atomic counter.
460  *
461  * @param v
462  *   A pointer to the atomic counter.
463  */
464 static inline void
465 rte_atomic32_init(rte_atomic32_t *v)
466 {
467         v->cnt = 0;
468 }
469
470 /**
471  * Atomically read a 32-bit value from a counter.
472  *
473  * @param v
474  *   A pointer to the atomic counter.
475  * @return
476  *   The value of the counter.
477  */
478 static inline int32_t
479 rte_atomic32_read(const rte_atomic32_t *v)
480 {
481         return v->cnt;
482 }
483
484 /**
485  * Atomically set a counter to a 32-bit value.
486  *
487  * @param v
488  *   A pointer to the atomic counter.
489  * @param new_value
490  *   The new value for the counter.
491  */
492 static inline void
493 rte_atomic32_set(rte_atomic32_t *v, int32_t new_value)
494 {
495         v->cnt = new_value;
496 }
497
498 /**
499  * Atomically add a 32-bit value to an atomic counter.
500  *
501  * @param v
502  *   A pointer to the atomic counter.
503  * @param inc
504  *   The value to be added to the counter.
505  */
506 static inline void
507 rte_atomic32_add(rte_atomic32_t *v, int32_t inc)
508 {
509         __sync_fetch_and_add(&v->cnt, inc);
510 }
511
512 /**
513  * Atomically subtract a 32-bit value from an atomic counter.
514  *
515  * @param v
516  *   A pointer to the atomic counter.
517  * @param dec
518  *   The value to be subtracted from the counter.
519  */
520 static inline void
521 rte_atomic32_sub(rte_atomic32_t *v, int32_t dec)
522 {
523         __sync_fetch_and_sub(&v->cnt, dec);
524 }
525
526 /**
527  * Atomically increment a counter by one.
528  *
529  * @param v
530  *   A pointer to the atomic counter.
531  */
532 static inline void
533 rte_atomic32_inc(rte_atomic32_t *v);
534
535 #ifdef RTE_FORCE_INTRINSICS
536 static inline void
537 rte_atomic32_inc(rte_atomic32_t *v)
538 {
539         rte_atomic32_add(v, 1);
540 }
541 #endif
542
543 /**
544  * Atomically decrement a counter by one.
545  *
546  * @param v
547  *   A pointer to the atomic counter.
548  */
549 static inline void
550 rte_atomic32_dec(rte_atomic32_t *v);
551
552 #ifdef RTE_FORCE_INTRINSICS
553 static inline void
554 rte_atomic32_dec(rte_atomic32_t *v)
555 {
556         rte_atomic32_sub(v,1);
557 }
558 #endif
559
560 /**
561  * Atomically add a 32-bit value to a counter and return the result.
562  *
563  * Atomically adds the 32-bits value (inc) to the atomic counter (v) and
564  * returns the value of v after addition.
565  *
566  * @param v
567  *   A pointer to the atomic counter.
568  * @param inc
569  *   The value to be added to the counter.
570  * @return
571  *   The value of v after the addition.
572  */
573 static inline int32_t
574 rte_atomic32_add_return(rte_atomic32_t *v, int32_t inc)
575 {
576         return __sync_add_and_fetch(&v->cnt, inc);
577 }
578
579 /**
580  * Atomically subtract a 32-bit value from a counter and return
581  * the result.
582  *
583  * Atomically subtracts the 32-bit value (inc) from the atomic counter
584  * (v) and returns the value of v after the subtraction.
585  *
586  * @param v
587  *   A pointer to the atomic counter.
588  * @param dec
589  *   The value to be subtracted from the counter.
590  * @return
591  *   The value of v after the subtraction.
592  */
593 static inline int32_t
594 rte_atomic32_sub_return(rte_atomic32_t *v, int32_t dec)
595 {
596         return __sync_sub_and_fetch(&v->cnt, dec);
597 }
598
599 /**
600  * Atomically increment a 32-bit counter by one and test.
601  *
602  * Atomically increments the atomic counter (v) by one and returns true if
603  * the result is 0, or false in all other cases.
604  *
605  * @param v
606  *   A pointer to the atomic counter.
607  * @return
608  *   True if the result after the increment operation is 0; false otherwise.
609  */
610 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v);
611
612 #ifdef RTE_FORCE_INTRINSICS
613 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
614 {
615         return __sync_add_and_fetch(&v->cnt, 1) == 0;
616 }
617 #endif
618
619 /**
620  * Atomically decrement a 32-bit counter by one and test.
621  *
622  * Atomically decrements the atomic counter (v) by one and returns true if
623  * the result is 0, or false in all other cases.
624  *
625  * @param v
626  *   A pointer to the atomic counter.
627  * @return
628  *   True if the result after the decrement operation is 0; false otherwise.
629  */
630 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v);
631
632 #ifdef RTE_FORCE_INTRINSICS
633 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
634 {
635         return __sync_sub_and_fetch(&v->cnt, 1) == 0;
636 }
637 #endif
638
639 /**
640  * Atomically test and set a 32-bit atomic counter.
641  *
642  * If the counter value is already set, return 0 (failed). Otherwise, set
643  * the counter value to 1 and return 1 (success).
644  *
645  * @param v
646  *   A pointer to the atomic counter.
647  * @return
648  *   0 if failed; else 1, success.
649  */
650 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v);
651
652 #ifdef RTE_FORCE_INTRINSICS
653 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
654 {
655         return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
656 }
657 #endif
658
659 /**
660  * Atomically set a 32-bit counter to 0.
661  *
662  * @param v
663  *   A pointer to the atomic counter.
664  */
665 static inline void rte_atomic32_clear(rte_atomic32_t *v)
666 {
667         v->cnt = 0;
668 }
669
670 /*------------------------- 64 bit atomic operations -------------------------*/
671
672 /**
673  * An atomic compare and set function used by the mutex functions.
674  * (atomic) equivalent to:
675  *   if (*dst == exp)
676  *     *dst = src (all 64-bit words)
677  *
678  * @param dst
679  *   The destination into which the value will be written.
680  * @param exp
681  *   The expected value.
682  * @param src
683  *   The new value.
684  * @return
685  *   Non-zero on success; 0 on failure.
686  */
687 static inline int
688 rte_atomic64_cmpset(volatile uint64_t *dst, uint64_t exp, uint64_t src);
689
690 #ifdef RTE_FORCE_INTRINSICS
691 static inline int
692 rte_atomic64_cmpset(volatile uint64_t *dst, uint64_t exp, uint64_t src)
693 {
694         return __sync_bool_compare_and_swap(dst, exp, src);
695 }
696 #endif
697
698 /**
699  * The atomic counter structure.
700  */
701 typedef struct {
702         volatile int64_t cnt;  /**< Internal counter value. */
703 } rte_atomic64_t;
704
705 /**
706  * Static initializer for an atomic counter.
707  */
708 #define RTE_ATOMIC64_INIT(val) { (val) }
709
710 /**
711  * Initialize the atomic counter.
712  *
713  * @param v
714  *   A pointer to the atomic counter.
715  */
716 static inline void
717 rte_atomic64_init(rte_atomic64_t *v);
718
719 #ifdef RTE_FORCE_INTRINSICS
720 static inline void
721 rte_atomic64_init(rte_atomic64_t *v)
722 {
723 #ifdef __LP64__
724         v->cnt = 0;
725 #else
726         int success = 0;
727         uint64_t tmp;
728
729         while (success == 0) {
730                 tmp = v->cnt;
731                 success = rte_atomic64_cmpset((volatile uint64_t *)&v->cnt,
732                                               tmp, 0);
733         }
734 #endif
735 }
736 #endif
737
738 /**
739  * Atomically read a 64-bit counter.
740  *
741  * @param v
742  *   A pointer to the atomic counter.
743  * @return
744  *   The value of the counter.
745  */
746 static inline int64_t
747 rte_atomic64_read(rte_atomic64_t *v);
748
749 #ifdef RTE_FORCE_INTRINSICS
750 static inline int64_t
751 rte_atomic64_read(rte_atomic64_t *v)
752 {
753 #ifdef __LP64__
754         return v->cnt;
755 #else
756         int success = 0;
757         uint64_t tmp;
758
759         while (success == 0) {
760                 tmp = v->cnt;
761                 /* replace the value by itself */
762                 success = rte_atomic64_cmpset((volatile uint64_t *)&v->cnt,
763                                               tmp, tmp);
764         }
765         return tmp;
766 #endif
767 }
768 #endif
769
770 /**
771  * Atomically set a 64-bit counter.
772  *
773  * @param v
774  *   A pointer to the atomic counter.
775  * @param new_value
776  *   The new value of the counter.
777  */
778 static inline void
779 rte_atomic64_set(rte_atomic64_t *v, int64_t new_value);
780
781 #ifdef RTE_FORCE_INTRINSICS
782 static inline void
783 rte_atomic64_set(rte_atomic64_t *v, int64_t new_value)
784 {
785 #ifdef __LP64__
786         v->cnt = new_value;
787 #else
788         int success = 0;
789         uint64_t tmp;
790
791         while (success == 0) {
792                 tmp = v->cnt;
793                 success = rte_atomic64_cmpset((volatile uint64_t *)&v->cnt,
794                                               tmp, new_value);
795         }
796 #endif
797 }
798 #endif
799
800 /**
801  * Atomically add a 64-bit value to a counter.
802  *
803  * @param v
804  *   A pointer to the atomic counter.
805  * @param inc
806  *   The value to be added to the counter.
807  */
808 static inline void
809 rte_atomic64_add(rte_atomic64_t *v, int64_t inc);
810
811 #ifdef RTE_FORCE_INTRINSICS
812 static inline void
813 rte_atomic64_add(rte_atomic64_t *v, int64_t inc)
814 {
815         __sync_fetch_and_add(&v->cnt, inc);
816 }
817 #endif
818
819 /**
820  * Atomically subtract a 64-bit value from a counter.
821  *
822  * @param v
823  *   A pointer to the atomic counter.
824  * @param dec
825  *   The value to be subtracted from the counter.
826  */
827 static inline void
828 rte_atomic64_sub(rte_atomic64_t *v, int64_t dec);
829
830 #ifdef RTE_FORCE_INTRINSICS
831 static inline void
832 rte_atomic64_sub(rte_atomic64_t *v, int64_t dec)
833 {
834         __sync_fetch_and_sub(&v->cnt, dec);
835 }
836 #endif
837
838 /**
839  * Atomically increment a 64-bit counter by one and test.
840  *
841  * @param v
842  *   A pointer to the atomic counter.
843  */
844 static inline void
845 rte_atomic64_inc(rte_atomic64_t *v);
846
847 #ifdef RTE_FORCE_INTRINSICS
848 static inline void
849 rte_atomic64_inc(rte_atomic64_t *v)
850 {
851         rte_atomic64_add(v, 1);
852 }
853 #endif
854
855 /**
856  * Atomically decrement a 64-bit counter by one and test.
857  *
858  * @param v
859  *   A pointer to the atomic counter.
860  */
861 static inline void
862 rte_atomic64_dec(rte_atomic64_t *v);
863
864 #ifdef RTE_FORCE_INTRINSICS
865 static inline void
866 rte_atomic64_dec(rte_atomic64_t *v)
867 {
868         rte_atomic64_sub(v, 1);
869 }
870 #endif
871
872 /**
873  * Add a 64-bit value to an atomic counter and return the result.
874  *
875  * Atomically adds the 64-bit value (inc) to the atomic counter (v) and
876  * returns the value of v after the addition.
877  *
878  * @param v
879  *   A pointer to the atomic counter.
880  * @param inc
881  *   The value to be added to the counter.
882  * @return
883  *   The value of v after the addition.
884  */
885 static inline int64_t
886 rte_atomic64_add_return(rte_atomic64_t *v, int64_t inc);
887
888 #ifdef RTE_FORCE_INTRINSICS
889 static inline int64_t
890 rte_atomic64_add_return(rte_atomic64_t *v, int64_t inc)
891 {
892         return __sync_add_and_fetch(&v->cnt, inc);
893 }
894 #endif
895
896 /**
897  * Subtract a 64-bit value from an atomic counter and return the result.
898  *
899  * Atomically subtracts the 64-bit value (dec) from the atomic counter (v)
900  * and returns the value of v after the subtraction.
901  *
902  * @param v
903  *   A pointer to the atomic counter.
904  * @param dec
905  *   The value to be subtracted from the counter.
906  * @return
907  *   The value of v after the subtraction.
908  */
909 static inline int64_t
910 rte_atomic64_sub_return(rte_atomic64_t *v, int64_t dec);
911
912 #ifdef RTE_FORCE_INTRINSICS
913 static inline int64_t
914 rte_atomic64_sub_return(rte_atomic64_t *v, int64_t dec)
915 {
916         return __sync_sub_and_fetch(&v->cnt, dec);
917 }
918 #endif
919
920 /**
921  * Atomically increment a 64-bit counter by one and test.
922  *
923  * Atomically increments the atomic counter (v) by one and returns
924  * true if the result is 0, or false in all other cases.
925  *
926  * @param v
927  *   A pointer to the atomic counter.
928  * @return
929  *   True if the result after the addition is 0; false otherwise.
930  */
931 static inline int rte_atomic64_inc_and_test(rte_atomic64_t *v);
932
933 #ifdef RTE_FORCE_INTRINSICS
934 static inline int rte_atomic64_inc_and_test(rte_atomic64_t *v)
935 {
936         return rte_atomic64_add_return(v, 1) == 0;
937 }
938 #endif
939
940 /**
941  * Atomically decrement a 64-bit counter by one and test.
942  *
943  * Atomically decrements the atomic counter (v) by one and returns true if
944  * the result is 0, or false in all other cases.
945  *
946  * @param v
947  *   A pointer to the atomic counter.
948  * @return
949  *   True if the result after subtraction is 0; false otherwise.
950  */
951 static inline int rte_atomic64_dec_and_test(rte_atomic64_t *v);
952
953 #ifdef RTE_FORCE_INTRINSICS
954 static inline int rte_atomic64_dec_and_test(rte_atomic64_t *v)
955 {
956         return rte_atomic64_sub_return(v, 1) == 0;
957 }
958 #endif
959
960 /**
961  * Atomically test and set a 64-bit atomic counter.
962  *
963  * If the counter value is already set, return 0 (failed). Otherwise, set
964  * the counter value to 1 and return 1 (success).
965  *
966  * @param v
967  *   A pointer to the atomic counter.
968  * @return
969  *   0 if failed; else 1, success.
970  */
971 static inline int rte_atomic64_test_and_set(rte_atomic64_t *v);
972
973 #ifdef RTE_FORCE_INTRINSICS
974 static inline int rte_atomic64_test_and_set(rte_atomic64_t *v)
975 {
976         return rte_atomic64_cmpset((volatile uint64_t *)&v->cnt, 0, 1);
977 }
978 #endif
979
980 /**
981  * Atomically set a 64-bit counter to 0.
982  *
983  * @param v
984  *   A pointer to the atomic counter.
985  */
986 static inline void rte_atomic64_clear(rte_atomic64_t *v);
987
988 #ifdef RTE_FORCE_INTRINSICS
989 static inline void rte_atomic64_clear(rte_atomic64_t *v)
990 {
991         rte_atomic64_set(v, 0);
992 }
993 #endif
994
995 #endif /* _RTE_ATOMIC_H_ */