Library of Assembled Shared Sources
lock_free_spmc_ring_buffer.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2022 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#pragma once
44
45#include "stde_common.h"
47#include "../util/atomic.h"
48
49namespace lass
50{
51namespace stde
52{
53namespace impl
54{
55
56class lock_free_spmc_ring_buffer_base: util::NonCopyable
57{
58public:
59
60 bool empty() const;
61 bool full() const;
62
63protected:
64
65 lock_free_spmc_ring_buffer_base(size_t capacity);
66 ~lock_free_spmc_ring_buffer_base() = default;
67
68 typedef num::Tuint32 index_type;
69
70 struct alignas(8) tagged_index_t
71 {
72 tagged_index_t() noexcept {}
73 tagged_index_t(index_type index, index_type tag) : index(index), tag(tag) {}
74 index_type index = 0;
75 index_type tag = 0;
76
77 static bool empty(tagged_index_t head, tagged_index_t tail) { return head.index == tail.index && head.tag == tail.tag; }
78 static bool full(tagged_index_t head, tagged_index_t tail) { return head.index == tail.index && head.tag != tail.tag; }
79 };
80
81
82 tagged_index_t next_index(tagged_index_t index) const;
83
84 alignas(LASS_LOCK_FREE_ALIGNMENT) std::atomic<tagged_index_t> head_;
85 alignas(LASS_LOCK_FREE_ALIGNMENT) std::atomic<tagged_index_t> tail_;
86 const index_type ring_size_;
87
88private:
89
90 static_assert(sizeof(tagged_index_t) == 8, "tagged_index_t should be 8-bytes");
91
92 static size_t enforce_valid_size(size_t size);
93
94};
95
96
97
98template<typename T>
99class lock_free_spmc_value_ring_buffer: private lock_free_spmc_ring_buffer_base
100{
101public:
102 typedef T value_type;
103
104 lock_free_spmc_value_ring_buffer(size_t capacity);
105 ~lock_free_spmc_value_ring_buffer() = default;
106
107 bool try_push(const value_type& x);
108 bool try_push(value_type&& x);
109 template <class... Args> bool try_emplace(Args&&... args);
110 bool try_pop(value_type& x);
111
112 using lock_free_spmc_ring_buffer_base::empty;
113 using lock_free_spmc_ring_buffer_base::full;
114
115private:
116
117 std::vector< std::atomic<value_type> > ring_;
118};
119
120
121template
122<
123 typename T,
124 typename FixedAllocator = util::AllocatorConcurrentFreeList<>
125>
126class lock_free_spmc_object_ring_buffer: private lock_free_spmc_ring_buffer_base
127{
128public:
129 typedef T value_type;
130
131 lock_free_spmc_object_ring_buffer(size_t capacity);
132 ~lock_free_spmc_object_ring_buffer();
133
134 bool try_push(const value_type& x);
135 bool try_push(value_type&& x);
136 template <class... Args> bool try_emplace(Args&&... args);
137 bool try_pop(value_type& x);
138
139 using lock_free_spmc_ring_buffer_base::empty;
140 using lock_free_spmc_ring_buffer_base::full;
141
142private:
143
144 FixedAllocator value_allocator_;
145 std::vector< std::atomic<T*> > ring_;
146};
147
148}
149
150template<typename T>
151using lock_free_spmc_ring_buffer = typename std::conditional<
152 std::is_trivially_copy_constructible<T>::value,
153 impl::lock_free_spmc_value_ring_buffer<T>,
154 impl::lock_free_spmc_object_ring_buffer<T>
155 >::type;
156
157}
158
159}
160
162
163// EOF
lass extensions to the standard library
Library for Assembled Shared Sources.
Definition config.h:53