bimodal-array 0.1.1

Dual-mode access to a contiguous array.
Documentation
  • Coverage
  • 100%
    14 out of 14 items documented3 out of 10 items with examples
  • Size
  • Source code size: 28.19 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.65 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • afgTheCat/bimodal-array
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • afgTheCat

bimodal_array

bimodal_array provides phase-separated, dual-mode access to a contiguous array.

It enables a scatter–gather access pattern where:

  • Independent workers can update elements concurrently.
  • A coordinator can later acquire exclusive access to the entire array.

Access Model

Access is granted through two handle types:

  • ElementHandle<T> — grants mutable access to a single element.
  • ArrayHandle<T> — grants exclusive mutable access to the entire array.

Handles represent the ability to attempt a lock. The actual lock is held by guards:

  • ElementHandle::lock() returns an ElementGuard<T>
  • ArrayHandle::lock() returns an ArrayGuard<T>

Locking Rules

  • Guards for different elements may be held concurrently.
  • An ArrayGuard<T> provides exclusive access to the entire array.
  • An ArrayGuard<T> cannot coexist with any ElementGuard<T>.
  • Lock acquisition is non-blocking.

Internally, lock state is coordinated via a single AtomicUsize. As a result, bimodal_array may not scale well under heavy contention.


Example

use bimodal_array::bimodal_array;
use std::thread;

const WORKER_COUNT: usize = 8;

let data = vec![0usize; WORKER_COUNT];
let (mut array_handle, element_handles) = bimodal_array(data);

// Scatter phase
let threads: Vec<_> = element_handles
    .into_iter()
    .enumerate()
    .map(|(idx, mut element)| {
        thread::spawn(move || {
            let mut guard = element.lock().unwrap();
            *guard = 10 - idx;
        })
    })
    .collect();

for t in threads {
    t.join().unwrap();
}

// Gather phase
let mut array_guard = array_handle.lock().unwrap();
array_guard.sort();