Synchronizer

Struct Synchronizer 

Source
pub struct Synchronizer<H: Hasher + Default = WyHash, WL = LockDisabled, const N: usize = 1024, const SD: u64 = 1000000000> { /* 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.

Template parameters:

  • H - hasher used for checksum calculation
  • WL - optional write locking to prevent multiple writers. (default LockDisabled)
  • N - serializer scratch space size
  • SD - sleep duration in nanoseconds used by writer during lock acquisition (default 1s)

Implementations§

Source§

impl Synchronizer

Source

pub fn new(path_prefix: &OsStr) -> Self

Create new instance of Synchronizer using given path_prefix and default template parameters

Source§

impl<'a, H, WL, const N: usize, const SD: u64> Synchronizer<H, WL, N, SD>
where H: Hasher + Default, WL: WriteLockStrategy<'a>,

Source

pub fn with_params(path_prefix: &OsStr) -> Self

Create new instance of Synchronizer using given path_prefix and template parameters

Source

pub fn write<T>( &'a mut self, entity: &T, grace_duration: Duration, ) -> Result<(usize, bool), SynchronizerError>

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>( &'a mut self, data: &[u8], grace_duration: Duration, ) -> Result<(usize, bool), SynchronizerError>

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>( &'a mut self, check_bytes: bool, ) -> 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.

§Parameters
  • check_bytes: Whether to check that entity bytes can be safely read for type T, false - bytes check will not be performed (faster, but less safe), true - bytes check will be performed (slower, but safer).
§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.

Source

pub fn version(&'a mut self) -> Result<InstanceVersion, SynchronizerError>

Returns current InstanceVersion stored within the state, useful for detecting whether synchronized entity has changed.

Auto Trait Implementations§

§

impl<H, WL, const N: usize, const SD: u64> Freeze for Synchronizer<H, WL, N, SD>
where WL: Freeze,

§

impl<H, WL, const N: usize, const SD: u64> RefUnwindSafe for Synchronizer<H, WL, N, SD>
where WL: RefUnwindSafe,

§

impl<H, WL, const N: usize, const SD: u64> Send for Synchronizer<H, WL, N, SD>
where WL: Send,

§

impl<H, WL, const N: usize, const SD: u64> Sync for Synchronizer<H, WL, N, SD>
where WL: Sync,

§

impl<H, WL, const N: usize, const SD: u64> Unpin for Synchronizer<H, WL, N, SD>
where WL: Unpin,

§

impl<H, WL, const N: usize, const SD: u64> UnwindSafe for Synchronizer<H, WL, N, SD>
where WL: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

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 T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 F
where 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 T
where 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.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

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

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.