Blame libringbuffer/vatomic.h

Packit c04fcb
#ifndef _LTTNG_RING_BUFFER_VATOMIC_H
Packit c04fcb
#define _LTTNG_RING_BUFFER_VATOMIC_H
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * libringbuffer/vatomic.h
Packit c04fcb
 *
Packit c04fcb
 * Copyright (C) 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit c04fcb
 * of this software and associated documentation files (the "Software"), to deal
Packit c04fcb
 * in the Software without restriction, including without limitation the rights
Packit c04fcb
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Packit c04fcb
 * copies of the Software, and to permit persons to whom the Software is
Packit c04fcb
 * furnished to do so, subject to the following conditions:
Packit c04fcb
 *
Packit c04fcb
 * The above copyright notice and this permission notice shall be included in
Packit c04fcb
 * all copies or substantial portions of the Software.
Packit c04fcb
 *
Packit c04fcb
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit c04fcb
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit c04fcb
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit c04fcb
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit c04fcb
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit c04fcb
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit c04fcb
 * SOFTWARE.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#include <assert.h>
Packit c04fcb
#include <urcu/uatomic.h>
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Same data type (long) accessed differently depending on configuration.
Packit c04fcb
 * v field is for non-atomic access (protected by mutual exclusion).
Packit c04fcb
 * In the fast-path, the ring_buffer_config structure is constant, so the
Packit c04fcb
 * compiler can statically select the appropriate branch.
Packit c04fcb
 * local_t is used for per-cpu and per-thread buffers.
Packit c04fcb
 * atomic_long_t is used for globally shared buffers.
Packit c04fcb
 */
Packit c04fcb
union v_atomic {
Packit c04fcb
	long a;	/* accessed through uatomic */
Packit c04fcb
	long v;
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
long v_read(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a)
Packit c04fcb
{
Packit c04fcb
	assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
Packit c04fcb
	return uatomic_read(&v_a->a);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
void v_set(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a,
Packit c04fcb
	   long v)
Packit c04fcb
{
Packit c04fcb
	assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
Packit c04fcb
	uatomic_set(&v_a->a, v);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
void v_add(const struct lttng_ust_lib_ring_buffer_config *config, long v, union v_atomic *v_a)
Packit c04fcb
{
Packit c04fcb
	assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
Packit c04fcb
	uatomic_add(&v_a->a, v);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
void v_inc(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a)
Packit c04fcb
{
Packit c04fcb
	assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
Packit c04fcb
	uatomic_inc(&v_a->a);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * Non-atomic decrement. Only used by reader, apply to reader-owned subbuffer.
Packit c04fcb
 */
Packit c04fcb
static inline
Packit c04fcb
void _v_dec(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a)
Packit c04fcb
{
Packit c04fcb
	--v_a->v;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
static inline
Packit c04fcb
long v_cmpxchg(const struct lttng_ust_lib_ring_buffer_config *config, union v_atomic *v_a,
Packit c04fcb
	       long old, long _new)
Packit c04fcb
{
Packit c04fcb
	assert(config->sync != RING_BUFFER_SYNC_PER_CPU);
Packit c04fcb
	return uatomic_cmpxchg(&v_a->a, old, _new);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
#endif /* _LTTNG_RING_BUFFER_VATOMIC_H */