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 | ||
852c2936 MD |
12 | /* |
13 | * Same data type (long) accessed differently depending on configuration. | |
14 | * v field is for non-atomic access (protected by mutual exclusion). | |
15 | * In the fast-path, the ring_buffer_config structure is constant, so the | |
16 | * compiler can statically select the appropriate branch. | |
17 | * local_t is used for per-cpu and per-thread buffers. | |
18 | * atomic_long_t is used for globally shared buffers. | |
19 | */ | |
20 | union v_atomic { | |
21 | local_t l; | |
22 | atomic_long_t a; | |
23 | long v; | |
24 | }; | |
25 | ||
26 | static inline | |
27 | long v_read(const struct lib_ring_buffer_config *config, union v_atomic *v_a) | |
28 | { | |
29 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) | |
30 | return local_read(&v_a->l); | |
31 | else | |
32 | return atomic_long_read(&v_a->a); | |
33 | } | |
34 | ||
35 | static inline | |
36 | void v_set(const struct lib_ring_buffer_config *config, union v_atomic *v_a, | |
37 | long v) | |
38 | { | |
39 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) | |
40 | local_set(&v_a->l, v); | |
41 | else | |
42 | atomic_long_set(&v_a->a, v); | |
43 | } | |
44 | ||
45 | static inline | |
46 | void v_add(const struct lib_ring_buffer_config *config, long v, union v_atomic *v_a) | |
47 | { | |
48 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) | |
49 | local_add(v, &v_a->l); | |
50 | else | |
51 | atomic_long_add(v, &v_a->a); | |
52 | } | |
53 | ||
54 | static inline | |
55 | void v_inc(const struct lib_ring_buffer_config *config, union v_atomic *v_a) | |
56 | { | |
57 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) | |
58 | local_inc(&v_a->l); | |
59 | else | |
60 | atomic_long_inc(&v_a->a); | |
61 | } | |
62 | ||
63 | /* | |
64 | * Non-atomic decrement. Only used by reader, apply to reader-owned subbuffer. | |
65 | */ | |
66 | static inline | |
67 | void _v_dec(const struct lib_ring_buffer_config *config, union v_atomic *v_a) | |
68 | { | |
69 | --v_a->v; | |
70 | } | |
71 | ||
72 | static inline | |
73 | long v_cmpxchg(const struct lib_ring_buffer_config *config, union v_atomic *v_a, | |
74 | long old, long _new) | |
75 | { | |
76 | if (config->sync == RING_BUFFER_SYNC_PER_CPU) | |
77 | return local_cmpxchg(&v_a->l, old, _new); | |
78 | else | |
79 | return atomic_long_cmpxchg(&v_a->a, old, _new); | |
80 | } | |
81 | ||
82 | #endif /* _LINUX_RING_BUFFER_VATOMIC_H */ |