tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_MultipleWriterSeqLock.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11#pragma once
12
13#include "../../3rd_party/crill/seqlock_object.h"
14#include "../../tracktion_graph/utilities/tracktion_RealTimeSpinLock.h"
15
16namespace tracktion { inline namespace core
17{
18
22template<typename T>
24{
25public:
26 // Creates a seqlock_object with a default-constructed value.
27 MultipleWriterSeqLock() = default;
28
29 // Creates a seqlock_object with the given value.
31 {
32 store(t);
33 }
34
35 // Reads and returns the current value.
36 // Non-blocking guarantees: wait-free if there are no concurrent writes,
37 // otherwise none.
38 T load() const noexcept
39 {
40 return object.load();
41 }
42
43 // Attempts to read the current value and write it into the passed-in object.
44 // Returns: true if the read succeeded, false otherwise.
45 // Non-blocking guarantees: wait-free.
46 bool try_load(T& t) const noexcept
47 {
48 return object.try_load (t);
49 }
50
51 // Updates the current value to the value passed in.
52 // Non-blocking guarantees: wait-free for a single writer, blocking otherwise
53 void store(T t) noexcept
54 {
55 std::unique_lock lock (mutex);
56 object.store (t);
57 }
58
59private:
60 RealTimeSpinLock mutex;
61 crill::seqlock_object<T> object;
62};
63
64}} // namespace tracktion { inline namespace core
Wraps a seqlock to allow a thread-safe object with wait-free reads with respect to each other.
A basic spin lock that uses an atomic_flag to store the locked state so should never result in a syst...