Embedded Multicore Building Blocks V1.0.0
lock_free_mpmc_queue.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_CONTAINERS_LOCK_FREE_MPMC_QUEUE_H_
28 #define EMBB_CONTAINERS_LOCK_FREE_MPMC_QUEUE_H_
29 
30 #include <embb/base/atomic.h>
31 #include <embb/base/function.h>
32 
33 #include <embb/containers/lock_free_tree_value_pool.h>
34 #include <embb/containers/object_pool.h>
35 #include <embb/containers/internal/hazard_pointer.h>
36 
37 #include <limits>
38 #include <stdexcept>
39 
40 namespace embb {
41 namespace containers {
42 namespace internal {
51  template< typename Type >
52 class LockFreeMPMCQueueNode {
53  private:
58 
62  Type element;
63 
64  public:
70  LockFreeMPMCQueueNode();
71 
75  LockFreeMPMCQueueNode(
76  Type const& element);
78 
85 
89  Type GetElement();
90 };
91 } // namespace internal
92 
106 template< typename Type,
108 >
110  private:
115  size_t capacity;
116 
127 
133  delete_pointer_callback;
134 
138  typedef embb::containers::internal::HazardPointer
139  < internal::LockFreeMPMCQueueNode<Type>* >
140  MPMCQueueNodeHazardPointer_t;
141 
145  MPMCQueueNodeHazardPointer_t hazardPointer;
146 
147 
152 
157 
162  void DeletePointerCallback(internal::LockFreeMPMCQueueNode<Type>* to_delete);
163 
164  public:
179  size_t capacity);
181 
188 
196  size_t GetCapacity();
197 
211  bool TryEnqueue(
212  Type const& element);
214 
225  bool TryDequeue(
226  Type & element);
230 };
231 } // namespace containers
232 } // namespace embb
233 
234 #include <embb/containers/internal/lock_free_mpmc_queue-inl.h>
235 
236 #endif // EMBB_CONTAINERS_LOCK_FREE_MPMC_QUEUE_H_
Definition: lock_free_mpmc_queue.h:40
Lock-free queue for multiple producers and multiple consumers.
Definition: lock_free_mpmc_queue.h:109
Class representing atomic variables.
Definition: atomic.h:60
Lock-free value pool using binary tree construction.
Definition: lock_free_tree_value_pool.h:57
Pool for thread-safe management of arbitrary objects.
Definition: object_pool.h:59
Wraps function pointers, member function pointers, and functors with up to five arguments.
Definition: function.h:94