1117397Skan// Low-level functions for atomic operations: Sparc version  -*- C++ -*-
2117397Skan
3169691Skan// Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005
4169691Skan// Free Software Foundation, Inc.
5117397Skan//
6117397Skan// This file is part of the GNU ISO C++ Library.  This library is free
7117397Skan// software; you can redistribute it and/or modify it under the
8117397Skan// terms of the GNU General Public License as published by the
9117397Skan// Free Software Foundation; either version 2, or (at your option)
10117397Skan// any later version.
11117397Skan
12117397Skan// This library is distributed in the hope that it will be useful,
13117397Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
14117397Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15117397Skan// GNU General Public License for more details.
16117397Skan
17117397Skan// You should have received a copy of the GNU General Public License along
18117397Skan// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20117397Skan// USA.
21117397Skan
22117397Skan// As a special exception, you may use this file as part of a free software
23117397Skan// library without restriction.  Specifically, if other files instantiate
24117397Skan// templates or use macros or inline functions from this file, or you compile
25117397Skan// this file and link it with other files to produce an executable, this
26117397Skan// file does not by itself cause the resulting executable to be covered by
27117397Skan// the GNU General Public License.  This exception does not however
28117397Skan// invalidate any other reasons why the executable file might be covered by
29117397Skan// the GNU General Public License.
30117397Skan
31169691Skan#include <ext/atomicity.h>
32117397Skan
33169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
34169691Skan
35117397Skan#ifdef __arch64__
36132720Skan  _Atomic_word
37132720Skan  __attribute__ ((__unused__))
38132720Skan  __exchange_and_add(volatile _Atomic_word* __mem, int __val)
39132720Skan  {
40132720Skan    _Atomic_word __tmp1, __tmp2;
41132720Skan    _Atomic_word __val_extended = __val;
42117397Skan
43132720Skan    __asm__ __volatile__("1:	ldx	[%3], %0\n\t"
44132720Skan			 "	add	%0, %4, %1\n\t"
45132720Skan			 "	casx	[%3], %0, %1\n\t"
46132720Skan			 "	sub	%0, %1, %0\n\t"
47132720Skan			 "	brnz,pn	%0, 1b\n\t"
48132720Skan			 "	 nop"
49132720Skan			 : "=&r" (__tmp1), "=&r" (__tmp2), "=m" (*__mem)
50132720Skan			 : "r" (__mem), "r" (__val_extended), "m" (*__mem));
51132720Skan    return __tmp2;
52132720Skan  }
53132720Skan
54132720Skan  void
55132720Skan  __attribute__ ((__unused__))
56132720Skan  __atomic_add(volatile _Atomic_word* __mem, int __val)
57132720Skan  {
58132720Skan    _Atomic_word __tmp1, __tmp2;
59132720Skan    _Atomic_word __val_extended = __val;
60132720Skan
61132720Skan    __asm__ __volatile__("1:	ldx	[%3], %0\n\t"
62132720Skan			 "	add	%0, %4, %1\n\t"
63132720Skan			 "	casx	[%3], %0, %1\n\t"
64132720Skan			 "	sub	%0, %1, %0\n\t"
65132720Skan			 "	brnz,pn	%0, 1b\n\t"
66132720Skan			 "	 nop"
67132720Skan			 : "=&r" (__tmp1), "=&r" (__tmp2), "=m" (*__mem)
68132720Skan			 : "r" (__mem), "r" (__val_extended), "m" (*__mem));
69132720Skan  }
70132720Skan
71117397Skan#else /* __arch32__ */
72117397Skan
73132720Skan  template<int __inst>
74132720Skan    struct _Atomicity_lock
75132720Skan    {
76132720Skan      static unsigned char _S_atomicity_lock;
77132720Skan    };
78117397Skan
79132720Skan  template<int __inst>
80132720Skan  unsigned char _Atomicity_lock<__inst>::_S_atomicity_lock = 0;
81132720Skan
82132720Skan  template unsigned char _Atomicity_lock<0>::_S_atomicity_lock;
83132720Skan
84132720Skan  _Atomic_word
85132720Skan  __attribute__ ((__unused__))
86132720Skan  __exchange_and_add(volatile _Atomic_word* __mem, int __val)
87132720Skan  {
88132720Skan    _Atomic_word __result, __tmp;
89132720Skan
90132720Skan    __asm__ __volatile__("1:	ldstub	[%1], %0\n\t"
91132720Skan			 "	cmp	%0, 0\n\t"
92132720Skan			 "	bne	1b\n\t"
93132720Skan			 "	 nop"
94132720Skan			 : "=&r" (__tmp)
95132720Skan			 : "r" (&_Atomicity_lock<0>::_S_atomicity_lock)
96132720Skan			 : "memory");
97132720Skan    __result = *__mem;
98132720Skan    *__mem += __val;
99132720Skan    __asm__ __volatile__("stb	%%g0, [%0]"
100132720Skan			 : /* no outputs */
101132720Skan			 : "r" (&_Atomicity_lock<0>::_S_atomicity_lock)
102132720Skan			 : "memory");
103132720Skan    return __result;
104132720Skan  }
105132720Skan
106132720Skan  void
107132720Skan  __attribute__ ((__unused__))
108132720Skan  __atomic_add(volatile _Atomic_word* __mem, int __val)
109132720Skan  {
110132720Skan    _Atomic_word __tmp;
111132720Skan
112132720Skan    __asm__ __volatile__("1:	ldstub	[%1], %0\n\t"
113132720Skan			 "	cmp	%0, 0\n\t"
114132720Skan			 "	bne	1b\n\t"
115132720Skan			 "	 nop"
116132720Skan			 : "=&r" (__tmp)
117132720Skan			 : "r" (&_Atomicity_lock<0>::_S_atomicity_lock)
118132720Skan			 : "memory");
119132720Skan    *__mem += __val;
120132720Skan    __asm__ __volatile__("stb	%%g0, [%0]"
121132720Skan			 : /* no outputs */
122132720Skan			 : "r" (&_Atomicity_lock<0>::_S_atomicity_lock)
123132720Skan			 : "memory");
124132720Skan  }
125117397Skan#endif /* __arch32__ */
126169691Skan
127169691Skan_GLIBCXX_END_NAMESPACE
128