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