/*
 * Port of uClibc for TMS320C6000 DSP architecture 
 * Copyright (C) 2004 Texas Instruments Incorporated
 * Author of TMS320C6000 port: Aurelien Jacquiot 
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library 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 Library General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#ifndef _ATOMICITY_H
#define _ATOMICITY_H	1

typedef int _Atomic_word;

extern cregister volatile unsigned int CSR;
#define global_sti()             CSR |= 1
#define global_cli()             CSR &= -2

static inline _Atomic_word
__attribute__ ((unused))
__exchange_and_add (volatile _Atomic_word *mem, int val)
{
	_Atomic_word result;
	
	global_cli();
	result = *mem;
	*mem += val;
	global_sti();
	
	return result;
}

static inline void
__attribute__ ((unused))
__atomic_add (volatile _Atomic_word *mem, int val)
{
	global_cli();
	*mem += val;
	global_sti();
}

static inline int
__attribute__ ((unused))
__compare_and_swap (volatile long int *p, long int oldval, long int newval)
{
	global_cli();
	if (*p != oldval) {
		global_sti();
		return 0;
	}
	
	*p = newval;
	global_sti();

	return 1;
}

#endif /* atomicity.h */
