Import lib ring buffer into LTTng modules
[lttng-modules.git] / lib / ringbuffer / vatomic.h
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
12 #include <asm/atomic.h>
13 #include <asm/local.h>
14
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 {
24 local_t l;
25 atomic_long_t a;
26 long v;
27 };
28
29 static inline
30 long v_read(const struct lib_ring_buffer_config *config, union v_atomic *v_a)
31 {
32 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
33 return local_read(&v_a->l);
34 else
35 return atomic_long_read(&v_a->a);
36 }
37
38 static inline
39 void v_set(const struct lib_ring_buffer_config *config, union v_atomic *v_a,
40 long v)
41 {
42 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
43 local_set(&v_a->l, v);
44 else
45 atomic_long_set(&v_a->a, v);
46 }
47
48 static inline
49 void v_add(const struct lib_ring_buffer_config *config, long v, union v_atomic *v_a)
50 {
51 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
52 local_add(v, &v_a->l);
53 else
54 atomic_long_add(v, &v_a->a);
55 }
56
57 static inline
58 void v_inc(const struct lib_ring_buffer_config *config, union v_atomic *v_a)
59 {
60 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
61 local_inc(&v_a->l);
62 else
63 atomic_long_inc(&v_a->a);
64 }
65
66 /*
67 * Non-atomic decrement. Only used by reader, apply to reader-owned subbuffer.
68 */
69 static inline
70 void _v_dec(const struct lib_ring_buffer_config *config, union v_atomic *v_a)
71 {
72 --v_a->v;
73 }
74
75 static inline
76 long v_cmpxchg(const struct lib_ring_buffer_config *config, union v_atomic *v_a,
77 long old, long _new)
78 {
79 if (config->sync == RING_BUFFER_SYNC_PER_CPU)
80 return local_cmpxchg(&v_a->l, old, _new);
81 else
82 return atomic_long_cmpxchg(&v_a->a, old, _new);
83 }
84
85 #endif /* _LINUX_RING_BUFFER_VATOMIC_H */
This page took 0.032261 seconds and 5 git commands to generate.