-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b98c7ab
commit a935aa0
Showing
3 changed files
with
177 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2020 Roc Streaming authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include "roc_core/seqlock_impl.h" | ||
|
||
#include "roc_core/atomic_ops.h" | ||
#include "roc_core/cpu_instructions.h" | ||
|
||
namespace roc { | ||
namespace core { | ||
|
||
SeqlockImpl::SeqlockImpl(void* val, size_t val_size) | ||
: val_(val) | ||
, val_size_(val_size) | ||
, ver_(0) { | ||
} | ||
|
||
seqlock_version_t SeqlockImpl::version() const { | ||
return AtomicOps::load_seq_cst(ver_); | ||
} | ||
|
||
bool SeqlockImpl::try_store(const void* value, seqlock_version_t& ver) { | ||
seqlock_version_t ver0 = AtomicOps::load_relaxed(ver_); | ||
if (ver0 & 1) { | ||
return false; | ||
} | ||
|
||
if (!AtomicOps::compare_exchange_relaxed(ver_, ver0, ver0 + 1)) { | ||
return false; | ||
} | ||
AtomicOps::fence_release(); | ||
|
||
volatile_copy_(val_, value, val_size_); | ||
AtomicOps::fence_seq_cst(); | ||
|
||
ver = ver0 + 2; | ||
AtomicOps::store_relaxed(ver_, ver); | ||
|
||
return true; | ||
} | ||
|
||
void SeqlockImpl::exclusive_store(const void* value, seqlock_version_t& ver) { | ||
const seqlock_version_t ver0 = AtomicOps::load_relaxed(ver_); | ||
AtomicOps::store_relaxed(ver_, ver0 + 1); | ||
AtomicOps::fence_release(); | ||
|
||
volatile_copy_(val_, value, val_size_); | ||
AtomicOps::fence_seq_cst(); | ||
|
||
ver = ver0 + 2; | ||
AtomicOps::store_relaxed(ver_, ver); | ||
} | ||
|
||
// If the concurrent store is running and is not sleeping, retrying 3 times | ||
// should be enough to succeed. This may fail if the concurrent store was | ||
// preempted in the middle, of if there are multiple concurrent stores. | ||
bool SeqlockImpl::try_load_repeat(void* value, seqlock_version_t& ver) const { | ||
if (try_load_(value, ver)) { | ||
return true; | ||
} | ||
if (try_load_(value, ver)) { | ||
return true; | ||
} | ||
if (try_load_(value, ver)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void SeqlockImpl::wait_load(void* value, seqlock_version_t& ver) const { | ||
while (!try_load_(value, ver)) { | ||
cpu_relax(); | ||
} | ||
} | ||
|
||
// We use hand-rolled loop instead of memcpy() or default (trivial) copy constructor | ||
// to be sure that the copying will be covered by our memory fences. On some | ||
// platforms, memcpy() and copy constructor may be implemented using streaming | ||
// instructions which may ignore memory fences. | ||
void SeqlockImpl::volatile_copy_(volatile void* dst, | ||
const volatile void* src, | ||
size_t val_size) { | ||
volatile char* dst_ptr = reinterpret_cast<volatile char*>(&dst); | ||
const volatile char* src_ptr = reinterpret_cast<const volatile char*>(&src); | ||
|
||
for (size_t n = 0; n < val_size; n++) { | ||
dst_ptr[n] = src_ptr[n]; | ||
} | ||
} | ||
|
||
bool SeqlockImpl::try_load_(void* value, seqlock_version_t& ver) const { | ||
const seqlock_version_t ver0 = AtomicOps::load_relaxed(ver_); | ||
AtomicOps::fence_seq_cst(); | ||
|
||
volatile_copy_(value, val_, val_size_); | ||
AtomicOps::fence_acquire(); | ||
|
||
ver = AtomicOps::load_relaxed(ver_); | ||
return ((ver0 & 1) == 0 && ver0 == ver); | ||
} | ||
|
||
} // namespace core | ||
} // namespace roc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2020 Roc Streaming authors | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
//! @file roc_core/seqlock_impl.h | ||
//! @brief Seqlock implementation. | ||
|
||
#ifndef ROC_CORE_SEQLOCK_IMPL_H_ | ||
#define ROC_CORE_SEQLOCK_IMPL_H_ | ||
|
||
#include "roc_core/stddefs.h" | ||
|
||
namespace roc { | ||
namespace core { | ||
|
||
typedef uint32_t seqlock_version_t; | ||
|
||
//! Seqlock implementation. | ||
class SeqlockImpl { | ||
public: | ||
//! Initialize. | ||
SeqlockImpl(void* val, size_t val_size); | ||
|
||
//! Load value version. | ||
seqlock_version_t version() const; | ||
|
||
//! Try to store value. | ||
bool try_store(const void* value, seqlock_version_t& ver); | ||
|
||
//! Store value. | ||
void exclusive_store(const void* value, seqlock_version_t& ver); | ||
|
||
//! Try to load value and version. | ||
bool try_load_repeat(void* value, seqlock_version_t& ver) const; | ||
|
||
//! Load value and version. | ||
void wait_load(void* value, seqlock_version_t& ver) const; | ||
|
||
private: | ||
static void | ||
volatile_copy_(volatile void* dst, const volatile void* src, size_t val_size); | ||
|
||
bool try_load_(void* value, seqlock_version_t& ver) const; | ||
|
||
void* val_; | ||
size_t val_size_; | ||
seqlock_version_t ver_; | ||
}; | ||
|
||
} // namespace core | ||
} // namespace roc | ||
|
||
#endif // ROC_CORE_SEQLOCK_IMPL_H_ |