Blame src/utils/atomic.h

Packit Service aa3af4
/*
Packit Service aa3af4
 * Copyright (c) 2001-2020 Mellanox Technologies, Ltd. All rights reserved.
Packit Service aa3af4
 *
Packit Service aa3af4
 * This software is available to you under a choice of one of two
Packit Service aa3af4
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit Service aa3af4
 * General Public License (GPL) Version 2, available from the file
Packit Service aa3af4
 * COPYING in the main directory of this source tree, or the
Packit Service aa3af4
 * BSD license below:
Packit Service aa3af4
 *
Packit Service aa3af4
 *     Redistribution and use in source and binary forms, with or
Packit Service aa3af4
 *     without modification, are permitted provided that the following
Packit Service aa3af4
 *     conditions are met:
Packit Service aa3af4
 *
Packit Service aa3af4
 *      - Redistributions of source code must retain the above
Packit Service aa3af4
 *        copyright notice, this list of conditions and the following
Packit Service aa3af4
 *        disclaimer.
Packit Service aa3af4
 *
Packit Service aa3af4
 *      - Redistributions in binary form must reproduce the above
Packit Service aa3af4
 *        copyright notice, this list of conditions and the following
Packit Service aa3af4
 *        disclaimer in the documentation and/or other materials
Packit Service aa3af4
 *        provided with the distribution.
Packit Service aa3af4
 *
Packit Service aa3af4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit Service aa3af4
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit Service aa3af4
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit Service aa3af4
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit Service aa3af4
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit Service aa3af4
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service aa3af4
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit Service aa3af4
 * SOFTWARE.
Packit Service aa3af4
 */
Packit Service aa3af4
Packit Service aa3af4
Packit Service aa3af4
#ifndef ATOMIC_H_
Packit Service aa3af4
#define ATOMIC_H_
Packit Service aa3af4
Packit Service aa3af4
#include "asm.h"
Packit Service aa3af4
#include "utils/bullseye.h"
Packit Service aa3af4
Packit Service aa3af4
struct atomic_t {
Packit Service aa3af4
	__volatile__ int counter;
Packit Service aa3af4
};
Packit Service aa3af4
Packit Service aa3af4
#define ATOMIC_INIT(i)  { (i) }
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 * Read atomic variable.
Packit Service aa3af4
 * @param v pointer of type atomic_t
Packit Service aa3af4
 * @return Value of the atomic.
Packit Service aa3af4
 *
Packit Service aa3af4
 * Atomically reads the value of @v.
Packit Service aa3af4
 */
Packit Service aa3af4
#define atomic_read(v) ((v)->counter)
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 * Set atomic variable.
Packit Service aa3af4
 * @param v pointer of type atomic_t.
Packit Service aa3af4
 * @param i required value.
Packit Service aa3af4
 */
Packit Service aa3af4
#define atomic_set(v,i) (((v)->counter) = (i))
Packit Service aa3af4
Packit Service aa3af4
#if 0
Packit Service aa3af4
Packit Service aa3af4
#if _BullseyeCoverage
Packit Service aa3af4
    #pragma BullseyeCoverage off
Packit Service aa3af4
#endif
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 *  Returns current contents of addr and replaces contents with value.
Packit Service aa3af4
 *  @param value Values to set.
Packit Service aa3af4
 *  @param addr Address to set.
Packit Service aa3af4
 *  @return Previous value of *addr.
Packit Service aa3af4
 */
Packit Service aa3af4
template<typename T>
Packit Service aa3af4
static inline T atomic_swap(T new_value, T *addr)
Packit Service aa3af4
{
Packit Service aa3af4
	return (T)xchg((unsigned long)new_value, (void*)addr);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 *  Replaces *addr with new_value if it equals old_value.
Packit Service aa3af4
 *  @param old_value Expected value.
Packit Service aa3af4
 *  @param new_value Value to set.
Packit Service aa3af4
 *  @param addr Address to set.
Packit Service aa3af4
 *  @return true if was set, false if not.
Packit Service aa3af4
 */
Packit Service aa3af4
template<typename T>
Packit Service aa3af4
static bool atomic_cas(T old_value, T new_value, T *addr)
Packit Service aa3af4
{
Packit Service aa3af4
	return cmpxchg((unsigned long)old_value, (unsigned long)new_value, (void*)addr);
Packit Service aa3af4
}
Packit Service aa3af4
#if _BullseyeCoverage
Packit Service aa3af4
    #pragma BullseyeCoverage on
Packit Service aa3af4
#endif
Packit Service aa3af4
Packit Service aa3af4
#endif
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 * Add to the atomic variable.
Packit Service aa3af4
 * @param i integer value to add.
Packit Service aa3af4
 * @param v pointer of type atomic_t.
Packit Service aa3af4
 * @return Value before add.
Packit Service aa3af4
 */
Packit Service aa3af4
static inline int atomic_fetch_and_inc(atomic_t *v)
Packit Service aa3af4
{
Packit Service aa3af4
	return atomic_fetch_and_add(1, &v->counter);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
/**
Packit Service aa3af4
 * Add to the atomic variable.
Packit Service aa3af4
 * @param i integer value to add.
Packit Service aa3af4
 * @param v pointer of type atomic_t.
Packit Service aa3af4
 * @return Value before add.
Packit Service aa3af4
 */
Packit Service aa3af4
static inline int atomic_fetch_and_dec(atomic_t *v)
Packit Service aa3af4
{
Packit Service aa3af4
	return atomic_fetch_and_add(-1, &v->counter);
Packit Service aa3af4
}
Packit Service aa3af4
Packit Service aa3af4
#endif /* ATOMIC_H_ */