3 // Copyright (C) 2007-2013 Free Software Foundation, Inc.
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
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__)
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.
49 __attribute((dllimport
)) void __attribute__((stdcall)) Sleep (unsigned long);
52 namespace __gnu_parallel
54 template<typename _Tp
>
56 __add_omp(volatile _Tp
* __ptr
, _Tp __addend
)
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
>
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
>
83 __cas_omp(volatile _Tp
* __ptr
, _Tp __comparand
, _Tp __replacement
)
88 if (*__ptr
== __comparand
)
90 *__ptr
= __replacement
;
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
>
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
,
114 return __cas_omp(__ptr
, __comparand
, __replacement
);
117 /** @brief Yield control to another thread, without waiting for
118 * the end of the time slice.
123 #if defined (_WIN32) && !defined (__CYGWIN__)
131 #endif /* _GLIBCXX_PARALLEL_COMPATIBILITY_H */