Cleanup: Move lib/ringbuffer/ headers to include/ringbuffer/
[lttng-modules.git] / include / ringbuffer / vatomic.h
1 /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only)
2 *
3 * ringbuffer/vatomic.h
4 *
5 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8 #ifndef _LIB_RING_BUFFER_VATOMIC_H
9 #define _LIB_RING_BUFFER_VATOMIC_H
10
11 #include <asm/atomic.h>
12 #include <asm/local.h>
13
14 /*
15 * Same data type (long) accessed differently depending on configuration.
16 * v field is for non-atomic access (protected by mutual exclusion).
17 * In the fast-path, the ring_buffer_config structure is constant, so the
18 * compiler can statically select the appropriate branch.
19 * local_t is used for per-cpu and per-thread buffers.
20 * atomic_long_t is used for globally shared buffers.
21 */
22 union v_atomic {
23 local_t l;
24 atomic_long_t a;
25 long v;
26 };
27
28 static inline
29 long v_read(const struct lib_ring_buffer_config *config, union v_atomic *v_a)
30 {
31 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
32 return local_read(&v_a->l);
33 else
34 return atomic_long_read(&v_a->a);
35 }
36
37 static inline
38 void v_set(const struct lib_ring_buffer_config *config, union v_atomic *v_a,
39 long v)
40 {
41 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
42 local_set(&v_a->l, v);
43 else
44 atomic_long_set(&v_a->a, v);
45 }
46
47 static inline
48 void v_add(const struct lib_ring_buffer_config *config, long v, union v_atomic *v_a)
49 {
50 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
51 local_add(v, &v_a->l);
52 else
53 atomic_long_add(v, &v_a->a);
54 }
55
56 static inline
57 void v_inc(const struct lib_ring_buffer_config *config, union v_atomic *v_a)
58 {
59 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
60 local_inc(&v_a->l);
61 else
62 atomic_long_inc(&v_a->a);
63 }
64
65 /*
66 * Non-atomic decrement. Only used by reader, apply to reader-owned subbuffer.
67 */
68 static inline
69 void _v_dec(const struct lib_ring_buffer_config *config, union v_atomic *v_a)
70 {
71 --v_a->v;
72 }
73
74 static inline
75 long v_cmpxchg(const struct lib_ring_buffer_config *config, union v_atomic *v_a,
76 long old, long _new)
77 {
78 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
79 return local_cmpxchg(&v_a->l, old, _new);
80 else
81 return atomic_long_cmpxchg(&v_a->a, old, _new);
82 }
83
84 #endif /* _LIB_RING_BUFFER_VATOMIC_H */
This page took 0.030911 seconds and 4 git commands to generate.