| 1 | #ifndef _LIB_RING_BUFFER_VATOMIC_H |
| 2 | #define _LIB_RING_BUFFER_VATOMIC_H |
| 3 | |
| 4 | /* |
| 5 | * lib/ringbuffer/vatomic.h |
| 6 | * |
| 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Lesser General Public |
| 11 | * License as published by the Free Software Foundation; only |
| 12 | * version 2.1 of the License. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | #include <asm/atomic.h> |
| 25 | #include <asm/local.h> |
| 26 | |
| 27 | /* |
| 28 | * Same data type (long) accessed differently depending on configuration. |
| 29 | * v field is for non-atomic access (protected by mutual exclusion). |
| 30 | * In the fast-path, the ring_buffer_config structure is constant, so the |
| 31 | * compiler can statically select the appropriate branch. |
| 32 | * local_t is used for per-cpu and per-thread buffers. |
| 33 | * atomic_long_t is used for globally shared buffers. |
| 34 | */ |
| 35 | union v_atomic { |
| 36 | local_t l; |
| 37 | atomic_long_t a; |
| 38 | long v; |
| 39 | }; |
| 40 | |
| 41 | static inline |
| 42 | long v_read(const struct lib_ring_buffer_config *config, union v_atomic *v_a) |
| 43 | { |
| 44 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) |
| 45 | return local_read(&v_a->l); |
| 46 | else |
| 47 | return atomic_long_read(&v_a->a); |
| 48 | } |
| 49 | |
| 50 | static inline |
| 51 | void v_set(const struct lib_ring_buffer_config *config, union v_atomic *v_a, |
| 52 | long v) |
| 53 | { |
| 54 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) |
| 55 | local_set(&v_a->l, v); |
| 56 | else |
| 57 | atomic_long_set(&v_a->a, v); |
| 58 | } |
| 59 | |
| 60 | static inline |
| 61 | void v_add(const struct lib_ring_buffer_config *config, long v, union v_atomic *v_a) |
| 62 | { |
| 63 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) |
| 64 | local_add(v, &v_a->l); |
| 65 | else |
| 66 | atomic_long_add(v, &v_a->a); |
| 67 | } |
| 68 | |
| 69 | static inline |
| 70 | void v_inc(const struct lib_ring_buffer_config *config, union v_atomic *v_a) |
| 71 | { |
| 72 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) |
| 73 | local_inc(&v_a->l); |
| 74 | else |
| 75 | atomic_long_inc(&v_a->a); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Non-atomic decrement. Only used by reader, apply to reader-owned subbuffer. |
| 80 | */ |
| 81 | static inline |
| 82 | void _v_dec(const struct lib_ring_buffer_config *config, union v_atomic *v_a) |
| 83 | { |
| 84 | --v_a->v; |
| 85 | } |
| 86 | |
| 87 | static inline |
| 88 | long v_cmpxchg(const struct lib_ring_buffer_config *config, union v_atomic *v_a, |
| 89 | long old, long _new) |
| 90 | { |
| 91 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) |
| 92 | return local_cmpxchg(&v_a->l, old, _new); |
| 93 | else |
| 94 | return atomic_long_cmpxchg(&v_a->a, old, _new); |
| 95 | } |
| 96 | |
| 97 | #endif /* _LIB_RING_BUFFER_VATOMIC_H */ |