fn aquire_write(config: &AtomicU32) -> Result<usize, TripleError> {
        // The variable storing the aquired write 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 write_lock = 3;

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

                // returns if the write lock has already been acquired, so it
                // does not aquire multiple write locks
                if cfg.write != 3 {
                    return None;
                }

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

                // update the new config
                cfg.write = write;

                // set the return value
                write_lock = write;

                // convert the config back into a single number
                Some(cfg.into())
            })
            .map_err(|_| TripleError::WriteUpdate)?;

        // return the index of the aquired lock
        Ok(write_lock as usize)
    }
/// Atomically try to get the write 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 least recently updated to most
/// recently.
/// Returns an error if the write lock is already acquired or if it is
/// unable to find an unlocked state.
Atomically try to get the write 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 least recently updated to most
recently.
Returns an error if the write lock is already acquired or if it is
unable to find an unlocked state.
Atomically try to get the write 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 least recently updated to most recently . Returns an error if the write lock is already acquired or if it is unable to find an unlocked state .
fn aquire_write(config: &AtomicU32) -> Result<usize, TripleError> {
        // The variable storing the aquired write 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 write_lock = 3;
        config
            .fetch_update(SeqCst, SeqCst, |x| {
                let mut cfg: TripleConfig = x.into();
                // returns if the write lock has already been acquired, so it
                // does not aquire multiple write locks
                if cfg.write != 3 {
                    return None;
                }
                // try to get the least recently updated state
                let write = if cfg.read != cfg.idx3 {
                    cfg.idx3
                } else if cfg.read != cfg.idx2 {
                    cfg.idx2
                } else if cfg.read != cfg.idx1 {
                    cfg.idx1
                } else {
                    // should not be possible to get here, but handle it anyway
                    return None;
                };
                // update the new config
                cfg.write = write;
                // set the return value
                write_lock = write;
                // convert the config back into a single number
                Some(cfg.into())
            })
            .map_err(|_| TripleError::WriteUpdate)?;
        // return the index of the aquired lock
        Ok(write_lock as usize)
    }
fn aquire_write ( config : & AtomicU32 ) -> Result < usize , TripleError > { // The variable storing the aquired write 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 write_lock = 3 ; config . fetch_update ( SeqCst , SeqCst , | x | { let mut cfg : TripleConfig = x . into ( ) ; // returns if the write lock has already been acquired, so it // does not aquire multiple write locks if cfg . write != 3 { return None ; } // try to get the least recently updated state let write = if cfg . read != cfg . idx3 { cfg . idx3 } else if cfg . read != cfg . idx2 { cfg . idx2 } else if cfg . read != cfg . idx1 { cfg . idx1 } else { // should not be possible to get here, but handle it anyway return None ; } ; // update the new config cfg . write = write ; // set the return value write_lock = write ; // convert the config back into a single number Some ( cfg . into ( ) ) } ) . map_err ( | _ | TripleError :: WriteUpdate ) ? ; // return the index of the aquired lock Ok ( write_lock as usize ) }
Atomically try to get the write lock
Takes the reference to the config to atomically update
Returns the index of the state that was acquired.
Atomically try to get the write lock Takes the reference to the config to atomically update Returns the index of the state that was acquired .
config
&AtomicU32
config
&AtomicU32