SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
reader_writer_manager.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <cassert>
13#include <functional>
14#include <mutex>
15#include <seqan3/std/new>
16
20
21namespace seqan3::detail
22{
23
27struct writer_count : public detail::strong_type<size_t, writer_count>
28{
31
33 using base_t::base_t;
34};
35
39struct reader_count : public detail::strong_type<size_t, reader_count>
40{
43
45 using base_t::base_t;
46};
47
70{
71private:
73 class [[nodiscard]] scoped_writer_type
74 {
75 public:
80 scoped_writer_type() = delete;
85
89 explicit scoped_writer_type(reader_writer_manager & _manager) : manager{_manager}
90 {}
91
94 {
95 manager.writer_arrive();
96 }
98
101 };
102
104 class [[nodiscard]] scoped_reader_type
105 {
106 public:
115
119 explicit scoped_reader_type(reader_writer_manager & _manager) : manager{_manager}
120 {}
121
124 {
125 manager.reader_arrive();
126 }
128
131 };
132
133public:
143
160 template <typename concurrent_t>
161 requires requires { std::declval<concurrent_t>().close(); } // requires closable concurrent data structure.
162 reader_writer_manager(reader_count const rcount, writer_count const wcount, concurrent_t & ds) :
163 reader_latch{static_cast<ptrdiff_t>(rcount.get())},
164 writer_latch{static_cast<ptrdiff_t>(wcount.get())},
165 completion_fn{[&ds]()
166 {
167 ds.close();
168 }}
169 {
170 if (rcount.get() < 1 || wcount.get() < 1)
171 throw std::invalid_argument{"Both, reader count and writer count must be at least 1."};
172 }
174
192 {
193 writer_latch.arrive_and_wait();
194
195 std::call_once(flag, completion_fn);
196 }
197
214 void writer_arrive() noexcept
215 {
216 writer_latch.arrive();
217
218 if (writer_latch.try_wait())
219 std::call_once(flag, completion_fn);
220 }
221
238 {
239 reader_latch.arrive_and_wait();
240 }
241
256 void reader_arrive() noexcept
257 {
258 reader_latch.arrive();
259 }
260
279 {
280 return scoped_writer_type{*this};
281 }
282
301 {
302 return scoped_reader_type{*this};
303 }
304
305private:
314};
315
316} // namespace seqan3::detail
T call_once(T... args)
A single-use synchronisation point to coordinate concurrent threads.
Definition latch.hpp:36
A strictly scope-based seqan3::detail::reader_writer_manager wrapper for consumer threads.
Definition reader_writer_manager.hpp:105
scoped_reader_type & operator=(scoped_reader_type &&)=delete
Deleted. Class holds a reference.
~scoped_reader_type()
Calls reader_arrive on the wrapped latch and destructs.
Definition reader_writer_manager.hpp:123
scoped_reader_type(reader_writer_manager &_manager)
Constructs the scoped reader with the associated manager.
Definition reader_writer_manager.hpp:119
scoped_reader_type & operator=(scoped_reader_type const &)=delete
Deleted. Class holds a reference.
scoped_reader_type(scoped_reader_type const &)=default
Deleted.
scoped_reader_type(scoped_reader_type &&)=default
Defaulted.
reader_writer_manager & manager
The wrapped latch.
Definition reader_writer_manager.hpp:130
A strictly scope-based seqan3::detail::reader_writer_manager wrapper for producer threads.
Definition reader_writer_manager.hpp:74
~scoped_writer_type()
Calls writer_arrive on the wrapped latch and destructs.
Definition reader_writer_manager.hpp:93
reader_writer_manager & manager
The wrapped latch.
Definition reader_writer_manager.hpp:100
scoped_writer_type & operator=(scoped_writer_type &&)=delete
Deleted. Class holds a reference.
scoped_writer_type & operator=(scoped_writer_type const &)=delete
Deleted. Class holds a reference.
scoped_writer_type(reader_writer_manager &_manager)
Constructs the scoped writer with the associated manager.
Definition reader_writer_manager.hpp:89
scoped_writer_type(scoped_writer_type &&)=default
Defaulted.
scoped_writer_type(scoped_writer_type const &)=default
Deleted.
A single-use synchronisation point for closable concurrent data structures.
Definition reader_writer_manager.hpp:70
scoped_writer_type register_writer() noexcept
Registers the current thread as a producer thread for the monitored resource.
Definition reader_writer_manager.hpp:278
reader_writer_manager(reader_count const rcount, writer_count const wcount, concurrent_t &ds)
Constructs the reader_writer_manager with the reader count, writer count and the associated data stru...
Definition reader_writer_manager.hpp:162
std::function< void()> completion_fn
The stored completion function.
Definition reader_writer_manager.hpp:313
void reader_arrive_and_wait() noexcept
Atomically decrements writer counter by one and blocks the calling thread.
Definition reader_writer_manager.hpp:237
latch writer_latch
The internal latch for producer threads.
Definition reader_writer_manager.hpp:309
reader_writer_manager(reader_writer_manager const &)=delete
Deleted.
~reader_writer_manager()=default
Defaulted.
void writer_arrive() noexcept
Atomically decrements writer counter by one.
Definition reader_writer_manager.hpp:214
latch reader_latch
The internal latch for consumer threads.
Definition reader_writer_manager.hpp:307
reader_writer_manager(reader_writer_manager &&)=delete
Deleted.
void reader_arrive() noexcept
Atomically decrements reader counter by one.
Definition reader_writer_manager.hpp:256
void writer_arrive_and_wait() noexcept
Atomically decrements writer counter by one and blocks the calling thread.
Definition reader_writer_manager.hpp:191
reader_writer_manager & operator=(reader_writer_manager &&)=delete
Deleted.
scoped_reader_type register_reader() noexcept
Registers the current thread as a consumer thread for the monitored resource.
Definition reader_writer_manager.hpp:300
reader_writer_manager & operator=(reader_writer_manager const &)=delete
Deleted.
CRTP base class to declare a strong typedef for a regular type to avoid ambiguous parameter settings ...
Definition strong_type.hpp:174
@ flag
The alignment flag (bit information), uint16_t value.
constexpr std::size_t hardware_destructive_interference_size
Minimum offset between two objects to avoid false sharing.
Definition new:54
Provides seqan3::detail::latch.
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
constexpr auto const & get(configuration< configs_t... > const &config) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition configuration.hpp:412
The <new> header from C++17's standard library.
Provides seqan3::detail::spin_delay.
Provides basic data structure for strong types.
A strong type to set the reader count of a seqan3::detail::reader_writer_manager.
Definition reader_writer_manager.hpp:40
A strong type to set the writer count of a seqan3::detail::reader_writer_manager.
Definition reader_writer_manager.hpp:28
Hide me