Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

132 lines
3.7KB

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