1132720Skan// Low-level functions for atomic operations: PA-RISC version  -*- C++ -*-
2117397Skan
3169691Skan// Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4132720Skan//
5132720Skan// This file is part of the GNU ISO C++ Library.  This library is free
6132720Skan// software; you can redistribute it and/or modify it under the
7132720Skan// terms of the GNU General Public License as published by the
8132720Skan// Free Software Foundation; either version 2, or (at your option)
9132720Skan// any later version.
10117397Skan
11132720Skan// This library is distributed in the hope that it will be useful,
12132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14132720Skan// GNU General Public License for more details.
15117397Skan
16132720Skan// You should have received a copy of the GNU General Public License along
17132720Skan// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19132720Skan// USA.
20117397Skan
21132720Skan// As a special exception, you may use this file as part of a free software
22132720Skan// library without restriction.  Specifically, if other files instantiate
23132720Skan// templates or use macros or inline functions from this file, or you compile
24132720Skan// this file and link it with other files to produce an executable, this
25132720Skan// file does not by itself cause the resulting executable to be covered by
26132720Skan// the GNU General Public License.  This exception does not however
27132720Skan// invalidate any other reasons why the executable file might be covered by
28132720Skan// the GNU General Public License.
29117397Skan
30132720Skan#include <bits/c++config.h>
31169691Skan#include <ext/atomicity.h>
32117397Skan
33169691Skan_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
34169691Skan
35132720Skan  template<int _Inst>
36132720Skan    struct _Atomicity_lock
37132720Skan    {
38132720Skan      static volatile int _S_atomicity_lock;
39132720Skan    };
40132720Skan
41132720Skan  template<int _Inst>
42132720Skan  volatile int
43132720Skan  _Atomicity_lock<_Inst>::_S_atomicity_lock __attribute__ ((aligned (16))) = 1;
44117397Skan
45132720Skan  // Because of the lack of weak support when using the hpux som
46132720Skan  // linker, we explicitly instantiate the atomicity lock.
47132720Skan  template volatile int _Atomicity_lock<0>::_S_atomicity_lock;
48117397Skan
49132720Skan  int
50132720Skan  __attribute__ ((__unused__))
51132720Skan  __exchange_and_add(volatile _Atomic_word* __mem, int __val)
52132720Skan  {
53132720Skan    _Atomic_word result;
54132720Skan    int tmp;
55132720Skan    volatile int& lock = _Atomicity_lock<0>::_S_atomicity_lock;
56132720Skan
57132720Skan    __asm__ __volatile__ ("ldcw 0(%1),%0\n\t"
58132720Skan			  "cmpib,<>,n 0,%0,.+20\n\t"
59132720Skan			  "ldw 0(%1),%0\n\t"
60132720Skan			  "cmpib,= 0,%0,.-4\n\t"
61132720Skan			  "nop\n\t"
62132720Skan			  "b,n .-20"
63132720Skan			  : "=&r" (tmp)
64169691Skan			  : "r" (&lock)
65169691Skan			  : "memory");
66132720Skan
67132720Skan    result = *__mem;
68132720Skan    *__mem = result + __val;
69169691Skan    __asm__ __volatile__ ("stw %1,0(%0)"
70132720Skan			  : : "r" (&lock), "r" (tmp) : "memory");
71132720Skan    return result;
72132720Skan  }
73132720Skan
74132720Skan  void
75132720Skan  __attribute__ ((__unused__))
76132720Skan  __atomic_add(volatile _Atomic_word* __mem, int __val)
77132720Skan  {
78132720Skan    int tmp;
79132720Skan    volatile int& lock = _Atomicity_lock<0>::_S_atomicity_lock;
80132720Skan
81132720Skan    __asm__ __volatile__ ("ldcw 0(%1),%0\n\t"
82132720Skan			  "cmpib,<>,n 0,%0,.+20\n\t"
83132720Skan			  "ldw 0(%1),%0\n\t"
84132720Skan			  "cmpib,= 0,%0,.-4\n\t"
85132720Skan			  "nop\n\t"
86132720Skan			  "b,n .-20"
87132720Skan			  : "=&r" (tmp)
88169691Skan			  : "r" (&lock)
89169691Skan			  : "memory");
90132720Skan
91132720Skan    *__mem += __val;
92169691Skan    __asm__ __volatile__ ("stw %1,0(%0)"
93132720Skan			  : : "r" (&lock), "r" (tmp) : "memory");
94132720Skan  }
95169691Skan
96169691Skan_GLIBCXX_END_NAMESPACE
97