| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _LTTNG_RING_BUFFER_VATOMIC_H |
| 8 | #define _LTTNG_RING_BUFFER_VATOMIC_H |
| 9 | |
| 10 | #include <assert.h> |
| 11 | #include <urcu/uatomic.h> |
| 12 | |
| 13 | /* |
| 14 | * Same data type (long) accessed differently depending on configuration. |
| 15 | * v field is for non-atomic access (protected by mutual exclusion). |
| 16 | * In the fast-path, the ring_buffer_config structure is constant, so the |
| 17 | * compiler can statically select the appropriate branch. |
| 18 | * local_t is used for per-cpu and per-thread buffers. |
| 19 | * atomic_long_t is used for globally shared buffers. |
| 20 | */ |
| 21 | union v_atomic { |
| 22 | long a; /* accessed through uatomic */ |
| 23 | long v; |
| 24 | }; |
| 25 | |
| 26 | static inline |
| 27 | long v_read(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a) |
| 28 | { |
| 29 | assert(config->sync != RING_BUFFER_SYNC_PER_CPU); |
| 30 | return uatomic_read(&v_a->a); |
| 31 | } |
| 32 | |
| 33 | static inline |
| 34 | void v_set(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a, |
| 35 | long v) |
| 36 | { |
| 37 | assert(config->sync != RING_BUFFER_SYNC_PER_CPU); |
| 38 | uatomic_set(&v_a->a, v); |
| 39 | } |
| 40 | |
| 41 | static inline |
| 42 | void v_add(const struct lttng_ust_lib_ring_buffer_config *config, long v, union v_atomic *v_a) |
| 43 | { |
| 44 | assert(config->sync != RING_BUFFER_SYNC_PER_CPU); |
| 45 | uatomic_add(&v_a->a, v); |
| 46 | } |
| 47 | |
| 48 | static inline |
| 49 | void v_inc(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a) |
| 50 | { |
| 51 | assert(config->sync != RING_BUFFER_SYNC_PER_CPU); |
| 52 | uatomic_inc(&v_a->a); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Non-atomic decrement. Only used by reader, apply to reader-owned subbuffer. |
| 57 | */ |
| 58 | static inline |
| 59 | void _v_dec(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a) |
| 60 | { |
| 61 | --v_a->v; |
| 62 | } |
| 63 | |
| 64 | static inline |
| 65 | long v_cmpxchg(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a, |
| 66 | long old, long _new) |
| 67 | { |
| 68 | assert(config->sync != RING_BUFFER_SYNC_PER_CPU); |
| 69 | return uatomic_cmpxchg(&v_a->a, old, _new); |
| 70 | } |
| 71 | |
| 72 | #endif /* _LTTNG_RING_BUFFER_VATOMIC_H */ |