Orocos Real-Time Toolkit
2.5.0
|
00001 /*************************************************************************** 00002 tag: Peter Soetens Thu Oct 22 11:59:07 CEST 2009 oro_atomic.h 00003 00004 oro_atomic.h - description 00005 ------------------- 00006 begin : Thu October 22 2009 00007 copyright : (C) 2009 Peter Soetens 00008 email : peter@thesourcworks.com 00009 00010 *************************************************************************** 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of the GNU General Public * 00013 * License as published by the Free Software Foundation; * 00014 * version 2 of the License. * 00015 * * 00016 * As a special exception, you may use this file as part of a free * 00017 * software library without restriction. Specifically, if other files * 00018 * instantiate templates or use macros or inline functions from this * 00019 * file, or you compile this file and link it with other files to * 00020 * produce an executable, this file does not by itself cause the * 00021 * resulting executable to be covered by the GNU General Public * 00022 * License. This exception does not however invalidate any other * 00023 * reasons why the executable file might be covered by the GNU General * 00024 * Public License. * 00025 * * 00026 * This library is distributed in the hope that it will be useful, * 00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00029 * Lesser General Public License for more details. * 00030 * * 00031 * You should have received a copy of the GNU General Public * 00032 * License along with this library; if not, write to the Free Software * 00033 * Foundation, Inc., 59 Temple Place, * 00034 * Suite 330, Boston, MA 02111-1307 USA * 00035 * * 00036 ***************************************************************************/ 00037 00038 00039 00040 #ifndef __ARCH_NOASM_ORO_ATOMIC__ 00041 #define __ARCH_NOASM_ORO_ATOMIC__ 00042 00043 /* 00044 * @file Atomic operations that C can't guarantee us. Useful for 00045 * resource counting etc. We emulate it using mutexes. 00046 */ 00047 00048 #include "../../rtt-config.h" 00049 #include "../fosi.h" 00050 00051 #ifndef __GNUC__ 00052 #define __inline__ 00053 #endif 00054 00059 typedef struct { 00060 rt_mutex_t m; 00061 int volatile cnter; 00062 } oro_atomic_t; 00063 00064 #define ORO_ATOMIC_SETUP(a_int,n) rtos_mutex_init(&((a_int)->m)); (a_int)->cnter = (n) 00065 #define ORO_ATOMIC_CLEANUP(a_int) rtos_mutex_destroy(&((a_int)->m)) 00066 00067 #define oro_atomic_read(a_int) ((a_int)->cnter) 00068 00069 #define oro_atomic_set(a_int,n) (((a_int)->cnter) = (n)) 00070 00071 static __inline__ void oro_atomic_add(oro_atomic_t *a_int, int n ) 00072 { 00073 rtos_mutex_lock(&((a_int)->m)); (a_int)->cnter += n; rtos_mutex_unlock(&((a_int)->m)); 00074 } 00075 00076 static __inline__ void oro_atomic_sub(oro_atomic_t *a_int, int n ) 00077 { 00078 rtos_mutex_lock(&((a_int)->m)); (a_int)->cnter -= n; rtos_mutex_unlock(&((a_int)->m)); 00079 } 00080 00081 static __inline__ int oro_atomic_add_and_test(oro_atomic_t *a_int, int n ) 00082 { 00083 int ret = 0; 00084 rtos_mutex_lock(&((a_int)->m)); (a_int)->cnter -= n; ret = ((a_int)->cnter == 0); rtos_mutex_unlock(&((a_int)->m)); 00085 return ret; 00086 } 00087 00088 static __inline__ int oro_atomic_sub_and_test(oro_atomic_t *a_int, int n ) 00089 { 00090 int ret = 0; 00091 rtos_mutex_lock(&((a_int)->m)); (a_int)->cnter -= n; ret = ((a_int)->cnter == 0); rtos_mutex_unlock(&((a_int)->m)); 00092 return ret; 00093 } 00094 00095 static __inline__ void oro_atomic_inc(oro_atomic_t *a_int) 00096 { 00097 rtos_mutex_lock(&((a_int)->m)); ++((a_int)->cnter) ; rtos_mutex_unlock(&((a_int)->m)); 00098 } 00099 00100 static __inline__ void oro_atomic_dec(oro_atomic_t *a_int) 00101 { 00102 rtos_mutex_lock(&((a_int)->m)); --((a_int)->cnter) ; rtos_mutex_unlock(&((a_int)->m)); 00103 } 00104 00105 static __inline__ int oro_atomic_dec_and_test(oro_atomic_t *a_int) 00106 { 00107 int ret = 0; 00108 rtos_mutex_lock(&((a_int)->m)); --((a_int)->cnter); ret = ((a_int)->cnter == 0); rtos_mutex_unlock(&((a_int)->m)); 00109 return ret; 00110 } 00111 00112 static __inline__ int oro_atomic_inc_and_test(oro_atomic_t *a_int) 00113 { 00114 int ret = 0; 00115 rtos_mutex_lock(&((a_int)->m)); ++((a_int)->cnter); ret = ((a_int)->cnter == 0); rtos_mutex_unlock(&((a_int)->m)); 00116 return ret; 00117 } 00118 00119 #endif