2017-06-18 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libstdc++-v3 / libsupc++ / eh_atomics.h
blob22a45c1b06dc4be31fc111a84e98c1faab42b5bf
1 // Exception Handling support header for -*- C++ -*-
3 // Copyright (C) 2016-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file eh_atomics.h
27 * This is an internal header file, included by library source files.
28 * Do not attempt to use it directly.
31 #ifndef _EH_ATOMICS_H
32 #define _EH_ATOMICS_H 1
34 #include <bits/c++config.h>
35 #include <bits/atomic_word.h>
36 #include <bits/atomic_lockfree_defines.h>
37 #if ATOMIC_INT_LOCK_FREE <= 1
38 # include <ext/atomicity.h>
39 #endif
41 #pragma GCC visibility push(default)
42 extern "C++" {
43 namespace __gnu_cxx
45 void
46 __eh_atomic_inc (_Atomic_word* __count) __attribute__((always_inline));
48 bool
49 __eh_atomic_dec (_Atomic_word* __count) __attribute__((always_inline));
51 // Increments the count.
52 inline void
53 __eh_atomic_inc (_Atomic_word* __count)
55 #if ATOMIC_INT_LOCK_FREE > 1
56 __atomic_add_fetch (__count, 1, __ATOMIC_ACQ_REL);
57 #else
58 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE (__count);
59 __gnu_cxx::__atomic_add_dispatch (__count, 1);
60 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER (__count);
61 #endif
64 // Decrements the count and returns true if it reached zero.
65 inline bool
66 __eh_atomic_dec (_Atomic_word* __count)
68 #if ATOMIC_INT_LOCK_FREE > 1
69 return __atomic_sub_fetch (__count, 1, __ATOMIC_ACQ_REL) == 0;
70 #else
71 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE (__count);
72 if (__gnu_cxx::__exchange_and_add_dispatch (__count, -1) == 1)
74 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER (__count);
75 return true;
77 return false;
78 #endif
80 } // namespace __gnu_cxx
82 #pragma GCC visibility pop
84 #endif // _EH_ATOMICS_H