Browse Source

Merge branch 'atomic-counter'

tmp/parse_blk
Roman Zeyde 5 years ago
parent
commit
70d0c1d47f
No known key found for this signature in database GPG Key ID: 87CAE5FA46917CBB
  1. 3
      Cargo.toml
  2. 21
      src/daemon.rs

3
Cargo.toml

@ -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"

21
src/daemon.rs

@ -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 {

Loading…
Cancel
Save