4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef _RTE_ATOMIC_H_
36 #define _RTE_ATOMIC_H_
42 * This file defines a generic API for atomic
43 * operations. The implementation is architecture-specific.
45 * See lib/librte_eal/common/include/i686/arch/rte_atomic.h
46 * See lib/librte_eal/common/include/x86_64/arch/rte_atomic.h
55 #include "arch/rte_atomic.h"
61 * General memory barrier.
63 * Guarantees that the LOAD and STORE operations generated before the
64 * barrier occur before the LOAD and STORE operations generated after.
66 #define rte_mb() asm volatile("mfence;" : : : "memory")
69 * Write memory barrier.
71 * Guarantees that the STORE operations generated before the barrier
72 * occur before the STORE operations generated after.
74 #define rte_wmb() asm volatile("sfence;" : : : "memory")
77 * Read memory barrier.
79 * Guarantees that the LOAD operations generated before the barrier
80 * occur before the LOAD operations generated after.
82 #define rte_rmb() asm volatile("lfence;" : : : "memory")
84 /*------------------------- 16 bit atomic operations -------------------------*/
87 * Atomic compare and set.
89 * (atomic) equivalent to:
91 * *dst = src (all 16-bit words)
94 * The destination location into which the value will be written.
100 * Non-zero on success; 0 on failure.
103 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src);
106 * The atomic counter structure.
109 volatile int16_t cnt; /**< An internal counter value. */
113 * Static initializer for an atomic counter.
115 #define RTE_ATOMIC16_INIT(val) { (val) }
118 * Initialize an atomic counter.
121 * A pointer to the atomic counter.
124 rte_atomic16_init(rte_atomic16_t *v)
130 * Atomically read a 16-bit value from a counter.
133 * A pointer to the atomic counter.
135 * The value of the counter.
137 static inline int16_t
138 rte_atomic16_read(const rte_atomic16_t *v);
141 * Atomically set a counter to a 16-bit value.
144 * A pointer to the atomic counter.
146 * The new value for the counter.
149 rte_atomic16_set(rte_atomic16_t *v, int16_t new_value);
152 * Atomically add a 16-bit value to an atomic counter.
155 * A pointer to the atomic counter.
157 * The value to be added to the counter.
160 rte_atomic16_add(rte_atomic16_t *v, int16_t inc);
163 * Atomically subtract a 16-bit value from an atomic counter.
166 * A pointer to the atomic counter.
168 * The value to be subtracted from the counter.
171 rte_atomic16_sub(rte_atomic16_t *v, int16_t dec);
174 * Atomically increment a counter by one.
177 * A pointer to the atomic counter.
180 rte_atomic16_inc(rte_atomic16_t *v);
183 * Atomically decrement a counter by one.
186 * A pointer to the atomic counter.
189 rte_atomic16_dec(rte_atomic16_t *v);
192 * Atomically add a 16-bit value to a counter and return the result.
194 * Atomically adds the 16-bits value (inc) to the atomic counter (v) and
195 * returns the value of v after addition.
198 * A pointer to the atomic counter.
200 * The value to be added to the counter.
202 * The value of v after the addition.
204 static inline int16_t
205 rte_atomic16_add_return(rte_atomic16_t *v, int16_t inc);
208 * Atomically subtract a 16-bit value from a counter and return
211 * Atomically subtracts the 16-bit value (inc) from the atomic counter
212 * (v) and returns the value of v after the subtraction.
215 * A pointer to the atomic counter.
217 * The value to be subtracted from the counter.
219 * The value of v after the subtraction.
221 static inline int16_t
222 rte_atomic16_sub_return(rte_atomic16_t *v, int16_t dec);
225 * Atomically increment a 16-bit counter by one and test.
227 * Atomically increments the atomic counter (v) by one and returns true if
228 * the result is 0, or false in all other cases.
231 * A pointer to the atomic counter.
233 * True if the result after the increment operation is 0; false otherwise.
236 rte_atomic16_inc_and_test(rte_atomic16_t *v);
239 * Atomically decrement a 16-bit counter by one and test.
241 * Atomically decrements the atomic counter (v) by one and returns true if
242 * the result is 0, or false in all other cases.
245 * A pointer to the atomic counter.
247 * True if the result after the decrement operation is 0; false otherwise.
250 rte_atomic16_dec_and_test(rte_atomic16_t *v);
253 * Atomically test and set a 16-bit atomic counter.
255 * If the counter value is already set, return 0 (failed). Otherwise, set
256 * the counter value to 1 and return 1 (success).
259 * A pointer to the atomic counter.
261 * 0 if failed; else 1, success.
264 rte_atomic16_test_and_set(rte_atomic16_t *v);
267 * Atomically set a 16-bit counter to 0.
270 * A pointer to the atomic counter.
273 rte_atomic16_clear(rte_atomic16_t *v);
275 /*------------------------- 32 bit atomic operations -------------------------*/
278 * Atomic compare and set.
280 * (atomic) equivalent to:
282 * *dst = src (all 32-bit words)
285 * The destination location into which the value will be written.
287 * The expected value.
291 * Non-zero on success; 0 on failure.
294 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src);
297 * The atomic counter structure.
300 volatile int32_t cnt; /**< An internal counter value. */
304 * Static initializer for an atomic counter.
306 #define RTE_ATOMIC32_INIT(val) { (val) }
309 * Initialize an atomic counter.
312 * A pointer to the atomic counter.
315 rte_atomic32_init(rte_atomic32_t *v);
318 * Atomically read a 32-bit value from a counter.
321 * A pointer to the atomic counter.
323 * The value of the counter.
325 static inline int32_t
326 rte_atomic32_read(const rte_atomic32_t *v);
329 * Atomically set a counter to a 32-bit value.
332 * A pointer to the atomic counter.
334 * The new value for the counter.
337 rte_atomic32_set(rte_atomic32_t *v, int32_t new_value);
340 * Atomically add a 32-bit value to an atomic counter.
343 * A pointer to the atomic counter.
345 * The value to be added to the counter.
348 rte_atomic32_add(rte_atomic32_t *v, int32_t inc);
351 * Atomically subtract a 32-bit value from an atomic counter.
354 * A pointer to the atomic counter.
356 * The value to be subtracted from the counter.
359 rte_atomic32_sub(rte_atomic32_t *v, int32_t dec);
362 * Atomically increment a counter by one.
365 * A pointer to the atomic counter.
368 rte_atomic32_inc(rte_atomic32_t *v);
371 * Atomically decrement a counter by one.
374 * A pointer to the atomic counter.
377 rte_atomic32_dec(rte_atomic32_t *v);
380 * Atomically add a 32-bit value to a counter and return the result.
382 * Atomically adds the 32-bits value (inc) to the atomic counter (v) and
383 * returns the value of v after addition.
386 * A pointer to the atomic counter.
388 * The value to be added to the counter.
390 * The value of v after the addition.
392 static inline int32_t
393 rte_atomic32_add_return(rte_atomic32_t *v, int32_t inc);
396 * Atomically subtract a 32-bit value from a counter and return
399 * Atomically subtracts the 32-bit value (inc) from the atomic counter
400 * (v) and returns the value of v after the subtraction.
403 * A pointer to the atomic counter.
405 * The value to be subtracted from the counter.
407 * The value of v after the subtraction.
409 static inline int32_t
410 rte_atomic32_sub_return(rte_atomic32_t *v, int32_t dec);
413 * Atomically increment a 32-bit counter by one and test.
415 * Atomically increments the atomic counter (v) by one and returns true if
416 * the result is 0, or false in all other cases.
419 * A pointer to the atomic counter.
421 * True if the result after the increment operation is 0; false otherwise.
424 rte_atomic32_inc_and_test(rte_atomic32_t *v);
427 * Atomically decrement a 32-bit counter by one and test.
429 * Atomically decrements the atomic counter (v) by one and returns true if
430 * the result is 0, or false in all other cases.
433 * A pointer to the atomic counter.
435 * True if the result after the decrement operation is 0; false otherwise.
438 rte_atomic32_dec_and_test(rte_atomic32_t *v);
441 * Atomically test and set a 32-bit atomic counter.
443 * If the counter value is already set, return 0 (failed). Otherwise, set
444 * the counter value to 1 and return 1 (success).
447 * A pointer to the atomic counter.
449 * 0 if failed; else 1, success.
452 rte_atomic32_test_and_set(rte_atomic32_t *v);
455 * Atomically set a 32-bit counter to 0.
458 * A pointer to the atomic counter.
461 rte_atomic32_clear(rte_atomic32_t *v);
463 /*------------------------- 64 bit atomic operations -------------------------*/
466 * An atomic compare and set function used by the mutex functions.
467 * (atomic) equivalent to:
469 * *dst = src (all 64-bit words)
472 * The destination into which the value will be written.
474 * The expected value.
478 * Non-zero on success; 0 on failure.
481 rte_atomic64_cmpset(volatile uint64_t *dst, uint64_t exp, uint64_t src);
484 * The atomic counter structure.
487 volatile int64_t cnt; /**< Internal counter value. */
491 * Static initializer for an atomic counter.
493 #define RTE_ATOMIC64_INIT(val) { (val) }
496 * Initialize the atomic counter.
499 * A pointer to the atomic counter.
502 rte_atomic64_init(rte_atomic64_t *v);
505 * Atomically read a 64-bit counter.
508 * A pointer to the atomic counter.
510 * The value of the counter.
512 static inline int64_t
513 rte_atomic64_read(rte_atomic64_t *v);
516 * Atomically set a 64-bit counter.
519 * A pointer to the atomic counter.
521 * The new value of the counter.
524 rte_atomic64_set(rte_atomic64_t *v, int64_t new_value);
527 * Atomically add a 64-bit value to a counter.
530 * A pointer to the atomic counter.
532 * The value to be added to the counter.
535 rte_atomic64_add(rte_atomic64_t *v, int64_t inc);
538 * Atomically subtract a 64-bit value from a counter.
541 * A pointer to the atomic counter.
543 * The value to be subtracted from the counter.
546 rte_atomic64_sub(rte_atomic64_t *v, int64_t dec);
549 * Atomically increment a 64-bit counter by one and test.
552 * A pointer to the atomic counter.
555 rte_atomic64_inc(rte_atomic64_t *v);
558 * Atomically decrement a 64-bit counter by one and test.
561 * A pointer to the atomic counter.
564 rte_atomic64_dec(rte_atomic64_t *v);
567 * Add a 64-bit value to an atomic counter and return the result.
569 * Atomically adds the 64-bit value (inc) to the atomic counter (v) and
570 * returns the value of v after the addition.
573 * A pointer to the atomic counter.
575 * The value to be added to the counter.
577 * The value of v after the addition.
579 static inline int64_t
580 rte_atomic64_add_return(rte_atomic64_t *v, int64_t inc);
583 * Subtract a 64-bit value from an atomic counter and return the result.
585 * Atomically subtracts the 64-bit value (dec) from the atomic counter (v)
586 * and returns the value of v after the subtraction.
589 * A pointer to the atomic counter.
591 * The value to be subtracted from the counter.
593 * The value of v after the subtraction.
595 static inline int64_t
596 rte_atomic64_sub_return(rte_atomic64_t *v, int64_t dec);
599 * Atomically increment a 64-bit counter by one and test.
601 * Atomically increments the atomic counter (v) by one and returns
602 * true if the result is 0, or false in all other cases.
605 * A pointer to the atomic counter.
607 * True if the result after the addition is 0; false otherwise.
610 rte_atomic64_inc_and_test(rte_atomic64_t *v);
613 * Atomically decrement a 64-bit counter by one and test.
615 * Atomically decrements the atomic counter (v) by one and returns true if
616 * the result is 0, or false in all other cases.
619 * A pointer to the atomic counter.
621 * True if the result after subtraction is 0; false otherwise.
624 rte_atomic64_dec_and_test(rte_atomic64_t *v);
627 * Atomically test and set a 64-bit atomic counter.
629 * If the counter value is already set, return 0 (failed). Otherwise, set
630 * the counter value to 1 and return 1 (success).
633 * A pointer to the atomic counter.
635 * 0 if failed; else 1, success.
638 rte_atomic64_test_and_set(rte_atomic64_t *v);
641 * Atomically set a 64-bit counter to 0.
644 * A pointer to the atomic counter.
647 rte_atomic64_clear(rte_atomic64_t *v);
649 #endif /* __DOXYGEN__ */
656 #endif /* _RTE_ATOMIC_H_ */