]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/parallel/compatibility.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / parallel / compatibility.h
CommitLineData
c2ba9709
JS
1// -*- C++ -*-
2
8d9254fc 3// Copyright (C) 2007-2020 Free Software Foundation, Inc.
c2ba9709
JS
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
748086b7 8// Foundation; either version 3, or (at your option) any later
c2ba9709
JS
9// version.
10
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.
15
748086b7
JJ
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.
c2ba9709 19
748086b7
JJ
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/>.
c2ba9709
JS
24
25/** @file parallel/compatibility.h
26 * @brief Compatibility layer, mostly concerned with atomic operations.
693ac783
JW
27 *
28 * This file is a GNU parallel extension to the Standard C++ Library
29 * and contains implementation details for the library's internal use.
c2ba9709
JS
30 */
31
32// Written by Felix Putze.
33
34#ifndef _GLIBCXX_PARALLEL_COMPATIBILITY_H
35#define _GLIBCXX_PARALLEL_COMPATIBILITY_H 1
36
37#include <parallel/types.h>
38#include <parallel/base.h>
39
cccc4450 40#if !defined(_WIN32) || defined (__CYGWIN__)
c2ba9709
JS
41#include <sched.h>
42#endif
43
cccc4450
DS
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.
48extern "C"
49__attribute((dllimport)) void __attribute__((stdcall)) Sleep (unsigned long);
50#endif
51
c2ba9709
JS
52namespace __gnu_parallel
53{
80241991
JW
54 template<typename _Tp>
55 inline _Tp
56 __add_omp(volatile _Tp* __ptr, _Tp __addend)
c2ba9709 57 {
80241991
JW
58 int64_t __res;
59#pragma omp critical
60 {
61 __res = *__ptr;
62 *(__ptr) += __addend;
63 }
64 return __res;
c2ba9709 65 }
c2ba9709
JS
66
67 /** @brief Add a value to a variable, atomically.
68 *
1acba85b
JS
69 * @param __ptr Pointer to a signed integer.
70 * @param __addend Value to add.
c2ba9709 71 */
1acba85b 72 template<typename _Tp>
80241991
JW
73 inline _Tp
74 __fetch_and_add(volatile _Tp* __ptr, _Tp __addend)
75 {
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);
79 }
c2ba9709 80
80241991
JW
81 template<typename _Tp>
82 inline bool
83 __cas_omp(volatile _Tp* __ptr, _Tp __comparand, _Tp __replacement)
c2ba9709 84 {
80241991
JW
85 bool __res = false;
86#pragma omp critical
87 {
88 if (*__ptr == __comparand)
89 {
90 *__ptr = __replacement;
91 __res = true;
92 }
93 }
94 return __res;
c2ba9709 95 }
c2ba9709 96
80241991
JW
97 /** @brief Compare-and-swap
98 *
99 * Compare @c *__ptr and @c __comparand. If equal, let @c
8e32aa11 100 * *__ptr=__replacement and return @c true, return @c false otherwise.
c2ba9709 101 *
1acba85b
JS
102 * @param __ptr Pointer to signed integer.
103 * @param __comparand Compare value.
693ac783
JW
104 * @param __replacement Replacement value.
105 */
1acba85b 106 template<typename _Tp>
80241991
JW
107 inline bool
108 __compare_and_swap(volatile _Tp* __ptr, _Tp __comparand, _Tp __replacement)
109 {
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);
115 }
c2ba9709 116
80241991 117 /** @brief Yield control to another thread, without waiting for
693ac783
JW
118 * the end of the time slice.
119 */
c2ba9709 120 inline void
1acba85b 121 __yield()
c2ba9709 122 {
cccc4450 123#if defined (_WIN32) && !defined (__CYGWIN__)
c2ba9709
JS
124 Sleep(0);
125#else
126 sched_yield();
127#endif
128 }
129} // end namespace
130
cbcd1e45 131#endif /* _GLIBCXX_PARALLEL_COMPATIBILITY_H */