3 // Copyright (C) 2007, 2008, 2009 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/queue.h
26 * @brief Lock-free double-ended queue.
27 * This file is a GNU parallel extension to the Standard C++ Library.
30 // Written by Johannes Singler.
32 #ifndef _GLIBCXX_PARALLEL_QUEUE_H
33 #define _GLIBCXX_PARALLEL_QUEUE_H 1
35 #include <parallel/types.h>
36 #include <parallel/base.h>
37 #include <parallel/compatibility.h>
39 /** @brief Decide whether to declare certain variable volatile in this file. */
40 #define _GLIBCXX_VOLATILE volatile
42 namespace __gnu_parallel
44 /**@brief Double-ended queue of bounded size, allowing lock-free
45 * atomic access. push_front() and pop_front() must not be called
46 * concurrently to each other, while pop_back() can be called
47 * concurrently at all times.
48 * @c empty(), @c size(), and @c top() are intentionally not provided.
49 * Calling them would not make sense in a concurrent setting.
50 * @param T Contained element type. */
52 class RestrictedBoundedConcurrentQueue
55 /** @brief Array of elements, seen as cyclic buffer. */
58 /** @brief Maximal number of elements contained at the same time. */
59 sequence_index_t max_size;
61 /** @brief Cyclic begin and end pointers contained in one
62 atomically changeable value. */
63 _GLIBCXX_VOLATILE lcas_t borders;
66 /** @brief Constructor. Not to be called concurrent, of course.
67 * @param max_size Maximal number of elements to be contained. */
68 RestrictedBoundedConcurrentQueue(sequence_index_t max_size)
70 this->max_size = max_size;
71 base = new T[max_size];
72 borders = encode2(0, 0);
76 /** @brief Destructor. Not to be called concurrent, of course. */
77 ~RestrictedBoundedConcurrentQueue()
80 /** @brief Pushes one element into the queue at the front end.
81 * Must not be called concurrently with pop_front(). */
83 push_front(const T& t)
85 lcas_t former_borders = borders;
86 int former_front, former_back;
87 decode2(former_borders, former_front, former_back);
88 *(base + former_front % max_size) = t;
89 #if _GLIBCXX_ASSERTIONS
90 // Otherwise: front - back > max_size eventually.
91 _GLIBCXX_PARALLEL_ASSERT(((former_front + 1) - former_back)
94 fetch_and_add(&borders, encode2(1, 0));
97 /** @brief Pops one element from the queue at the front end.
98 * Must not be called concurrently with pop_front(). */
102 int former_front, former_back;
104 decode2(borders, former_front, former_back);
105 while (former_front > former_back)
108 lcas_t former_borders = encode2(former_front, former_back);
109 lcas_t new_borders = encode2(former_front - 1, former_back);
110 if (compare_and_swap(&borders, former_borders, new_borders))
112 t = *(base + (former_front - 1) % max_size);
116 decode2(borders, former_front, former_back);
121 /** @brief Pops one element from the queue at the front end.
122 * Must not be called concurrently with pop_front(). */
124 pop_back(T& t) //queue behavior
126 int former_front, former_back;
128 decode2(borders, former_front, former_back);
129 while (former_front > former_back)
132 lcas_t former_borders = encode2(former_front, former_back);
133 lcas_t new_borders = encode2(former_front, former_back + 1);
134 if (compare_and_swap(&borders, former_borders, new_borders))
136 t = *(base + former_back % max_size);
140 decode2(borders, former_front, former_back);
145 } //namespace __gnu_parallel
147 #undef _GLIBCXX_VOLATILE
149 #endif /* _GLIBCXX_PARALLEL_QUEUE_H */