Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make read_critical_section::lock mutable in debug builds #645

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions olc_art.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2024 Laurynas Biveinis
// Copyright 2019-2025 UnoDB contributors

Check notice on line 1 in olc_art.cpp

View check run for this annotation

codefactor.io / CodeFactor

olc_art.cpp#L1

olc_art.cpp should include its header file olc_art.HPP. (build/include)

Check notice on line 1 in olc_art.cpp

View check run for this annotation

codefactor.io / CodeFactor

olc_art.cpp#L1

olc_art.cpp should include its header file olc_art.hpp. (build/include)

//
// CAUTION: [global.hpp] MUST BE THE FIRST INCLUDE IN ALL SOURCE AND
Expand Down Expand Up @@ -94,9 +94,10 @@
//
// @return the outcome of that computation and false if the
// read_critical_section could not be unlocked.
inline bool UNLOCK(unodb::optimistic_lock::read_critical_section &cs,
bool ret) {
return (!cs.try_read_unlock()) ? false : ret;
[[nodiscard]] inline bool unlock_and_return(
const unodb::optimistic_lock::read_critical_section &cs,
bool ret) noexcept {
return UNODB_DETAIL_LIKELY(cs.try_read_unlock()) ? ret : false;
}

} // namespace
Expand Down Expand Up @@ -1718,12 +1719,13 @@
if (cmp_ < 0) {
// FWD and the search key is ordered before this node. We
// want the left-most leaf under the node.
return UNLOCK(node_critical_section,
try_left_most_traversal(node, parent_critical_section));
return unlock_and_return(
node_critical_section,
try_left_most_traversal(node, parent_critical_section));
}
// FWD and the search key is ordered after this node. Right
// most descent and then next().
return UNLOCK(
return unlock_and_return(
node_critical_section,
try_right_most_traversal(node, parent_critical_section)) &&
try_next();
Expand All @@ -1732,13 +1734,15 @@
if (cmp_ < 0) {
// REV and the search key is ordered before this node. We
// want the preceeding key.
return UNLOCK(node_critical_section,
try_left_most_traversal(node, parent_critical_section)) &&
return unlock_and_return(
node_critical_section,
try_left_most_traversal(node, parent_critical_section)) &&
try_prior();
}
// REV and the search key is ordered after this node.
return UNLOCK(node_critical_section,
try_right_most_traversal(node, parent_critical_section));
return unlock_and_return(
node_critical_section,
try_right_most_traversal(node, parent_critical_section));
}
remaining_key.shift_right(key_prefix_length);
auto res = inode->find_child(node_type, remaining_key[0]);
Expand Down
12 changes: 8 additions & 4 deletions optimistic_lock.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2019-2023 Laurynas Biveinis
// Copyright (C) 2019-2025 UnoDB contributors
#ifndef UNODB_DETAIL_OPTIMISTIC_LOCK_HPP
#define UNODB_DETAIL_OPTIMISTIC_LOCK_HPP

Expand Down Expand Up @@ -242,7 +242,7 @@ class [[nodiscard]] optimistic_lock final {
// the version that was used to construct this
// read_critical_section.
[[nodiscard, gnu::flatten]] UNODB_DETAIL_FORCE_INLINE bool try_read_unlock()
UNODB_DETAIL_RELEASE_CONST noexcept {
const noexcept {
const auto result = lock->try_read_unlock(version);
#ifndef NDEBUG
lock = nullptr;
Expand All @@ -269,7 +269,7 @@ class [[nodiscard]] optimistic_lock final {
//
// @return true if the version is unchanged and false if the
// caller MUST restart because the version has been changed.
[[nodiscard]] bool check() UNODB_DETAIL_RELEASE_CONST noexcept {
[[nodiscard]] bool check() const noexcept {
const auto result = lock->check(version);
#ifndef NDEBUG
if (UNODB_DETAIL_UNLIKELY(!result)) lock = nullptr; // LCOV_EXCL_LINE
Expand Down Expand Up @@ -307,7 +307,11 @@ class [[nodiscard]] optimistic_lock final {
read_critical_section &operator=(const read_critical_section &) = delete;

private:
optimistic_lock *lock{nullptr};
#ifndef NDEBUG
mutable
#endif
optimistic_lock *lock{nullptr};

version_type version{0};

friend class write_guard;
Expand Down
Loading