/*
 * Copyright (c) Likewise Software.  All rights Reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the license, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.  You should have received a copy
 * of the GNU Lesser General Public License along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * LESSER GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

/*
 * Module Name:
 *
 *        atomic.h
 *
 * Abstract:
 *
 *        Atomic operations
 *
 * Authors: Brian Koropoff (bkoropoff@likewisesoftware.com)
 *
 */

#ifndef __LWBASE_ATOMIC_H__
#define __LWBASE_ATOMIC_H__

#include <lw/types.h>
#include <lw/attrs.h>

#if defined(LW_ISA_X86)
#include <lw/atomic_x86.h>
#elif defined(LW_ISA_X86_64)
#include <lw/atomic_x86_64.h>
#else
#include <lw/atomic_lock.h>
#endif

static inline
LW_LONG
LwInterlockedRead(
    LW_IN LW_LONG volatile *plSource
    )
{
	return lw_atomic_load_acq_32(plSource);
}

static inline
LW_LONG
LwInterlockedIncrement(
    LW_IN LW_OUT LW_LONG volatile* plDestination
    )
{
	/*
	 * lw_atomic_fetchadd_32() returns the old value.
	 * LwInterlockedIncrement() returns the new value.
	 */
	return (lw_atomic_fetchadd_32(plDestination, 1) + 1);
}

static inline
LW_LONG
LwInterlockedDecrement(
    LW_IN LW_OUT LW_LONG volatile* plDestination
    )
{
	/*
	 * lw_atomic_fetchadd_32() returns the old value.
	 * LwInterlockedDecrement() returns the new value.
	 */
	return (lw_atomic_fetchadd_32(plDestination, -1) - 1);
}

/* Operations on pointers. */
#define	lw_atomic_set_ptr(p, v) \
	lw_atomic_set_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_set_acq_ptr(p, v) \
	lw_atomic_set_acq_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_set_rel_ptr(p, v) \
	lw_atomic_set_rel_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_clear_ptr(p, v) \
	lw_atomic_clear_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_clear_acq_ptr(p, v) \
	lw_atomic_clear_acq_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_clear_rel_ptr(p, v) \
	lw_atomic_clear_rel_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_add_ptr(p, v) \
	lw_atomic_add_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_add_acq_ptr(p, v) \
	lw_atomic_add_acq_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_add_rel_ptr(p, v) \
	lw_atomic_add_rel_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_subtract_ptr(p, v) \
	lw_atomic_subtract_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_subtract_acq_ptr(p, v) \
	lw_atomic_subtract_acq_uintptr_t((volatile uintptr_t *)(p), \
	    (uintptr_t)(v))
#define	lw_atomic_subtract_rel_ptr(p, v) \
	lw_atomic_subtract_rel_uintptr_t((volatile uintptr_t *)(p), \
	    (uintptr_t)(v))
#define	lw_atomic_load_acq_ptr(p) \
	lw_atomic_load_acq_uintptr_t((volatile uintptr_t *)(p))
#define	lw_atomic_store_rel_ptr(p, v) \
	lw_atomic_store_rel_uintptr_t((volatile uintptr_t *)(p), (v))
#define	lw_atomic_cmpset_ptr(dst, old, new) \
	lw_atomic_cmpset_uintptr_t((volatile uintptr_t *)(dst), \
	    (uintptr_t)(old), (uintptr_t)(new))
#define	lw_atomic_cmpset_acq_ptr(dst, old, new) \
	lw_atomic_cmpset_acq_uintptr_t((volatile uintptr_t *)(dst), \
	    (uintptr_t)(old), (uintptr_t)(new))
#define	lw_atomic_cmpset_rel_ptr(dst, old, new) \
	lw_atomic_cmpset_rel_uintptr_t((volatile uintptr_t *)(dst), \
	    (uintptr_t)(old), (uintptr_t)(new))
#define	lw_atomic_swap_ptr(p, v) \
	lw_atomic_swap_uintptr_t((volatile uintptr_t *)(p), (uintptr_t)(v))
#define	lw_atomic_readandclear_ptr(p) \
	lw_atomic_readandclear_uintptr_t((volatile uintptr_t *)(p))

#ifndef LW_STRICT_NAMESPACE

#define InterlockedCompareExchange(Desination, NewValue, CompareValue) \
    LwInterlockedCompareExchange(Desination, NewValue, CompareValue)
#define InterlockedCompareExchangePointer(Destination, Exchange, Comparand) \
    LwInterlockedCompareExchangePointer(Destination, Exchange, Comparand)
#define InterlockedRead(Source)             LwInterlockedRead(Source)
#define InterlockedIncrement(Destination)   LwInterlockedIncrement(Destination)
#define InterlockedDecrement(Destination)   LwInterlockedDecrement(Destination)

#endif /* LW_STRICT_NAMESPACE */

LW_END_EXTERN_C

#endif
