atomic-time 0.2.1

Lock-free, thread-safe atomic versions of Duration, SystemTime, Instant and their Option variants
Documentation

Lock-free, thread-safe atomic versions of Duration, SystemTime, Instant and their Option variants

English | 简体中文

Introduction

atomic-time provides lock-free, thread-safe atomic versions of Rust's standard time types. All types use AtomicU128 (via portable-atomic) under the hood and expose the same API patterns as the standard std::sync::atomic types (load, store, swap, compare_exchange, compare_exchange_weak, fetch_update).

Types

Type Wraps no_std
AtomicDuration Duration Yes
AtomicOptionDuration Option<Duration> Yes
AtomicSystemTime SystemTime No
AtomicOptionSystemTime Option<SystemTime> No
AtomicInstant Instant No
AtomicOptionInstant Option<Instant> No

Installation

[dependencies]
atomic-time = "0.2"

Feature Flags

Feature Default Description
std Yes Enables SystemTime and Instant types
serde No Enables Serialize/Deserialize for all types

For no_std environments (only AtomicDuration and AtomicOptionDuration are available):

[dependencies]
atomic-time = { version = "0.2", default-features = false }

Example

use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use atomic_time::AtomicDuration;

let timeout = Arc::new(AtomicDuration::new(Duration::from_secs(30)));

// Update from another thread
let timeout_clone = timeout.clone();
std::thread::spawn(move || {
    timeout_clone.store(Duration::from_secs(60), Ordering::Release);
});
use std::sync::atomic::Ordering;
use std::time::Instant;
use atomic_time::AtomicOptionInstant;

// Track the last time an event occurred
let last_event = AtomicOptionInstant::none();
assert_eq!(last_event.load(Ordering::Relaxed), None);

last_event.store(Some(Instant::now()), Ordering::Release);
assert!(last_event.load(Ordering::Acquire).is_some());

Benchmarks

Run with cargo bench in the benchmark/ directory. Apple M4 Pro.

Duration (cargo bench --bench duration)

Implementation Single-thread load Single-thread store Contended read Load under write contention Contended store
AtomicDuration 1.04 ns 0.72 ns 1.08 ns 4.44 ns 10.7 ns
AtomicOptionDuration 1.16 ns 0.74 ns 1.20 ns 4.73 ns 14.5 ns
ArcSwap<Duration> 2.30 ns 86.5 ns 2.38 ns 15.5 ns 1,400 ns
parking_lot::RwLock 3.40 ns 2.09 ns 9.3 ns 241.8 ns 37.7 ns
std::sync::RwLock 4.44 ns 2.26 ns 411.2 ns 89.0 ns 36.8 ns

Instant (cargo bench --bench instant)

Implementation Single-thread load Single-thread store Contended read Load under write contention Contended store
AtomicInstant 2.16 ns 3.52 ns 2.17 ns 14.97 ns 22.7 ns
AtomicOptionInstant 2.36 ns 3.51 ns 2.40 ns 17.63 ns 21.2 ns
ArcSwap<Instant> 2.95 ns 74.9 ns 2.92 ns 17.53 ns 972 ns
parking_lot::RwLock 3.43 ns 2.10 ns 81.3 ns 213.9 ns 26.9 ns
std::sync::RwLock 4.48 ns 2.28 ns 422.3 ns 87.7 ns 76.9 ns

SystemTime (cargo bench --bench system_time)

Implementation Single-thread load Single-thread store Contended read Load under write contention Contended store
AtomicSystemTime 1.26 ns 3.41 ns 1.28 ns 8.89 ns 27.1 ns
AtomicOptionSystemTime 1.25 ns 3.47 ns 1.30 ns 9.17 ns 25.2 ns
ArcSwap<SystemTime> 2.29 ns 83.7 ns 2.39 ns 16.4 ns 1,227 ns
parking_lot::RwLock 3.50 ns 2.13 ns 22.0 ns 204.5 ns 31.0 ns
std::sync::RwLock 4.69 ns 2.31 ns 554.2 ns 84.4 ns 38.7 ns

Contended read = 4 background threads also reading; Load under write contention = 4 background threads writing, load measured on main thread; Contended store = 4 background threads writing, store measured on main thread. All numbers from Apple M4 Pro.

MSRV

The minimum supported Rust version is 1.70.0.

License

atomic-time is under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE, LICENSE-MIT for details.

Copyright (c) 2026 Al Liu.