#ifndef __RTL_LINUX_SYNC_H__
#define __RTL_LINUX_SYNC_H__

#include <linux/rtl_trace.h>
#include <asm/spinlock.h>

/* DO NOT USE THESE OUTSIDE OF THE RTLINUX CODE!!! */
/* (c) Victor Yodaiken 1999. */

#ifndef CONFIG_RTL_TRACER

#define rtl_allow_interrupts() __asm__ __volatile__ ("sti": : :"memory")
#define rtl_stop_interrupts() __asm__ __volatile__ ("cli": : :"memory")
#define rtl_save_interrupts(x) \
__asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */ :"memory")
#define rtl_restore_interrupts(x) \
__asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory")
#define rtl_no_interrupts(x) \
__asm__ __volatile__("pushfl ; cli;  popl %0": "=g" (x): /* no input */ :"memory")
/* I wonder if the code above is slower because the cli gives the pushed
   value a chance to disappear from some forwarding register? VY */


#else

#define rtl_allow_interrupts() do { rtl_trace2a(RTL_TRACE_ALLOW_INTERRUPTS, 0); __asm__ __volatile__ ("sti": : :"memory"); } while (0)
#define rtl_stop_interrupts() do { __asm__ __volatile__ ("cli": : :"memory"); rtl_trace2(RTL_TRACE_STOP_INTERRUPTS, 0); } while (0)
#define rtl_save_interrupts(x) \
do { __asm__ __volatile__("pushfl ; popl %0":"=g" (x): /* no input */ :"memory");  rtl_trace2(RTL_TRACE_SAVE_INTERRUPTS, x); } while (0)
#define rtl_restore_interrupts(x) \
do { __asm__ __volatile__ ("cli": : :"memory"); rtl_trace2a(RTL_TRACE_RESTORE_INTERRUPTS, x); __asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"g" (x):"memory"); } while (0)
#define rtl_no_interrupts(x) \
do { __asm__ __volatile__("pushfl ; cli;  popl %0": "=g" (x): /* no input */ :"memory"); rtl_trace2(RTL_TRACE_NO_INTERRUPTS, x); } while (0)

#endif

#define rtl_hard_savef_and_cli(x) rtl_no_interrupts(x)
#define rtl_hard_restore_flags(x) rtl_restore_interrupts(x)
#define rtl_hard_sti() rtl_allow_interrupts()
#define rtl_hard_cli() rtl_stop_interrupts()
#define rtl_hard_save_flags(x) rtl_save_interrupts(x)

#ifdef __SMP__
/*
#define rtl_spin_lock(x)  { static int z;\
                    	for(z=0;z<100000000;z++)\
	                  if(!test_and_set_bit(0,x.lock))break;\
			  rtl_trace2(RTL_TRACE_SPIN_LOCK, (long ) x); \
			if(! rtl_global.rtl_off){ \
                          if(z==100000000) { \
				conpr("\nRTLBADSpin=");\
			      	conprn((unsigned int)&z);\
				panic("badspin");    }}}

			  
#define rtl_spin_unlock(x)  do { rtl_trace2a(RTL_TRACE_SPIN_UNLOCK, (long) x); clear_bit(0,x.lock); } while (0)
*/

#define rtl_spin_lock(x) do { spin_lock(x); rtl_trace2(RTL_TRACE_SPIN_LOCK, (long)x); } while (0)
#define rtl_spin_unlock(x) do { rtl_trace2a(RTL_TRACE_SPIN_UNLOCK, (long) x); spin_unlock(x); } while (0)

#else

#define rtl_spin_lock(x)
#define rtl_spin_unlock(x)

#endif

#endif
