ee381dc27bdcf059a4f98c21122971eeb599dfb9
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / hppa / bits / atomic.h
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Carlos O'Donell <carlos@baldric.uwo.ca>, 2005.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <stdint.h>
21 #include <sysdep.h>
22 #include <abort-instr.h>
23 #include <kernel-features.h>
24
25 /* We need EFAULT, ENONSYS, and EAGAIN */
26 #if !defined EFAULT && !defined ENOSYS && !defined EAGAIN
27 #undef EFAULT
28 #undef ENOSYS
29 #undef EAGAIN
30 #define EFAULT  14
31 #define ENOSYS  251
32 #define EAGAIN  11
33 #endif
34
35 #ifndef _BITS_ATOMIC_H
36 #define _BITS_ATOMIC_H  1
37
38 typedef int8_t atomic8_t;
39 typedef uint8_t uatomic8_t;
40 typedef int_fast8_t atomic_fast8_t;
41 typedef uint_fast8_t uatomic_fast8_t;
42
43 typedef int32_t atomic32_t;
44 typedef uint32_t uatomic32_t;
45 typedef int_fast32_t atomic_fast32_t;
46 typedef uint_fast32_t uatomic_fast32_t;
47
48 typedef intptr_t atomicptr_t;
49 typedef uintptr_t uatomicptr_t;
50 typedef intmax_t atomic_max_t;
51 typedef uintmax_t uatomic_max_t;
52
53 /* prev = *addr;
54    if (prev == old)
55      *addr = new;
56    return prev; */
57
58 /* Use the kernel atomic light weight syscalls on hppa */ 
59 #define LWS "0xb0"
60 #define LWS_CAS 0x0
61 /* Note r31 is the link register */
62 #define LWS_CLOBBER "r1", "r26", "r25", "r24", "r23", "r22", "r21", "r20", "r28", "r31", "memory"
63 #define ASM_EAGAIN -EAGAIN
64
65 #if __ASSUME_LWS_CAS
66 /* The only basic operation needed is compare and exchange.  */
67 # define atomic_compare_and_exchange_val_acq(mem, newval, oldval)       \
68   ({                                                                    \
69      volatile int lws_errno = EFAULT;                                   \
70      volatile int lws_ret = 0xdeadbeef;                                 \
71      asm volatile(                                                      \
72         "0:                                     \n\t"                   \
73         "copy   %3, %%r26                       \n\t"                   \
74         "copy   %4, %%r25                       \n\t"                   \
75         "copy   %5, %%r24                       \n\t"                   \
76         "ble    " LWS "(%%sr2, %%r0)            \n\t"                   \
77         "ldi    0, %%r20                        \n\t"                   \
78         "cmpib,=,n " ASM_EAGAIN ",%%r21,0       \n\t"                   \
79         "nop                                    \n\t"                   \
80         "stw    %%r28, %0                       \n\t"                   \
81         "sub    %%r0, %%r21, %%r21              \n\t"                   \
82         "stw    %%r21, %1                       \n\t"                   \
83         : "=m" (lws_ret), "=m" (lws_errno), "=m" (*mem)                 \
84         : "r" (mem), "r" (oldval), "r" (newval)                         \
85         : LWS_CLOBBER                                                   \
86      );                                                                 \
87                                                                         \
88      if(lws_errno == EFAULT || lws_errno == ENOSYS)                     \
89         ABORT_INSTRUCTION;                                              \
90                                                                         \
91      lws_ret;                                                           \
92    })
93
94 # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval)      \
95   ({                                                                    \
96      int ret;                                                           \
97      ret = atomic_compare_and_exchange_val_acq(mem, newval, oldval);    \
98      /* Return 1 if it was already acquired */                          \
99      (ret != oldval);                                                   \
100    })
101 #else
102 /* Non-atomic primitives. */
103 # define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
104   ({ __typeof (mem) __gmemp = (mem);                                  \
105      __typeof (*mem) __gret = *__gmemp;                               \
106      __typeof (*mem) __gnewval = (newval);                            \
107                                                                       \
108      if (__gret == (oldval))                                          \
109        *__gmemp = __gnewval;                                          \
110      __gret; })
111
112 # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
113   ({ __typeof (mem) __gmemp = (mem);                                  \
114      __typeof (*mem) __gnewval = (newval);                            \
115                                                                       \
116      *__gmemp == (oldval) ? (*__gmemp = __gnewval, 0) : 1; })
117 #endif
118
119 #endif  /* bits/atomic.h */
120