/*
 *	atomics.h -- atomic/synchronising operations
 *	Copyright (C) 2000-2004 Fred Barnes
 *	Some code based on include/asm-i386/spinlock.h in the Linux kernel
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program 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 General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __ATOMICS_H
#define __ATOMICS_H

/*
 *	Stuff for spinlocks
 */

#ifdef USE_PTHREADS
/*
 *	if we're using POSIX threads, use a pthread_mutex_t instead
 *	of the spinlock.
 */
typedef pthread_mutex_t spl_t;

#define spl_init(X)		pthread_mutex_init((X), NULL)
#define spl_lock_or_fail(X)	pthread_mutex_trylock((X))
#define spl_lock(X)		pthread_mutex_lock((X))
#define spl_unlock(X)		pthread_mutex_unlock((X))


#else	/* !USE_PTHREADS */

typedef struct {
	volatile unsigned int lock;
} spl_t;

#define SPL_UNLOCKED	(spl_t) { 0 }

typedef struct { unsigned long a[100]; } __dummy_lock_t;
#define __dummy_lock(lock) (*(__dummy_lock_t *)(lock))

#define spl_init(X)	do { *(X) = SPL_UNLOCKED; } while (0)

static inline int spl_lock_or_fail (spl_t *lock)
{
	register int result;

	__asm__ __volatile__(
		"				\n"
		"	lock; btsl $0,%0	\n"
		"	jc 1f			\n"
		"	movl $0,%1		\n"
		"	jmp 2f			\n"
		"1:				\n"
		"	movl $1,%1		\n"
		"2:				\n"
	:"=m" (__dummy_lock(lock)), "=g" (result));
	return result;
}

static inline void spl_lock (spl_t *lock)
{
	__asm__ __volatile__(
		"				\n"
		"1:				\n"
		"	lock; btsl $0,%0	\n"
		"	jc 2f			\n"
		".section .text.lock,\"ax\"	\n"
		"2:				\n"
		"	testb $1,%0		\n"
		"	jne 2b			\n"
		"	jmp 1b			\n"
		".previous			\n"
	:"=m" (__dummy_lock(lock)));
}

static inline void spl_unlock (spl_t *lock)
{
	__asm__ __volatile__(
		"	lock; btrl $0,%0	\n"
	: "=m" (__dummy_lock(lock)));
}

#endif	/* !USE_PTHREADS */



/*
 *	Stuff for atomic INC/DEC
 */

#if !defined(OOS_BUILD)

typedef struct {
	volatile unsigned int value;
} atomic_t;

#define ATT_INITIAL	(atomic_t) { 0 }

typedef struct { unsigned long a[100]; } __dummy_atomic_t;
#define __dummy_atomic(val) (*(__dummy_atomic_t *)(val))


#define att_init(X)	do { *(X) = ATT_INITIAL; } while (0)

extern inline int att_val (atomic_t *atval)
{
	int result;

	__asm__ __volatile__ (
		"\n\t" \
		"movl %0,%1\n\t"
	: "=m" (__dummy_atomic(atval)), "=g" (result));
	return result;
}

extern inline void att_inc (atomic_t *atval)
{
	__asm__ __volatile__(
		"\n\t" \
		"lock; incl %0\n\t"
	: "=m" (__dummy_atomic(atval)) );
}

extern inline void att_dec (atomic_t *atval)
{
	__asm__ __volatile__(
		"\n\t" \
		"lock; decl %0\n\t"
	: "=m" (__dummy_atomic(atval)) );
}

#endif

/*
 *	Other, generally useful, stuff
 */

#define barrier() __asm__ __volatile__ ("": : :"memory")

#define XCHG(var,ptr) \
	__asm__ __volatile__ ("\n\t" \
		"xchgl	%0,(%1)\n\t" \
		: "=r" (var), "=r" (ptr) : "0" (var), "1" (ptr) : "cc")


#endif	/* !__ATOMICS_H */

