Struct sc_consensus::shared_data::SharedData
source · pub struct SharedData<T> { /* private fields */ }
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");
Deadlock
Be aware that this data structure doesn’t give you any guarantees that you can not create a
deadlock. If you use release_mutex
followed by a call
to shared_data
in the same thread will make your program dead lock.
The same applies when you are using a single threaded executor.
Implementations§
sourcepub fn new(shared_data: T) -> Self
pub fn new(shared_data: T) -> Self
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§
Auto Trait Implementations§
Blanket Implementations§
§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
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
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.fn __clone_box(&self, _: Private) -> *mut ()
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere Self: UniqueSaturatedFrom<T>,
§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere Self: UniqueSaturatedInto<T>,
T
. Read moresource§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from
.§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T
.