queue.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // -*- C++ -*-
  2. // Copyright (C) 2007, 2008, 2009 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 T Contained element type. */
  41. template<typename T>
  42. class RestrictedBoundedConcurrentQueue
  43. {
  44. private:
  45. /** @brief Array of elements, seen as cyclic buffer. */
  46. T* base;
  47. /** @brief Maximal number of elements contained at the same time. */
  48. sequence_index_t max_size;
  49. /** @brief Cyclic begin and end pointers contained in one
  50. atomically changeable value. */
  51. _GLIBCXX_VOLATILE lcas_t 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(sequence_index_t max_size)
  56. {
  57. this->max_size = max_size;
  58. base = new T[max_size];
  59. borders = encode2(0, 0);
  60. #pragma omp flush
  61. }
  62. /** @brief Destructor. Not to be called concurrent, of course. */
  63. ~RestrictedBoundedConcurrentQueue()
  64. { delete[] 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 T& t)
  69. {
  70. lcas_t former_borders = borders;
  71. int former_front, former_back;
  72. decode2(former_borders, former_front, former_back);
  73. *(base + former_front % max_size) = t;
  74. #if _GLIBCXX_ASSERTIONS
  75. // Otherwise: front - back > max_size eventually.
  76. _GLIBCXX_PARALLEL_ASSERT(((former_front + 1) - former_back)
  77. <= max_size);
  78. #endif
  79. fetch_and_add(&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(T& t)
  85. {
  86. int former_front, former_back;
  87. #pragma omp flush
  88. decode2(borders, former_front, former_back);
  89. while (former_front > former_back)
  90. {
  91. // Chance.
  92. lcas_t former_borders = encode2(former_front, former_back);
  93. lcas_t new_borders = encode2(former_front - 1, former_back);
  94. if (compare_and_swap(&borders, former_borders, new_borders))
  95. {
  96. t = *(base + (former_front - 1) % max_size);
  97. return true;
  98. }
  99. #pragma omp flush
  100. decode2(borders, former_front, former_back);
  101. }
  102. return false;
  103. }
  104. /** @brief Pops one element from the queue at the front end.
  105. * Must not be called concurrently with pop_front(). */
  106. bool
  107. pop_back(T& t) //queue behavior
  108. {
  109. int former_front, former_back;
  110. #pragma omp flush
  111. decode2(borders, former_front, former_back);
  112. while (former_front > former_back)
  113. {
  114. // Chance.
  115. lcas_t former_borders = encode2(former_front, former_back);
  116. lcas_t new_borders = encode2(former_front, former_back + 1);
  117. if (compare_and_swap(&borders, former_borders, new_borders))
  118. {
  119. t = *(base + former_back % max_size);
  120. return true;
  121. }
  122. #pragma omp flush
  123. decode2(borders, former_front, former_back);
  124. }
  125. return false;
  126. }
  127. };
  128. } //namespace __gnu_parallel
  129. #undef _GLIBCXX_VOLATILE
  130. #endif /* _GLIBCXX_PARALLEL_QUEUE_H */