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