136ac495dSmrg // Low-level functions for atomic operations: x86, x >= 3 version -*- C++ -*-
236ac495dSmrg
3*8feb0f0bSmrg // Copyright (C) 2003-2020 Free Software Foundation, Inc.
436ac495dSmrg //
536ac495dSmrg // This file is part of the GNU ISO C++ Library. This library is free
636ac495dSmrg // software; you can redistribute it and/or modify it under the
736ac495dSmrg // terms of the GNU General Public License as published by the
836ac495dSmrg // Free Software Foundation; either version 3, or (at your option)
936ac495dSmrg // any later version.
1036ac495dSmrg
1136ac495dSmrg // This library is distributed in the hope that it will be useful,
1236ac495dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
1336ac495dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1436ac495dSmrg // GNU General Public License for more details.
1536ac495dSmrg
1636ac495dSmrg // Under Section 7 of GPL version 3, you are granted additional
1736ac495dSmrg // permissions described in the GCC Runtime Library Exception, version
1836ac495dSmrg // 3.1, as published by the Free Software Foundation.
1936ac495dSmrg
2036ac495dSmrg // You should have received a copy of the GNU General Public License and
2136ac495dSmrg // a copy of the GCC Runtime Library Exception along with this program;
2236ac495dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
2336ac495dSmrg // <http://www.gnu.org/licenses/>.
2436ac495dSmrg
2536ac495dSmrg #include <ext/atomicity.h>
2636ac495dSmrg
_GLIBCXX_VISIBILITY(default)2736ac495dSmrg namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
2836ac495dSmrg {
2936ac495dSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
3036ac495dSmrg
3136ac495dSmrg template<int __inst>
3236ac495dSmrg struct _Atomicity_lock
3336ac495dSmrg {
3436ac495dSmrg static volatile _Atomic_word _S_atomicity_lock;
3536ac495dSmrg };
3636ac495dSmrg
3736ac495dSmrg template<int __inst>
3836ac495dSmrg volatile _Atomic_word _Atomicity_lock<__inst>::_S_atomicity_lock = 0;
3936ac495dSmrg
4036ac495dSmrg template volatile _Atomic_word _Atomicity_lock<0>::_S_atomicity_lock;
4136ac495dSmrg
4236ac495dSmrg _Atomic_word
4336ac495dSmrg __attribute__ ((__unused__))
4436ac495dSmrg __exchange_and_add(volatile _Atomic_word* __mem, int __val) throw ()
4536ac495dSmrg {
4636ac495dSmrg register _Atomic_word __result, __tmp = 1;
4736ac495dSmrg
4836ac495dSmrg // Obtain the atomic exchange/add spin lock.
4936ac495dSmrg do
5036ac495dSmrg {
5136ac495dSmrg __asm__ __volatile__ ("xchg{l} {%0,%1|%1,%0}"
5236ac495dSmrg : "=m" (_Atomicity_lock<0>::_S_atomicity_lock),
5336ac495dSmrg "+r" (__tmp)
5436ac495dSmrg : "m" (_Atomicity_lock<0>::_S_atomicity_lock));
5536ac495dSmrg }
5636ac495dSmrg while (__tmp);
5736ac495dSmrg
5836ac495dSmrg __result = *__mem;
5936ac495dSmrg *__mem += __val;
6036ac495dSmrg
6136ac495dSmrg // Release spin lock.
6236ac495dSmrg _Atomicity_lock<0>::_S_atomicity_lock = 0;
6336ac495dSmrg
6436ac495dSmrg return __result;
6536ac495dSmrg }
6636ac495dSmrg
6736ac495dSmrg void
6836ac495dSmrg __attribute__ ((__unused__))
6936ac495dSmrg __atomic_add(volatile _Atomic_word* __mem, int __val) throw ()
7036ac495dSmrg { __exchange_and_add(__mem, __val); }
7136ac495dSmrg
7236ac495dSmrg _GLIBCXX_END_NAMESPACE_VERSION
7336ac495dSmrg } // namespace
74