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

source

pub fn new(path: &str) -> Synchronizer

Create new instance of Synchronizer using given path

source

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>>,

Writes a given entity into the next available data file.

Returns the number of bytes written to the data file and a boolean flag, for diagnostic purposes, indicating whether the reader counter was reset due to a reader exiting without decrementing it.

Parameters
  • entity: The entity to be written to the data file.
  • grace_duration: The maximum period to wait for readers to finish before resetting the reader count to 0. This handles scenarios where a reader process has crashed or exited abnormally, failing to decrement the reader count. After the grace_duration has elapsed, if there are still active readers, the reader count is reset to 0 to restore synchronization state.
Returns

A result containing a tuple of the number of bytes written and a boolean indicating whether the reader count was reset, or a SynchronizerError if the operation fails.

source

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.

source

pub unsafe fn read<T>(&mut self) -> Result<ReadResult<'_, T>, SynchronizerError>where T: Archive, T::Archived: for<'b> CheckBytes<DefaultValidator<'b>>,

Reads and returns an entity struct from mapped memory wrapped in ReadGuard.

Safety

This method is marked as unsafe due to the potential for memory corruption if the returned result is used beyond the grace_duration set in the write method. The caller must ensure the ReadGuard (and any references derived from it) are dropped before this time period elapses to ensure safe operation.

Additionally, the use of unsafe here is related to the internal use of the rkyv::archived_root function, which has its own safety considerations. Particularly, it assumes the byte slice provided to it accurately represents an archived object, and that the root of the object is stored at the end of the slice.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

source§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> LayoutRaw for T

source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Gets the layout of the type.
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.