00d799392b5feea81ce2cc98b0a7d64e3efd286c
[kopensolaris-gnu/glibc.git] / sysdeps / sparc / sparc32 / bits / atomic.h
1 /* Atomic operations.  sparc32 version.
2    Copyright (C) 2003 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #ifndef _BITS_ATOMIC_H
22 #define _BITS_ATOMIC_H  1
23
24 /* We have no compare and swap, just test and set.
25    The following implementation contends on 64 global locks
26    per library and assumes no variable will be accessed using atomic.h
27    macros from two different libraries.  */
28
29 volatile unsigned char __sparc32_atomic_locks[64]
30   __attribute__ ((nocommon, section (".gnu.linkonce.b.__sparc32_atomic_locks"),
31                   visibility ("hidden")));
32
33 #define __sparc32_atomic_do_lock(addr) \
34   do                                                                  \
35     {                                                                 \
36       unsigned int __old_lock;                                        \
37       unsigned int __idx = (((long) addr >> 2) ^ ((long) addr >> 12)) \
38                            & 63;                                      \
39       do                                                              \
40         __asm ("ldstub %1, %0"                                        \
41                : "=r" (__old_lock),                                   \
42                  "=m" (__sparc32_atomic_locks[__idx])                 \
43                : "m" (__sparc32_atomic_locks[__idx]));                \
44       while (__old_lock);                                             \
45     }                                                                 \
46   while (0)
47
48 #define __sparc32_atomic_do_unlock(addr) \
49   do                                                                  \
50     __sparc32_atomic_locks[(((long) addr >> 2)                        \
51                             ^ ((long) addr >> 12)) & 63] = 0;         \
52   while (0)
53
54 /* The only basic operation needed is compare and exchange.  */
55 #define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
56   ({ __typeof (mem) __acev_memp = (mem);                              \
57      __typeof (*mem) __acev_ret;                                      \
58      __typeof (*mem) __acev_newval = (newval);                        \
59                                                                       \
60      __sparc32_atomic_do_lock (__acev_memp);                          \
61      __acev_ret = *__acev_memp;                                       \
62      if (__acev_ret == (oldval))                                      \
63        *__acev_memp = __acev_newval;                                  \
64      __sparc32_atomic_do_unlock (__acev_memp);                        \
65      __acev_ret; })
66
67 #define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
68   ({ __typeof (mem) __aceb_memp = (mem);                              \
69      int __aceb_ret;                                                  \
70      __typeof (*mem) __aceb_newval = (newval);                        \
71                                                                       \
72      __sparc32_atomic_do_lock (__aceb_memp);                          \
73      __aceb_ret = 0;                                                  \
74      if (*__aceb_memp == (oldval))                                    \
75        *__aceb_memp = __aceb_newval;                                  \
76      else                                                             \
77        __aceb_ret = 1;                                                \
78      __sparc32_atomic_do_unlock (__aceb_memp);                        \
79      __aceb_ret; })
80
81 #endif  /* bits/atomic.h */