Crate cycler[][src]

This is a data synchronization system with 1 writer and N readers. All writers and readers can operate on the data at the same time and will access the most up to date version when they switch. This is accomplished by storing N + 2 copies of the data with the writer publishing new versions of the data. The purpose of this is to allow read threads to have an unchanging up to date copy of the main data while the writer can edit it at the same time.

Requirements

The data type must also implement both ReadAccess and WriteAccess otherwise the access functions won’t be available. These types allow the reader to see a subset of the data, intended for hiding data only the writer needs like a change log for optimization of the copy. If you don’t care about access restrictions you can set ReadAccess::Read and WriteAccess::Write to Self and there will be zero runtime cost as the compiler should optimize that out.

Optimization

The most optimal thing to do with regard to memory usage is to have a single reader. N + 2 copies of the data must exist because in the worst case scenario all N readers are reading separate copies and the writer is finalizing a write. In this case the N copies of the data that are being read are inaccessible to writes and the last updated index cannot be written to so there must be an additional copy. What this means is that if you only utilize a single writer and distribute that the minimum amount of memory will be used. The trade off is that if you have multiple reading loops that operate at differing rates all the loops will operate at the slowest speed. Adding readers does not increase the amount of data to copy and may only slightly increase the time between copy switches.

This also is based on the clone_from idea to clone values which is not implemented by derive normally (Derivative can auto derive for you). This is a major optimization chance in this case and you can test/track the changes to reduce copy time.

Modules

rw_lock_cycler

The RwLockCycler uses parking_lot::RwLock to keep track of reading and writing to each block without using unsafe code. This is the first implementation but may not be the fastest as locks are required for each switch. Due to the way the data structure is designed there is always an available slot so all lock obtainment are use the try variant.

traits

These are traits that are implemented for the types of cyclers. CyclerWriter has many sub-traits that contain the functions allowing the writer to move to the next block, while CyclerReader is a single trait that allows the reader to move to the latest block. WriteAccess and ReadAccess are traits that should be implemented for any type that goes into a cycler.

Functions

build_multiple_reader

Creates a multi reader DefaultCycler, the amount of readers being initial_values.len() - 2.

build_single_reader

Creates a single reader RwLockCycler using values as the initial values for the slots.

Type Definitions

DefaultCyclerReader

This is the currently most optimal cycler reader that implements UniversalCyclerReader.

DefaultCyclerWriter

This is the currently most optimal cycler writer that implements UniversalCyclerWriter.