wait_free_spsc_queue.h 6.39 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_CONTAINERS_WAIT_FREE_SPSC_QUEUE_H_
#define EMBB_CONTAINERS_WAIT_FREE_SPSC_QUEUE_H_

#include <embb/base/atomic.h>

#include <iostream>
#include <stdexcept>
#include <stdlib.h>
#include <vector>
/**
 * \defgroup CPP_CONCEPTS_QUEUE Queue Concept
 * Concept for thread-safe queues
 *
 * \ingroup CPP_CONCEPT
 * \{
 * \par Description
 * A queue is an abstract data type holding a collection of elements of some
 * predetermined type. A queue provides two operations: \c TryEnqueue and
 * \c TryDequeue. \c TryEnqueue tries to add an element to the collection, and
 * \c TryDequeue tries to remove an element from the collection. A queue has
 * per-thread FIFO (First-In, First-out) semantics, i.e., if one thread enqueues
 * two elements and another thread dequeues these elements, then they appear in
 * the same order. The capacity \c cap of a queue defines the number of elements
 * it can store (depending on the implementation, a queue might store more than
 * \c cap elements, since for thread-safe memory management, more memory than
 * necessary for holding \c cap elements has to be provided).
 *
 * \par Requirements
 * - Let \c Queue be the queue class
56
 * - Let \c Type be the element type of the queue
57
 * - Let \c capacity be a value of type \c size_t
58
 * - Let \c element be a reference to an element of type \c Type
59 60 61 62 63 64 65 66 67
 *
 * \par Valid Expressions
 * <table>
 *   <tr>
 *     <th>Expression</th>
 *     <th>Return type</th>
 *     <th>Description</th>
 *   </tr>
 *   <tr>
68
 *     <td>\code{.cpp} Queue<Type>(capacity) \endcode</td>
69 70
 *     <td>Nothing</td>
 *     <td>
71
 *      Constructs a queue with minimal capacity \c capacity that holds elements of
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
 *      type \c T.
 *     </td>
 *   </tr>
 *   <tr>
 *     <td>\code{.cpp} TryEnqueue(element) \endcode</td>
 *     <td>\code{.cpp} bool \endcode</td>
 *     <td>
 *      Tries to enqueue \c element into the queue. Returns \c false if the
 *      queue is full, otherwise \c true.
 *     </td>
 *   </tr>
 *   <tr>
 *     <td>\code{.cpp} TryDequeue(element) \endcode</td>
 *     <td>\code{.cpp} bool \endcode</td>
 *     <td>
 *      Tries to dequeue an element from the queue. Returns \c false if the
 *      queue is empty, otherwise \c true. In the latter case, the dequeued
 *      element is stored in \c element which must be passed by reference.
 *     </td>
 *   </tr>
 * </table>
 *
 * \}
 */

/**
 * \defgroup CPP_CONTAINERS_QUEUES Queues
 * Concurrent queues
 *
 * \see CPP_CONCEPTS_QUEUE
 *
 * \ingroup CPP_CONTAINERS
 *
 */
namespace embb {
namespace containers {
/**
 * Wait-free queue for a single producer and a single consumer.
 *
 * \concept{CPP_CONCEPTS_QUEUE}
 *
 * \ingroup CPP_CONTAINERS_QUEUES
 *
 * \see LockFreeMPMCQueue
 *
117
 * \tparam Type Type of the queue elements
118 119
 * \tparam Allocator Allocator type for allocating queue elements.
 */
120
template<typename Type, class Allocator = embb::base::Allocator< Type > >
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
class WaitFreeSPSCQueue {
 private:
  /**
   * Allocator for allocating queue elements
   */
  Allocator allocator;

  /**
   * Capacity of the queue
   */
  size_t capacity;

  /**
   * Array holding the queue elements
   */
136
  Type* queue_array;
137 138 139 140 141 142 143 144 145 146 147

  /**
   * Index of the head in the \c queue_array
   */
  embb::base::Atomic<size_t> head_index;

  /**
   * Index of the tail in the \c queue_array
   */
  embb::base::Atomic<size_t> tail_index;

unknown committed
148 149 150 151 152
  /**
   * Align capacity to the next smallest power of two
   */
  static size_t AlignCapacityToPowerOfTwo(size_t capacity);

153 154
 public:
  /**
unknown committed
155
   * Creates a queue with at least the specified capacity.
156
   *
Tobias Fuchs committed
157
   * \memory Allocates \c 2^k elements of type \c Type, where \c k is the
unknown committed
158
   * smallest number such that <tt>capacity <= 2^k</tt> holds.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
   *
   * \notthreadsafe
   *
   * \see CPP_CONCEPTS_QUEUE
   */
  WaitFreeSPSCQueue(
    size_t capacity
    /**< [IN] Capacity of the queue */
  );

  /**
   * Destroys the queue.
   *
   * \notthreadsafe
   */
  ~WaitFreeSPSCQueue();

  /**
   * Returns the capacity of the queue.
   *
   * \return Number of elements the queue can hold.
   *
   * \waitfree
   */
  size_t GetCapacity();

  /**
   * Tries to enqueue an element into the queue.
   *
   * \return \c true if the element could be enqueued, \c false if the queue is
   * full.
   *
   * \waitfree
   *
   * \note Concurrently enqueueing elements by multiple producers leads to
   * undefined behavior.
   *
   * \see CPP_CONCEPTS_QUEUE
   */
  bool TryEnqueue(
199
    Type const & element
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    /**< [IN] Const reference to the element that shall be enqueued */
  );

  /**
   * Tries to dequeue an element from the queue.
   *
   * \return \c true if an element could be dequeued, \c false if the queue is
   * empty.
   *
   * \waitfree
   *
   * \note Concurrently dequeueing elements by multiple consumers leads to
   * undefined behavior.
   *
   * \see CPP_CONCEPTS_QUEUE
   */
  bool TryDequeue(
217 218 219
    Type & element
    /**< [IN,OUT] Reference to the dequeued element. Unchanged, if the
                  operation was not successful. */
220 221 222 223 224 225 226 227
  );
};
} // namespace containers
} // namespace embb

#include<embb/containers/internal/wait_free_spsc_queue-inl.h>

#endif  // EMBB_CONTAINERS_WAIT_FREE_SPSC_QUEUE_H_