/// Atomically reads data from `src`.
///
/// This operation uses the `SeqCst` ordering. If possible, an atomic instructions is used, and a
/// global lock otherwise.
Atomically reads data from `src`.
This operation uses the `SeqCst` ordering. If possible, an atomic instructions is used, and a
global lock otherwise.
Atomically reads data from ` src ` . This operation uses the ` SeqCst ` ordering . If possible an atomic instructions is used and a global lock otherwise .
unsafe fn atomic_load<T>(src: *mut T) -> T
where
    T: Copy,
{
    atomic! {
        T, a,
        {
            a = &*(src as *const _ as *const _);
            mem::transmute_copy(&a.load(Ordering::SeqCst))
        },
        {
            let lock = lock(src as usize);
            // Try doing an optimistic read first.
            if let Some(stamp) = lock.optimistic_read() {
                // We need a volatile read here because other threads might concurrently modify the
                // value. In theory, data races are *always* UB, even if we use volatile reads and
                // discard the data when a data race is detected. The proper solution would be to
                // do atomic reads and atomic writes, but we can't atomically read and write all
                // kinds of data since `AtomicU8` is not available on stable Rust yet.
                let val = ptr::read_volatile(src);
                if lock.validate_read(stamp) {
                    return val;
                }
                mem::forget(val);
            }
            // Grab a regular write lock so that writers don't starve this load.
            let guard = lock.write();
            let val = ptr::read(src);
            // The value hasn't been changed. Drop the guard without incrementing the stamp.
            guard.abort();
            val
        }
    }
}
unsafe fn atomic_load < T > ( src : * mut T ) -> T where T : Copy , { atomic ! { T a { a ( src as const as const ) mem transmute_copy ( a load ( Ordering SeqCst ) ) } { let lock lock ( src as usize ) // Try doing an optimistic read first. if let Some ( stamp ) lock optimistic_read ( ) { // We need a volatile read here because other threads might concurrently modify the // value. In theory, data races are *always* UB, even if we use volatile reads and // discard the data when a data race is detected. The proper solution would be to // do atomic reads and atomic writes, but we can't atomically read and write all // kinds of data since `AtomicU8` is not available on stable Rust yet. let val ptr read_volatile ( src ) if lock validate_read ( stamp ) { return val } mem forget ( val ) } // Grab a regular write lock so that writers don't starve this load. let guard lock write ( ) let val ptr read ( src ) // The value hasn't been changed. Drop the guard without incrementing the stamp. guard abort ( ) val } } }
Atomically reads data from `src`.
Atomically reads data from ` src ` .
src
*mut T