Struct mmap_sync::synchronizer::Synchronizer
source · pub struct Synchronizer { /* private fields */ }Expand description
Synchronizer is a concurrency primitive that manages data access between a single writer process and multiple reader processes.
It coordinates the access to two data files that store the shared data. A state file, also memory-mapped, stores the index of the current data file and the number of active readers for each index, updated via atomic instructions.
Implementations§
source§impl Synchronizer
impl Synchronizer
sourcepub fn new(path: &str) -> Synchronizer
pub fn new(path: &str) -> Synchronizer
Create new instance of Synchronizer using given path
sourcepub fn write<T>(
&mut self,
entity: &T,
grace_duration: Duration
) -> Result<(usize, bool), SynchronizerError>where
T: Serialize<AllocSerializer<1_000_000>>,
T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,
pub fn write<T>( &mut self, entity: &T, grace_duration: Duration ) -> Result<(usize, bool), SynchronizerError>where T: Serialize<AllocSerializer<1_000_000>>, T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,
Write given entity into the next available data file.
Returns number of bytes written to data file and a boolean flag, for diagnostic purposes,
indicating that we have reset our readers counter after a reader died without decrementing it.
sourcepub fn write_raw<T>(
&mut self,
data: &[u8],
grace_duration: Duration
) -> Result<(usize, bool), SynchronizerError>where
T: Serialize<AllocSerializer<1_000_000>>,
T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,
pub fn write_raw<T>( &mut self, data: &[u8], grace_duration: Duration ) -> Result<(usize, bool), SynchronizerError>where T: Serialize<AllocSerializer<1_000_000>>, T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,
Write raw data bytes representing type T into the next available data file.
Returns number of bytes written to data file and a boolean flag, for diagnostic purposes,
indicating that we have reset our readers counter after a reader died without decrementing it.
sourcepub unsafe fn read<T>(&mut self) -> Result<ReadResult<'_, T>, SynchronizerError>where
T: Archive,
T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,
pub unsafe fn read<T>(&mut self) -> Result<ReadResult<'_, T>, SynchronizerError>where T: Archive, T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,
Reads and returns entity struct from mapped memory wrapped in ReadGuard
Safety
This method marked unsafe because using returned result beyond grace_duration
may lead to memory corruption issues, so the caller must ensure prompt result disposal.