atomicity.h revision 117397
1// Low-level functions for atomic operations: Sparc version  -*- C++ -*-
2
3// Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2, or (at your option)
9// any later version.
10
11// This 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
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING.  If not, write to the Free
18// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19// USA.
20
21// As a special exception, you may use this file as part of a free software
22// library without restriction.  Specifically, if other files instantiate
23// templates or use macros or inline functions from this file, or you compile
24// this file and link it with other files to produce an executable, this
25// file does not by itself cause the resulting executable to be covered by
26// the GNU General Public License.  This exception does not however
27// invalidate any other reasons why the executable file might be covered by
28// the GNU General Public License.
29
30#ifndef _BITS_ATOMICITY_H
31#define _BITS_ATOMICITY_H	1
32
33#ifdef __arch64__
34
35typedef long _Atomic_word;
36
37static inline _Atomic_word
38__attribute__ ((__unused__))
39__exchange_and_add (volatile _Atomic_word *__mem, int __val)
40{
41  _Atomic_word __tmp1, __tmp2;
42
43  __asm__ __volatile__("1:	ldx	[%2], %0\n\t"
44		       "	add	%0, %3, %1\n\t"
45		       "	casx	[%2], %0, %1\n\t"
46		       "	sub	%0, %1, %0\n\t"
47		       "	brnz,pn	%0, 1b\n\t"
48		       "	 nop"
49		       : "=&r" (__tmp1), "=&r" (__tmp2)
50		       : "r" (__mem), "r" (__val)
51		       : "memory");
52  return __tmp2;
53}
54
55static inline void
56__attribute__ ((__unused__))
57__atomic_add (volatile _Atomic_word* __mem, int __val)
58{
59  _Atomic_word __tmp1, __tmp2;
60
61  __asm__ __volatile__("1:	ldx	[%2], %0\n\t"
62		       "	add	%0, %3, %1\n\t"
63		       "	casx	[%2], %0, %1\n\t"
64		       "	sub	%0, %1, %0\n\t"
65		       "	brnz,pn	%0, 1b\n\t"
66		       "	 nop"
67		       : "=&r" (__tmp1), "=&r" (__tmp2)
68		       : "r" (__mem), "r" (__val)
69		       : "memory");
70}
71
72#else /* __arch32__ */
73
74typedef int _Atomic_word;
75
76template <int __inst>
77struct __Atomicity_lock
78{
79  static unsigned char _S_atomicity_lock;
80};
81
82template <int __inst>
83unsigned char __Atomicity_lock<__inst>::_S_atomicity_lock = 0;
84
85template unsigned char __Atomicity_lock<0>::_S_atomicity_lock;
86
87static int
88__attribute__ ((__unused__))
89__exchange_and_add (volatile _Atomic_word* __mem, int __val)
90{
91  _Atomic_word __result, __tmp;
92
93  __asm__ __volatile__("1:	ldstub	[%1], %0\n\t"
94		       "	cmp	%0, 0\n\t"
95		       "	bne	1b\n\t"
96		       "	 nop"
97		       : "=&r" (__tmp)
98		       : "r" (&__Atomicity_lock<0>::_S_atomicity_lock)
99		       : "memory");
100  __result = *__mem;
101  *__mem += __val;
102  __asm__ __volatile__("stb	%%g0, [%0]"
103		       : /* no outputs */
104		       : "r" (&__Atomicity_lock<0>::_S_atomicity_lock)
105		       : "memory");
106  return __result;
107}
108
109static void
110__attribute__ ((__unused__))
111__atomic_add (volatile _Atomic_word* __mem, int __val)
112{
113  _Atomic_word __tmp;
114
115  __asm__ __volatile__("1:	ldstub	[%1], %0\n\t"
116		       "	cmp	%0, 0\n\t"
117		       "	bne	1b\n\t"
118		       "	 nop"
119		       : "=&r" (__tmp)
120		       : "r" (&__Atomicity_lock<0>::_S_atomicity_lock)
121		       : "memory");
122  *__mem += __val;
123  __asm__ __volatile__("stb	%%g0, [%0]"
124		       : /* no outputs */
125		       : "r" (&__Atomicity_lock<0>::_S_atomicity_lock)
126		       : "memory");
127}
128
129#endif /* __arch32__ */
130
131#endif /* atomicity.h */
132