Reverting merge from trunk
[official-gcc.git] / libstdc++-v3 / include / parallel / compatibility.h
blob6e7959b4c5a763252210a21c6dd7f2cdb0a0167a
1 // -*- C++ -*-
3 // Copyright (C) 2007-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
9 // version.
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file parallel/compatibility.h
26 * @brief Compatibility layer, mostly concerned with atomic operations.
28 * This file is a GNU parallel extension to the Standard C++ Library
29 * and contains implementation details for the library's internal use.
32 // Written by Felix Putze.
34 #ifndef _GLIBCXX_PARALLEL_COMPATIBILITY_H
35 #define _GLIBCXX_PARALLEL_COMPATIBILITY_H 1
37 #include <parallel/types.h>
38 #include <parallel/base.h>
40 #if !defined(_WIN32) || defined (__CYGWIN__)
41 #include <sched.h>
42 #endif
44 #ifdef __MINGW32__
45 // Including <windows.h> will drag in all the windows32 names. Since
46 // that can cause user code portability problems, we just declare the
47 // one needed function here.
48 extern "C"
49 __attribute((dllimport)) void __attribute__((stdcall)) Sleep (unsigned long);
50 #endif
52 namespace __gnu_parallel
54 template<typename _Tp>
55 inline _Tp
56 __add_omp(volatile _Tp* __ptr, _Tp __addend)
58 int64_t __res;
59 #pragma omp critical
61 __res = *__ptr;
62 *(__ptr) += __addend;
64 return __res;
67 /** @brief Add a value to a variable, atomically.
69 * @param __ptr Pointer to a signed integer.
70 * @param __addend Value to add.
72 template<typename _Tp>
73 inline _Tp
74 __fetch_and_add(volatile _Tp* __ptr, _Tp __addend)
76 if (__atomic_always_lock_free(sizeof(_Tp), __ptr))
77 return __atomic_fetch_add(__ptr, __addend, __ATOMIC_ACQ_REL);
78 return __add_omp(__ptr, __addend);
81 template<typename _Tp>
82 inline bool
83 __cas_omp(volatile _Tp* __ptr, _Tp __comparand, _Tp __replacement)
85 bool __res = false;
86 #pragma omp critical
88 if (*__ptr == __comparand)
90 *__ptr = __replacement;
91 __res = true;
94 return __res;
97 /** @brief Compare-and-swap
99 * Compare @c *__ptr and @c __comparand. If equal, let @c
100 * *__ptr=__replacement and return @c true, return @c false otherwise.
102 * @param __ptr Pointer to signed integer.
103 * @param __comparand Compare value.
104 * @param __replacement Replacement value.
106 template<typename _Tp>
107 inline bool
108 __compare_and_swap(volatile _Tp* __ptr, _Tp __comparand, _Tp __replacement)
110 if (__atomic_always_lock_free(sizeof(_Tp), __ptr))
111 return __atomic_compare_exchange_n(__ptr, &__comparand, __replacement,
112 false, __ATOMIC_ACQ_REL,
113 __ATOMIC_RELAXED);
114 return __cas_omp(__ptr, __comparand, __replacement);
117 /** @brief Yield control to another thread, without waiting for
118 * the end of the time slice.
120 inline void
121 __yield()
123 #if defined (_WIN32) && !defined (__CYGWIN__)
124 Sleep(0);
125 #else
126 sched_yield();
127 #endif
129 } // end namespace
131 #endif /* _GLIBCXX_PARALLEL_COMPATIBILITY_H */