Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

156 lines
5.4KB

  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/queue.h
  21. * @brief Lock-free double-ended queue.
  22. * This file is a GNU parallel extension to the Standard C++ Library.
  23. */
  24. // Written by Johannes Singler.
  25. #ifndef _GLIBCXX_PARALLEL_QUEUE_H
  26. #define _GLIBCXX_PARALLEL_QUEUE_H 1
  27. #include <parallel/types.h>
  28. #include <parallel/base.h>
  29. #include <parallel/compatibility.h>
  30. /** @brief Decide whether to declare certain variable volatile in this file. */
  31. #define _GLIBCXX_VOLATILE volatile
  32. namespace __gnu_parallel
  33. {
  34. /**@brief Double-ended queue of bounded size, allowing lock-free
  35. * atomic access. push_front() and pop_front() must not be called
  36. * concurrently to each other, while pop_back() can be called
  37. * concurrently at all times.
  38. * @c empty(), @c size(), and @c top() are intentionally not provided.
  39. * Calling them would not make sense in a concurrent setting.
  40. * @param _Tp Contained element type. */
  41. template<typename _Tp>
  42. class _RestrictedBoundedConcurrentQueue
  43. {
  44. private:
  45. /** @brief Array of elements, seen as cyclic buffer. */
  46. _Tp* _M_base;
  47. /** @brief Maximal number of elements contained at the same time. */
  48. _SequenceIndex _M_max_size;
  49. /** @brief Cyclic __begin and __end pointers contained in one
  50. atomically changeable value. */
  51. _GLIBCXX_VOLATILE _CASable _M_borders;
  52. public:
  53. /** @brief Constructor. Not to be called concurrent, of course.
  54. * @param __max_size Maximal number of elements to be contained. */
  55. _RestrictedBoundedConcurrentQueue(_SequenceIndex __max_size)
  56. {
  57. _M_max_size = __max_size;
  58. _M_base = new _Tp[__max_size];
  59. _M_borders = __encode2(0, 0);
  60. #pragma omp flush
  61. }
  62. /** @brief Destructor. Not to be called concurrent, of course. */
  63. ~_RestrictedBoundedConcurrentQueue()
  64. { delete[] _M_base; }
  65. /** @brief Pushes one element into the queue at the front end.
  66. * Must not be called concurrently with pop_front(). */
  67. void
  68. push_front(const _Tp& __t)
  69. {
  70. _CASable __former_borders = _M_borders;
  71. int __former_front, __former_back;
  72. __decode2(__former_borders, __former_front, __former_back);
  73. *(_M_base + __former_front % _M_max_size) = __t;
  74. #if _GLIBCXX_PARALLEL_ASSERTIONS
  75. // Otherwise: front - back > _M_max_size eventually.
  76. _GLIBCXX_PARALLEL_ASSERT(((__former_front + 1) - __former_back)
  77. <= _M_max_size);
  78. #endif
  79. __fetch_and_add(&_M_borders, __encode2(1, 0));
  80. }
  81. /** @brief Pops one element from the queue at the front end.
  82. * Must not be called concurrently with pop_front(). */
  83. bool
  84. pop_front(_Tp& __t)
  85. {
  86. int __former_front, __former_back;
  87. #pragma omp flush
  88. __decode2(_M_borders, __former_front, __former_back);
  89. while (__former_front > __former_back)
  90. {
  91. // Chance.
  92. _CASable __former_borders = __encode2(__former_front,
  93. __former_back);
  94. _CASable __new_borders = __encode2(__former_front - 1,
  95. __former_back);
  96. if (__compare_and_swap(&_M_borders, __former_borders,
  97. __new_borders))
  98. {
  99. __t = *(_M_base + (__former_front - 1) % _M_max_size);
  100. return true;
  101. }
  102. #pragma omp flush
  103. __decode2(_M_borders, __former_front, __former_back);
  104. }
  105. return false;
  106. }
  107. /** @brief Pops one element from the queue at the front end.
  108. * Must not be called concurrently with pop_front(). */
  109. bool
  110. pop_back(_Tp& __t) //queue behavior
  111. {
  112. int __former_front, __former_back;
  113. #pragma omp flush
  114. __decode2(_M_borders, __former_front, __former_back);
  115. while (__former_front > __former_back)
  116. {
  117. // Chance.
  118. _CASable __former_borders = __encode2(__former_front,
  119. __former_back);
  120. _CASable __new_borders = __encode2(__former_front,
  121. __former_back + 1);
  122. if (__compare_and_swap(&_M_borders, __former_borders,
  123. __new_borders))
  124. {
  125. __t = *(_M_base + __former_back % _M_max_size);
  126. return true;
  127. }
  128. #pragma omp flush
  129. __decode2(_M_borders, __former_front, __former_back);
  130. }
  131. return false;
  132. }
  133. };
  134. } //namespace __gnu_parallel
  135. #undef _GLIBCXX_VOLATILE
  136. #endif /* _GLIBCXX_PARALLEL_QUEUE_H */