Back to index

glibc  2.9
atomicity.h
Go to the documentation of this file.
00001 /* Low-level functions for atomic operations.  AM33 version.
00002    Copyright 1999, 2001 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Alexandre Oliva <aoliva@redhat.com>.
00005    Based on ../sparc/sparc32/atomicity.h
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public License as
00009    published by the Free Software Foundation; either version 2 of the
00010    License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00019    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020    Boston, MA 02111-1307, USA.  */
00021 
00022 #ifndef _ATOMICITY_H
00023 #define _ATOMICITY_H 1
00024 
00025 #include <inttypes.h>
00026 
00027 #define __acquire_lock(lock) \
00028   __asm__ __volatile__("1:  bset   %1, (%0)\n\t"        \
00029                      "      beq    1b"                  \
00030                      : : "a" (&(lock)), "d" (1)         \
00031                      : "memory")
00032 
00033 #define __release_lock(lock) lock = 0
00034 
00035 static int
00036 __attribute__ ((unused))
00037 exchange_and_add (volatile uint32_t *mem, int val)
00038 {
00039   static unsigned char lock;
00040   int result;
00041 
00042   __acquire_lock (lock);
00043 
00044   result = *mem;
00045   *mem += val;
00046 
00047   __release_lock (lock);
00048 
00049   return result;
00050 }
00051 
00052 static void
00053 __attribute__ ((unused))
00054 atomic_add (volatile uint32_t *mem, int val)
00055 {
00056   static unsigned char lock;
00057 
00058   __acquire_lock (lock);
00059 
00060   *mem += val;
00061 
00062   __release_lock (lock);
00063 }
00064 
00065 static int
00066 __attribute__ ((unused))
00067 compare_and_swap (volatile long int *p, long int oldval, long int newval)
00068 {
00069   static unsigned char lock;
00070   int ret;
00071 
00072   __acquire_lock (lock);
00073 
00074   if (*p != oldval)
00075     ret = 0;
00076   else
00077     {
00078       *p = newval;
00079       ret = 1;
00080     }
00081 
00082   __release_lock (lock);
00083 
00084   return ret;
00085 }
00086 
00087 #endif /* atomicity.h */