This is an automated email from the ASF dual-hosted git repository.
alexey pushed a commit to branch branch-1.18.x
in repository https://gitbox.apache.org/repos/asf/kudu.git
The following commit(s) were added to refs/heads/branch-1.18.x by this push:
new 64e75b22c [util] use std::atomic in HighWaterMark
64e75b22c is described below
commit 64e75b22c796fa99c2df8df3bbf5db4a92d6a319
Author: Alexey Serbin <[email protected]>
AuthorDate: Fri Mar 14 12:06:52 2025 -0700
[util] use std::atomic in HighWaterMark
This patch replaces chromium-based Atomics with std::atomic in the
implementation of HighWaterMark class. This is a part of the on-going
work to replace chromium-derived atomics with STL-based ones.
While TryIncrementBy() provides 'a strict limit' semantics as it should,
the rest of the read-style methods in HightWaterMark API adhere to using
the std::memory_order_relaxed ordering, similar to the original version.
That's because all the use cases of such methods involve TOCTOU
conditions anyway. With that, it wouldn't be a good idea switching to
more strict ordering for read operations (i.e. std::memory_order_acquire
for load) and incur a performance hit without a clear benefit in return.
Change-Id: I17e4d4cf308c06b0dd135a99e793067980d5ad20
Reviewed-on: http://gerrit.cloudera.org:8080/22629
Tested-by: Alexey Serbin <[email protected]>
Reviewed-by: Abhishek Chennaka <[email protected]>
(cherry picked from commit b1a4f5b4deec357159ac99477ac3479c005c3df2)
Reviewed-on: http://gerrit.cloudera.org:8080/22639
---
src/kudu/util/high_water_mark.h | 38 ++++++++++++++++++--------------------
1 file changed, 18 insertions(+), 20 deletions(-)
diff --git a/src/kudu/util/high_water_mark.h b/src/kudu/util/high_water_mark.h
index 57b5c1ff9..8a148858d 100644
--- a/src/kudu/util/high_water_mark.h
+++ b/src/kudu/util/high_water_mark.h
@@ -14,11 +14,11 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
-#ifndef KUDU_UTIL_HIGH_WATER_MARK_H
-#define KUDU_UTIL_HIGH_WATER_MARK_H
+#pragma once
-#include "kudu/gutil/macros.h"
-#include "kudu/util/atomic.h"
+#include <cstdint>
+
+#include "kudu/util/atomic-utils.h"
namespace kudu {
@@ -29,21 +29,21 @@ namespace kudu {
class HighWaterMark {
public:
explicit HighWaterMark(int64_t initial_value)
- : current_value_(initial_value),
- max_value_(initial_value) {
+ : current_value_(initial_value),
+ max_value_(initial_value) {
}
// Return the current value.
int64_t current_value() const {
- return current_value_.Load(kMemOrderNoBarrier);
+ return current_value_.load(std::memory_order_relaxed);
}
// Return the max value.
int64_t max_value() const {
- return max_value_.Load(kMemOrderNoBarrier);
+ return max_value_.load(std::memory_order_relaxed);
}
- bool CanIncrementBy(int64_t delta, int64_t max) {
+ bool CanIncrementBy(int64_t delta, int64_t max) const {
return current_value() + delta <= max;
}
@@ -56,9 +56,10 @@ class HighWaterMark {
if (new_val > max) {
return false;
}
- if (PREDICT_TRUE(current_value_.CompareAndSet(old_val,
- new_val,
- kMemOrderNoBarrier))) {
+ if (current_value_.compare_exchange_weak(old_val,
+ new_val,
+ std::memory_order_release,
+ std::memory_order_relaxed)) {
UpdateMax(new_val);
return true;
}
@@ -66,24 +67,21 @@ class HighWaterMark {
}
void IncrementBy(int64_t amount) {
- UpdateMax(current_value_.IncrementBy(amount, kMemOrderNoBarrier));
+ UpdateMax(current_value_.fetch_add(amount, std::memory_order_relaxed));
}
void set_value(int64_t v) {
- current_value_.Store(v, kMemOrderNoBarrier);
+ current_value_.store(v, std::memory_order_relaxed);
UpdateMax(v);
}
private:
void UpdateMax(int64_t value) {
- max_value_.StoreMax(value, kMemOrderNoBarrier);
+ AtomicStoreMax(max_value_, value);
}
- AtomicInt<int64_t> current_value_;
- AtomicInt<int64_t> max_value_;
+ std::atomic<int64_t> current_value_;
+ std::atomic<int64_t> max_value_;
};
} // namespace kudu
-#endif /* KUDU_UTIL_HIGH_WATER_MARK_H */
-
-