xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/ext/atomicity.h (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg // Support for atomic operations -*- C++ -*-
236ac495dSmrg 
3*8feb0f0bSmrg // Copyright (C) 2004-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 /** @file ext/atomicity.h
2636ac495dSmrg  *  This file is a GNU extension to the Standard C++ Library.
2736ac495dSmrg  */
2836ac495dSmrg 
2936ac495dSmrg #ifndef _GLIBCXX_ATOMICITY_H
3036ac495dSmrg #define _GLIBCXX_ATOMICITY_H	1
3136ac495dSmrg 
3236ac495dSmrg #pragma GCC system_header
3336ac495dSmrg 
3436ac495dSmrg #include <bits/c++config.h>
3536ac495dSmrg #include <bits/gthr.h>
3636ac495dSmrg #include <bits/atomic_word.h>
3736ac495dSmrg 
_GLIBCXX_VISIBILITY(default)3836ac495dSmrg namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
3936ac495dSmrg {
4036ac495dSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
4136ac495dSmrg 
4236ac495dSmrg   // Functions for portable atomic access.
4336ac495dSmrg   // To abstract locking primitives across all thread policies, use:
4436ac495dSmrg   // __exchange_and_add_dispatch
4536ac495dSmrg   // __atomic_add_dispatch
4636ac495dSmrg #ifdef _GLIBCXX_ATOMIC_BUILTINS
47*8feb0f0bSmrg   inline _Atomic_word
48*8feb0f0bSmrg   __attribute__((__always_inline__))
4936ac495dSmrg   __exchange_and_add(volatile _Atomic_word* __mem, int __val)
5036ac495dSmrg   { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
5136ac495dSmrg 
52*8feb0f0bSmrg   inline void
53*8feb0f0bSmrg   __attribute__((__always_inline__))
5436ac495dSmrg   __atomic_add(volatile _Atomic_word* __mem, int __val)
5536ac495dSmrg   { __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
5636ac495dSmrg #else
5736ac495dSmrg   _Atomic_word
58*8feb0f0bSmrg   __exchange_and_add(volatile _Atomic_word*, int) _GLIBCXX_NOTHROW;
5936ac495dSmrg 
6036ac495dSmrg   void
61*8feb0f0bSmrg   __atomic_add(volatile _Atomic_word*, int) _GLIBCXX_NOTHROW;
6236ac495dSmrg #endif
6336ac495dSmrg 
64*8feb0f0bSmrg   inline _Atomic_word
65*8feb0f0bSmrg   __attribute__((__always_inline__))
6636ac495dSmrg   __exchange_and_add_single(_Atomic_word* __mem, int __val)
6736ac495dSmrg   {
6836ac495dSmrg     _Atomic_word __result = *__mem;
6936ac495dSmrg     *__mem += __val;
7036ac495dSmrg     return __result;
7136ac495dSmrg   }
7236ac495dSmrg 
73*8feb0f0bSmrg   inline void
74*8feb0f0bSmrg   __attribute__((__always_inline__))
7536ac495dSmrg   __atomic_add_single(_Atomic_word* __mem, int __val)
7636ac495dSmrg   { *__mem += __val; }
7736ac495dSmrg 
78*8feb0f0bSmrg   inline _Atomic_word
79*8feb0f0bSmrg   __attribute__ ((__always_inline__))
8036ac495dSmrg   __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
8136ac495dSmrg   {
8236ac495dSmrg #ifdef __GTHREADS
8336ac495dSmrg     if (__gthread_active_p())
8436ac495dSmrg       return __exchange_and_add(__mem, __val);
8536ac495dSmrg #endif
86*8feb0f0bSmrg     return __exchange_and_add_single(__mem, __val);
8736ac495dSmrg   }
8836ac495dSmrg 
89*8feb0f0bSmrg   inline void
90*8feb0f0bSmrg   __attribute__ ((__always_inline__))
9136ac495dSmrg   __atomic_add_dispatch(_Atomic_word* __mem, int __val)
9236ac495dSmrg   {
9336ac495dSmrg #ifdef __GTHREADS
9436ac495dSmrg     if (__gthread_active_p())
95*8feb0f0bSmrg       {
9636ac495dSmrg 	__atomic_add(__mem, __val);
97*8feb0f0bSmrg 	return;
98*8feb0f0bSmrg       }
9936ac495dSmrg #endif
100*8feb0f0bSmrg     __atomic_add_single(__mem, __val);
10136ac495dSmrg   }
10236ac495dSmrg 
10336ac495dSmrg _GLIBCXX_END_NAMESPACE_VERSION
10436ac495dSmrg } // namespace
10536ac495dSmrg 
10636ac495dSmrg // Even if the CPU doesn't need a memory barrier, we need to ensure
10736ac495dSmrg // that the compiler doesn't reorder memory accesses across the
10836ac495dSmrg // barriers.
10936ac495dSmrg #ifndef _GLIBCXX_READ_MEM_BARRIER
11036ac495dSmrg #define _GLIBCXX_READ_MEM_BARRIER __atomic_thread_fence (__ATOMIC_ACQUIRE)
11136ac495dSmrg #endif
11236ac495dSmrg #ifndef _GLIBCXX_WRITE_MEM_BARRIER
11336ac495dSmrg #define _GLIBCXX_WRITE_MEM_BARRIER __atomic_thread_fence (__ATOMIC_RELEASE)
11436ac495dSmrg #endif
11536ac495dSmrg 
11636ac495dSmrg #endif
117