License text standardization, add missing licenses
[lttng-ust.git] / libringbuffer / vatomic.h
CommitLineData
e92f3e28
MD
1#ifndef _LTTNG_RING_BUFFER_VATOMIC_H
2#define _LTTNG_RING_BUFFER_VATOMIC_H
852c2936
MD
3
4/*
e92f3e28 5 * libringbuffer/vatomic.h
852c2936 6 *
e92f3e28 7 * Copyright (C) 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
852c2936 8 *
e92f3e28
MD
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
a60d70e6 15 *
e92f3e28
MD
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
852c2936
MD
18 */
19
14641deb
MD
20#include <assert.h>
21#include <urcu/uatomic.h>
22
852c2936
MD
23/*
24 * Same data type (long) accessed differently depending on configuration.
25 * v field is for non-atomic access (protected by mutual exclusion).
26 * In the fast-path, the ring_buffer_config structure is constant, so the
27 * compiler can statically select the appropriate branch.
28 * local_t is used for per-cpu and per-thread buffers.
29 * atomic_long_t is used for globally shared buffers.
30 */
31union v_atomic {
14641deb 32 long a; /* accessed through uatomic */
852c2936
MD
33 long v;
34};
35
36static inline
4cfec15c 37long v_read(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a)
852c2936 38{
14641deb
MD
39 assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
40 return uatomic_read(&v_a->a);
852c2936
MD
41}
42
43static inline
4cfec15c 44void v_set(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a,
852c2936
MD
45 long v)
46{
14641deb
MD
47 assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
48 uatomic_set(&v_a->a, v);
852c2936
MD
49}
50
51static inline
4cfec15c 52void v_add(const struct lttng_ust_lib_ring_buffer_config *config, long v, union v_atomic *v_a)
852c2936 53{
14641deb
MD
54 assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
55 uatomic_add(&v_a->a, v);
852c2936
MD
56}
57
58static inline
4cfec15c 59void v_inc(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a)
852c2936 60{
14641deb
MD
61 assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
62 uatomic_inc(&v_a->a);
852c2936
MD
63}
64
65/*
66 * Non-atomic decrement. Only used by reader, apply to reader-owned subbuffer.
67 */
68static inline
4cfec15c 69void _v_dec(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a)
852c2936
MD
70{
71 --v_a->v;
72}
73
74static inline
4cfec15c 75long v_cmpxchg(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a,
852c2936
MD
76 long old, long _new)
77{
14641deb
MD
78 assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
79 return uatomic_cmpxchg(&v_a->a, old, _new);
852c2936
MD
80}
81
e92f3e28 82#endif /* _LTTNG_RING_BUFFER_VATOMIC_H */
This page took 0.027305 seconds and 4 git commands to generate.