base/include/asm/rtai_atomic.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003 Philippe Gerum <rpm@xenomai.org>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #ifndef _RTAI_ASM_I386_ATOMIC_H
00020 #define _RTAI_ASM_I386_ATOMIC_H
00021 
00022 #ifdef __KERNEL__
00023 
00024 #include <linux/bitops.h>
00025 #include <asm/atomic.h>
00026 #include <asm/system.h>
00027 
00028 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
00029 
00030 #define atomic_cmpxchg(v, old, new)  ((int)cmpxchg(&((v)->counter), old, new))
00031 #define atomic_xchg(v, new)          (xchg(&((v)->counter), new))
00032 
00033 #endif
00034 
00035 #else /* !__KERNEL__ */
00036 
00037 #ifndef likely
00038 #if __GNUC__ == 2 && __GNUC_MINOR__ < 96
00039 #define __builtin_expect(x, expected_value) (x)
00040 #endif
00041 #define likely(x)   __builtin_expect(!!(x), 1)
00042 #define unlikely(x) __builtin_expect(!!(x), 0)
00043 #endif /* !likely */
00044 
00045 #ifdef CONFIG_SMP
00046 #define LOCK_PREFIX "lock ; "
00047 #else
00048 #define LOCK_PREFIX ""
00049 #endif
00050 
00051 #define atomic_t int
00052 
00053 struct __rtai_xchg_dummy { unsigned long a[100]; };
00054 #define __rtai_xg(x) ((struct __rtai_xchg_dummy *)(x))
00055 
00056 static inline unsigned long atomic_xchg(volatile void *ptr, unsigned long x)
00057 {
00058     __asm__ __volatile__(LOCK_PREFIX "xchgl %0,%1"
00059                      :"=r" (x)
00060                  :"m" (*__rtai_xg(ptr)), "0" (x)
00061                  :"memory");
00062     return x;
00063 }
00064 
00065 static inline unsigned long atomic_cmpxchg(volatile void *ptr, unsigned long o, unsigned long n)
00066 {
00067     unsigned long prev;
00068     __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
00069                  : "=a"(prev)
00070                  : "q"(n), "m" (*__rtai_xg(ptr)), "0" (o)
00071                  : "memory");
00072     return prev;
00073 }
00074 
00075 static __inline__ int atomic_dec_and_test(atomic_t *v)
00076 {
00077         unsigned char c;
00078 
00079         __asm__ __volatile__(
00080                 LOCK_PREFIX "decl %0; sete %1"
00081                 :"=m" (*__rtai_xg(v)), "=qm" (c)
00082                 :"m" (*__rtai_xg(v)) : "memory");
00083         return c != 0;
00084 }
00085 
00086 static __inline__ void atomic_inc(atomic_t *v)
00087 {
00088         __asm__ __volatile__(
00089                 LOCK_PREFIX "incl %0"
00090                 :"=m" (*__rtai_xg(v))
00091                 :"m" (*__rtai_xg(v)));
00092 }
00093 
00094 /* Depollute the namespace a bit. */
00095 #undef ADDR
00096 
00097 #endif /* __KERNEL__ */
00098 
00099 #endif /* !_RTAI_ASM_I386_ATOMIC_H */

Generated on Tue Feb 2 17:46:04 2010 for RTAI API by  doxygen 1.4.7