2006-12-03 Carlos O'Donell <carlos@systemhalted.org>
[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 */
26 #if !defined EFAULT && !defined ENOSYS
27 #define EFAULT  14
28 #define ENOSYS  251
29 #endif
30
31 #ifndef _BITS_ATOMIC_H
32 #define _BITS_ATOMIC_H  1
33
34 typedef int8_t atomic8_t;
35 typedef uint8_t uatomic8_t;
36 typedef int_fast8_t atomic_fast8_t;
37 typedef uint_fast8_t uatomic_fast8_t;
38
39 typedef int32_t atomic32_t;
40 typedef uint32_t uatomic32_t;
41 typedef int_fast32_t atomic_fast32_t;
42 typedef uint_fast32_t uatomic_fast32_t;
43
44 typedef intptr_t atomicptr_t;
45 typedef uintptr_t uatomicptr_t;
46 typedef intmax_t atomic_max_t;
47 typedef uintmax_t uatomic_max_t;
48
49 /* prev = *addr;
50    if (prev == old)
51      *addr = new;
52    return prev; */
53
54 /* Use the kernel atomic light weight syscalls on hppa */ 
55 #define LWS "0xb0"
56 #define LWS_CAS "0"
57 /* Note r31 is the link register */
58 #define LWS_CLOBBER "r1", "r26", "r25", "r24", "r23", "r22", "r21", "r20", "r28", "r31", "memory"
59 #define ASM_EAGAIN "11" 
60
61 #if __ASSUME_LWS_CAS
62 /* The only basic operation needed is compare and exchange.  */
63 # define atomic_compare_and_exchange_val_acq(mem, newval, oldval)       \
64   ({                                                                    \
65      volatile int lws_errno = EFAULT;                                   \
66      volatile int lws_ret = 0xdeadbeef;                                 \
67      asm volatile(                                                      \
68         "0:                                     \n\t"                   \
69         "copy   %3, %%r26                       \n\t"                   \
70         "copy   %4, %%r25                       \n\t"                   \
71         "copy   %5, %%r24                       \n\t"                   \
72         "ble    " LWS "(%%sr2, %%r0)            \n\t"                   \
73         "ldi    " LWS_CAS ", %%r20              \n\t"                   \
74         "cmpib,=,n " ASM_EAGAIN ",%%r21,0b      \n\t"                   \
75         "nop                                    \n\t"                   \
76         "stw    %%r28, %0                       \n\t"                   \
77         "sub    %%r0, %%r21, %%r21              \n\t"                   \
78         "stw    %%r21, %1                       \n\t"                   \
79         : "=m" (lws_ret), "=m" (lws_errno), "=m" (*mem)                 \
80         : "r" (mem), "r" (oldval), "r" (newval)                         \
81         : LWS_CLOBBER                                                   \
82      );                                                                 \
83                                                                         \
84      if(lws_errno == EFAULT || lws_errno == ENOSYS)                     \
85         ABORT_INSTRUCTION;                                              \
86                                                                         \
87      lws_ret;                                                           \
88    })
89
90 # define atomic_compare_and_exchange_bool_acq(mem, newval, oldval)      \
91   ({                                                                    \
92      int ret;                                                           \
93      ret = atomic_compare_and_exchange_val_acq(mem, newval, oldval);    \
94      /* Return 1 if it was already acquired */                          \
95      (ret != oldval);                                                   \
96    })
97 #else
98 # error __ASSUME_LWS_CAS is required to build glibc.
99 #endif  
100 /* __ASSUME_LWS_CAS */
101
102 #endif
103 /* _BITS_ATOMIC_H */