oro_atomic.h

00001 
00002 #ifndef __ARCH_NOASM_ORO_ATOMIC__
00003 #define __ARCH_NOASM_ORO_ATOMIC__
00004 
00005 /*
00006  * @file Atomic operations that C can't guarantee us.  Useful for
00007  * resource counting etc. We emulate it using mutexes.
00008  */
00009 
00010 #include "../../rtt-config.h"
00011 #include "../fosi.h"
00012 
00013 #ifndef __GNUC__
00014 #define __inline__
00015 #endif
00016 
00017 /*
00018  * Make sure gcc doesn't try to be clever and move things around
00019  * on us. We need to use _exactly_ the address the user gave us,
00020  * not some alias that contains the same information.
00021  */
00022 typedef struct { rt_mutex_t m; volatile int counter; } oro_atomic_t;
00023 
00024 #define ORO_ATOMIC_INIT(i)  { (i) }
00025 #define ORO_ATOMIC_SETUP(v,i)   rtos_mutex_init(&((v)->m)); (v)->counter = (i);
00026 #define ORO_ATOMIC_CLEANUP(v)   rtos_mutex_destroy(&((v)->m));
00027 
00035 #define oro_atomic_read(v)      ((v)->counter)
00036 
00045 #define oro_atomic_set(v,i)     (((v)->counter) = (i))
00046 
00055 static __inline__ void oro_atomic_add(int i, oro_atomic_t *v)
00056 {
00057   rtos_mutex_lock(&((v)->m)); (v)->counter += i; rtos_mutex_unlock(&((v)->m));
00058 }
00059 
00068 static __inline__ void oro_atomic_sub(int i, oro_atomic_t *v)
00069 {
00070   rtos_mutex_lock(&((v)->m)); (v)->counter -= i; rtos_mutex_unlock(&((v)->m));
00071 }
00072 
00083 static __inline__ int oro_atomic_sub_and_test(int i, oro_atomic_t *v)
00084 {
00085   int ret = 0;
00086   rtos_mutex_lock(&((v)->m)); (v)->counter -= i;  ret = ((v)->counter == 0); rtos_mutex_unlock(&((v)->m));
00087   return ret;
00088 }
00089 
00097 static __inline__ void oro_atomic_inc(oro_atomic_t *v)
00098 {
00099   rtos_mutex_lock(&((v)->m)); ++((v)->counter) ; rtos_mutex_unlock(&((v)->m));
00100 }
00101 
00109 static __inline__ void oro_atomic_dec(oro_atomic_t *v)
00110 {
00111   rtos_mutex_lock(&((v)->m)); --((v)->counter) ; rtos_mutex_unlock(&((v)->m));
00112 }
00113 
00123 static __inline__ int oro_atomic_dec_and_test(oro_atomic_t *v)
00124 {
00125   int ret = 0;
00126   rtos_mutex_lock(&((v)->m)); --((v)->counter);  ret = ((v)->counter == 0); rtos_mutex_unlock(&((v)->m));
00127   return ret;
00128 }
00129 
00139 static __inline__ int oro_atomic_inc_and_test(oro_atomic_t *v)
00140 {
00141   int ret = 0;
00142   rtos_mutex_lock(&((v)->m)); ++((v)->counter);  ret = ((v)->counter == 0); rtos_mutex_unlock(&((v)->m));
00143   return ret;
00144 }
00145 
00156 static __inline__ int oro_atomic_add_negative(int i, oro_atomic_t *v)
00157 {
00158   int ret = 0;
00159   rtos_mutex_lock(&((v)->m)); (v)->counter += i;  ret = ((v)->counter < 0); rtos_mutex_unlock(&((v)->m));
00160   return ret;
00161 }
00162 
00163 #endif
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3