Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
seqlock_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/seqlock_impl.h
10 //! @brief Seqlock implementation.
11 
12 #ifndef ROC_CORE_SEQLOCK_IMPL_H_
13 #define ROC_CORE_SEQLOCK_IMPL_H_
14 
15 #include "roc_core/stddefs.h"
16 
17 namespace roc {
18 namespace core {
19 
20 //! Type for holding seqlock value version.
21 //! Version is changed each value update.
22 //! May wrap.
23 typedef uint32_t seqlock_version_t;
24 
25 //! Check if this is a valid seqlock version.
26 //! Returns false if seqlock version corresponds to intermediate state
27 //! that should never be seen by seqlock user.
29  return (ver & 1) == 0;
30 }
31 
32 //! Seqlock implementation class.
33 //! @see Seqlock.
34 class SeqlockImpl {
35 public:
36  //! Initialize.
38 
39  //! Load value version.
41 
42  //! Try to store value.
44  void* current_value,
45  size_t value_size,
46  const void* new_value);
47 
48  //! Store value.
50  void* current_value,
51  size_t value_size,
52  const void* new_value);
53 
54  //! Try to load value and version.
56  const void* current_value,
57  size_t value_size,
58  void* return_value) const;
59 
60  //! Load value and version.
62  const void* current_value,
63  size_t value_size,
64  void* return_value) const;
65 
66 private:
67  bool try_load_(seqlock_version_t& ver,
68  const void* current_value,
69  size_t value_size,
70  void* return_value) const;
71 
72  seqlock_version_t ver_;
73 };
74 
75 } // namespace core
76 } // namespace roc
77 
78 #endif // ROC_CORE_SEQLOCK_IMPL_H_
Seqlock implementation class.
Definition: seqlock_impl.h:34
SeqlockImpl()
Initialize.
bool try_store(seqlock_version_t &ver, void *current_value, size_t value_size, const void *new_value)
Try to store value.
void exclusive_store(seqlock_version_t &ver, void *current_value, size_t value_size, const void *new_value)
Store value.
bool try_load_repeat(seqlock_version_t &ver, const void *current_value, size_t value_size, void *return_value) const
Try to load value and version.
void wait_load(seqlock_version_t &ver, const void *current_value, size_t value_size, void *return_value) const
Load value and version.
seqlock_version_t version() const
Load value version.
bool seqlock_version_is_valid(seqlock_version_t ver)
Check if this is a valid seqlock version. Returns false if seqlock version corresponds to intermediat...
Definition: seqlock_impl.h:28
uint32_t seqlock_version_t
Type for holding seqlock value version. Version is changed each value update. May wrap.
Definition: seqlock_impl.h:23
Root namespace.
Commonly used types and functions.