Macro active_standby::generate_aslock_handle[][src]

macro_rules! generate_aslock_handle {
    ( $Table:ident
        // Table might be a template type.
        $(<
            // Any number of inner types.
            $( $Inner:tt ),*
        >)?
    ) => { ... };
}
Expand description

This macro automatically generates an easy to use interface for interacting with an ActiveStandby data structure. The resulting AsLockHandle can be thought of as similar to Arc<RwLock>.

The user adds mutability to the table is by creating an impl for the generated WriteGuard.

The macro can’t handle paths, so you can’t pass ‘std::collections::HashMap’. In such a case just put ‘use std::collections::HashMap’ right before the macro invocation.

For a simple example check out bench.rs. For larger examples, check out active_standby::collections.

pub mod aslock {
    use active_standby::primitives::UpdateTables;
 
    // Generate an AsLockHandle, which will give wait free read accees
    // to the underlying data. This also generates the associated WriteGuard
    // which is used to mutate the data. Users should interact with this
    // similarly to Arc<RwLock<i32>>.
    active_standby::generate_aslock_handle!(i32);

    // Client's must implement the mutable interface that they want to offer
    // users of their active standby data structure. This is not automatically
    // generated.
    impl<'w> WriteGuard<'w> {
        pub fn add_one(&mut self) {
            struct AddOne {}
             
            impl<'a> UpdateTables<'a, i32, ()> for AddOne {
                fn apply_first(&mut self, table: &'a mut i32) {
                    *table = *table + 1;
                }
                fn apply_second(mut self, table: &mut i32) {
                    self.apply_first(table);
                }
            }
     
            self.guard.update_tables(AddOne {})
        }
    }
}

fn main() {
    let mut table = aslock::AsLockHandle::new(0);
    let mut table2 = table.clone();
    let handle = std::thread::spawn(move || {
        while *table2.read() != 1 {
            std::thread::sleep(std::time::Duration::from_micros(100));
        }
    });

    {
        let mut wg = table.write();
        wg.add_one();
    }
    handle.join();
}