fn acquire_read(config: &AtomicU32) -> Result<usize, TripleError> {
        // The variable storing the aquired read lock index
        // The initial value is an invalid index, on all paths that this
        // value is not set in the fetch_update, an error should be returned
        let mut read_lock = 3;

        config
            .fetch_update(SeqCst, SeqCst, |x| {
                let mut cfg: TripleConfig = x.into();

                // one state already has a read lock, so don't try to acquire
                // another lock
                if cfg.read != 3 {
                    return None;
                }

                // try to get the most recently written, unlocked state
                let read = if cfg.write != cfg.idx1 {
                    cfg.idx1
                } else if cfg.write != cfg.idx2 {
                    cfg.idx2
                } else if cfg.write != cfg.idx3 {
                    cfg.idx3
                } else {
                    // should not get here, but handle it anyway
                    return None;
                };

                // set the config to have the state chosen in the read lock
                // index
                cfg.read = read;

                // set the variable that says which lock was aquired to return
                // from this aquire function
                read_lock = read;

                // convert the state back into a single number to be stored
                Some(cfg.into())
            })
            .map_err(|_| TripleError::ReadUpdate)?;

        // read the aquired lock
        Ok(read_lock as usize)
    }
/// Atomically try to get the read lock.
/// Takes the reference to the config to atomically update
/// Returns the index of the state that was acquired.
/// Tries to get the states in order of most recently updated to least
/// recently.
/// Returns an error if the read lock is already acquired or if it is
/// unable to find an unlocked state.
Atomically try to get the read lock.
Takes the reference to the config to atomically update
Returns the index of the state that was acquired.
Tries to get the states in order of most recently updated to least
recently.
Returns an error if the read lock is already acquired or if it is
unable to find an unlocked state.
Atomically try to get the read lock . Takes the reference to the config to atomically update Returns the index of the state that was acquired . Tries to get the states in order of most recently updated to least recently . Returns an error if the read lock is already acquired or if it is unable to find an unlocked state .
fn acquire_read(config: &AtomicU32) -> Result<usize, TripleError> {
        // The variable storing the aquired read lock index
        // The initial value is an invalid index, on all paths that this
        // value is not set in the fetch_update, an error should be returned
        let mut read_lock = 3;
        config
            .fetch_update(SeqCst, SeqCst, |x| {
                let mut cfg: TripleConfig = x.into();
                // one state already has a read lock, so don't try to acquire
                // another lock
                if cfg.read != 3 {
                    return None;
                }
                // try to get the most recently written, unlocked state
                let read = if cfg.write != cfg.idx1 {
                    cfg.idx1
                } else if cfg.write != cfg.idx2 {
                    cfg.idx2
                } else if cfg.write != cfg.idx3 {
                    cfg.idx3
                } else {
                    // should not get here, but handle it anyway
                    return None;
                };
                // set the config to have the state chosen in the read lock
                // index
                cfg.read = read;
                // set the variable that says which lock was aquired to return
                // from this aquire function
                read_lock = read;
                // convert the state back into a single number to be stored
                Some(cfg.into())
            })
            .map_err(|_| TripleError::ReadUpdate)?;
        // read the aquired lock
        Ok(read_lock as usize)
    }
fn acquire_read ( config : & AtomicU32 ) -> Result < usize , TripleError > { // The variable storing the aquired read lock index // The initial value is an invalid index, on all paths that this // value is not set in the fetch_update, an error should be returned let mut read_lock = 3 ; config . fetch_update ( SeqCst , SeqCst , | x | { let mut cfg : TripleConfig = x . into ( ) ; // one state already has a read lock, so don't try to acquire // another lock if cfg . read != 3 { return None ; } // try to get the most recently written, unlocked state let read = if cfg . write != cfg . idx1 { cfg . idx1 } else if cfg . write != cfg . idx2 { cfg . idx2 } else if cfg . write != cfg . idx3 { cfg . idx3 } else { // should not get here, but handle it anyway return None ; } ; // set the config to have the state chosen in the read lock // index cfg . read = read ; // set the variable that says which lock was aquired to return // from this aquire function read_lock = read ; // convert the state back into a single number to be stored Some ( cfg . into ( ) ) } ) . map_err ( | _ | TripleError :: ReadUpdate ) ? ; // read the aquired lock Ok ( read_lock as usize ) }
Atomically try to get the read lock.
Atomically try to get the read lock .
config
&AtomicU32
config
&AtomicU32