1
0
Fork 0
mirror of https://github.com/romanz/electrs.git synced 2025-02-23 22:56:55 +01:00

Merge branch 'atomic-counter'

This commit is contained in:
Roman Zeyde 2019-06-13 22:25:29 +03:00
commit 70d0c1d47f
No known key found for this signature in database
GPG key ID: 87CAE5FA46917CBB
2 changed files with 24 additions and 0 deletions

View file

@ -14,6 +14,9 @@ edition = "2018"
[profile.release]
lto = true
[features]
latest_rust = [] # use latest Rust features (otherwise, support Rust 1.32)
[dependencies]
arrayref = "0.3"
base64 = "0.10"

View file

@ -13,6 +13,8 @@ use std::collections::{HashMap, HashSet};
use std::io::{BufRead, BufReader, Lines, Write};
use std::net::{SocketAddr, TcpStream};
use std::path::PathBuf;
#[cfg(feature = "latest_rust")]
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
@ -266,10 +268,29 @@ impl Connection {
}
}
#[cfg(feature = "latest_rust")]
struct Counter {
value: AtomicU64,
}
#[cfg(feature = "latest_rust")]
impl Counter {
fn new() -> Self {
Counter { value: 0.into() }
}
fn next(&self) -> u64 {
// fetch_add() returns previous value, we want current one
self.value.fetch_add(1, Ordering::Relaxed) + 1
}
}
#[cfg(not(feature = "latest_rust"))]
struct Counter {
value: Mutex<u64>,
}
#[cfg(not(feature = "latest_rust"))]
impl Counter {
fn new() -> Self {
Counter {