Struct sc_consensus::shared_data::SharedData[][src]

pub struct SharedData<T> { /* fields omitted */ }
Expand description

Some shared data that provides support for locking this shared data for some time.

When working with consensus engines there is often data that needs to be shared between multiple parts of the system, like block production and block import. This struct provides an abstraction for this shared data in a generic way.

The pain point when sharing this data is often the usage of mutex guards in an async context as this doesn’t work for most of them as these guards don’t implement Send. This abstraction provides a way to lock the shared data, while not having the mutex locked. So, the data stays locked and we are still able to hold this lock over an await call.

Example


let shared_data = SharedData::new(String::from("hello world"));

let lock = shared_data.shared_data_locked();

let shared_data2 = shared_data.clone();
let join_handle1 = std::thread::spawn(move || {
    // This will need to wait for the outer lock to be released before it can access the data.
    shared_data2.shared_data().push_str("1");
});

assert_eq!(*lock, "hello world");

// Let us release the mutex, but we still keep it locked.
// Now we could call `await` for example.
let mut lock = lock.release_mutex();

let shared_data2 = shared_data.clone();
let join_handle2 = std::thread::spawn(move || {
    shared_data2.shared_data().push_str("2");
});

// We still have the lock and can upgrade it to access the data.
assert_eq!(*lock.upgrade(), "hello world");
lock.upgrade().push_str("3");

drop(lock);
join_handle1.join().unwrap();
join_handle2.join().unwrap();

let data = shared_data.shared_data();
// As we don't know the order of the threads, we need to check for both combinations
assert!(*data == "hello world321" || *data == "hello world312");

Implementations

Create a new instance of SharedData to share the given shared_data.

Acquire access to the shared data.

This will give mutable access to the shared data. After the returned mutex guard is dropped, the shared data is accessible by other threads. So, this function should be used when reading/writing of the shared data in a local context is required.

When requiring to lock shared data for some longer time, even with temporarily releasing the lock, Self::shared_data_locked should be used.

Acquire access to the shared data and lock it.

This will give mutable access to the shared data. The returned SharedDataLocked provides the function SharedDataLocked::release_mutex to release the mutex, but keeping the data locked. This is useful in async contexts for example where the data needs to be locked, but a mutex guard can not be held.

For an example see SharedData.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Convert from a value of T into an equivalent instance of Option<Self>. Read more

Consume self to return Some equivalent value of Option<T>. Read more

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Get a reference to the inner from the outer.

Get a mutable reference to the inner from the outer.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

Convert from a value of T into an equivalent instance of Self. Read more

Consume self to return an equivalent value of T. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The counterpart to unchecked_from.

Consume self to return an equivalent value of T.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more