Skip to content

Commit 16dfe55

Browse files
mxindenfggarcia
andauthored
*: Use compare_exchange and fix sync import (#381)
* src/: Replace deprecated compare_and_swap with compare_exchange(_weak) `compare_and_swap` is deprecated in favor of `compare_exchange` and `compare_exchange_weak`. This commit replaces the former with either of the two latter options. Signed-off-by: Max Inden <[email protected]> * static/metric/: use TokenStream from proc_macro Signed-off-by: Federico Garcia Ronca <[email protected]> Co-authored-by: Federico Garcia Ronca <[email protected]>
1 parent 04622e3 commit 16dfe55

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

src/atomic64.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ impl Atomic for AtomicF64 {
110110
loop {
111111
let current = self.inner.load(Ordering::Acquire);
112112
let new = u64_to_f64(current) + delta;
113-
let swapped = self
114-
.inner
115-
.compare_and_swap(current, f64_to_u64(new), Ordering::Release);
116-
if swapped == current {
113+
let result = self.inner.compare_exchange_weak(
114+
current,
115+
f64_to_u64(new),
116+
Ordering::Release,
117+
Ordering::Relaxed,
118+
);
119+
if result.is_ok() {
117120
return;
118121
}
119122
}
@@ -205,9 +208,24 @@ impl Atomic for AtomicU64 {
205208
}
206209

207210
impl AtomicU64 {
208-
/// Get the value with the provided memory ordering.
209-
pub fn compare_and_swap(&self, current: u64, new: u64, ordering: Ordering) -> u64 {
210-
self.inner.compare_and_swap(current, new, ordering)
211+
/// Stores a value into the atomic integer if the current value is the same
212+
/// as the current value.
213+
///
214+
/// This function is allowed to spuriously fail even when the comparison
215+
/// succeeds, which can result in more efficient code on some platforms. The
216+
/// return value is a result indicating whether the new value was written
217+
/// and containing the previous value.
218+
///
219+
/// See [`StdAtomicU64`] for details.
220+
pub(crate) fn compare_exchange_weak(
221+
&self,
222+
current: u64,
223+
new: u64,
224+
success: Ordering,
225+
failure: Ordering,
226+
) -> Result<u64, u64> {
227+
self.inner
228+
.compare_exchange_weak(current, new, success, failure)
211229
}
212230

213231
/// Increment the value by a given amount with the provided memory ordering.

src/histogram.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,21 @@ impl HistogramCore {
410410
// shards were flipped, all in-progress `observe` calls are done. With
411411
// all of them done, the cold shard is now in a consistent state.
412412
//
413-
// `observe` uses `Release` ordering. `compare_and_swap` needs to use
413+
// `observe` uses `Release` ordering. `compare_exchange` needs to use
414414
// `Acquire` ordering to ensure that (1) one sees all the previous
415415
// `observe` stores to the counter and (2) to ensure the below shard
416416
// modifications happen after this point, thus the shard is not modified
417417
// by any `observe` operations.
418-
while overall_count
419-
!= cold_shard.count.compare_and_swap(
418+
while cold_shard
419+
.count
420+
.compare_exchange_weak(
420421
overall_count,
421422
// While at it, reset cold shard count on success.
422423
0,
423424
Ordering::Acquire,
425+
Ordering::Acquire,
424426
)
427+
.is_err()
425428
{}
426429

427430
// Get cold shard sum and reset to 0.

src/timer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ const CHECK_UPDATE_INTERVAL: Duration = Duration::from_millis(200);
4545

4646
/// Ensures background updater is running, which will call `now_millis` periodically.
4747
pub fn ensure_updater() {
48-
if !UPDATER_IS_RUNNING.compare_and_swap(false, true, Ordering::SeqCst) {
48+
if UPDATER_IS_RUNNING
49+
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
50+
.is_ok()
51+
{
4952
std::thread::Builder::new()
5053
.name("time updater".to_owned())
5154
.spawn(|| loop {

static-metric/src/auto_flush_from.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
use proc_macro::TokenStream;
34
use proc_macro2::Span;
4-
use syn::export::TokenStream2;
55
use syn::parse::{Parse, ParseStream};
66
use syn::token::*;
77
use syn::*;
@@ -38,7 +38,7 @@ impl Parse for AutoFlushFromDef {
3838
}
3939

4040
impl AutoFlushFromDef {
41-
pub fn auto_flush_from(&self) -> TokenStream2 {
41+
pub fn auto_flush_from(&self) -> TokenStream {
4242
let inner_class_name = self.inner_class_name.clone();
4343
let class_name = self.class_name.clone();
4444
let source_var_name = self.source_var_name.clone();
@@ -50,13 +50,14 @@ impl AutoFlushFromDef {
5050
}
5151
None => quote! {},
5252
};
53-
quote! {
53+
let token_stream_inner = quote! {
5454
{
5555
thread_local! {
5656
static INNER: #inner_class_name = #inner_class_name::from(& #source_var_name)#update_duration;
5757
}
5858
#class_name::from(&INNER)
5959
}
60-
}
60+
};
61+
TokenStream::from(token_stream_inner)
6162
}
6263
}

static-metric/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn make_auto_flush_static_metric(input: TokenStream) -> TokenStream {
6363
#[proc_macro]
6464
pub fn auto_flush_from(input: TokenStream) -> TokenStream {
6565
let def: AutoFlushFromDef = syn::parse(input).unwrap();
66-
def.auto_flush_from().into()
66+
def.auto_flush_from()
6767
}
6868

6969
/// Register a `CounterVec` and create static metrics from it.

0 commit comments

Comments
 (0)