Skip to main content

Crate bimodal_array

Crate bimodal_array 

Source
Expand description

bimodal_array provides dual-mode access to a contiguous array. Access is granted through two handler types:

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

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

  1. when ElementHandle::lock() succeeds, it produces an ElementGuard<T>
  2. when ArrayHandle::lock() succeeds, it produces an ArrayGuard<T>.

Each ElementHandle is bound to a specific element. Guards for different elements may may be held concurrently. An ArrayGuard<T> provides exclusive access to the entire array and cannot coexist with any ElementGuard<T>.

This pattern is useful for scatter–gather style workloads where independent workers update elements in parallel, followed by an orchestration step that processes the entire array. Internally, lock state is coordinated via a single AtomicUsize. As a result, bimodal_array may not scale well under high contention.

§Examples

const WORKER_COUNT: usize = 8;
let data = vec![0usize; WORKER_COUNT];
let (mut array_handle, element_handles) = bimodal_array(data);
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();
threads.into_iter().for_each(|t| t.join().unwrap());
let mut array_guard = array_handle.lock().unwrap();
array_guard.sort();

Structs§

ArrayGuard
A guard providing exclusive access to the entire array.
ArrayHandle
A handle granting array-wide access to a bimodal_array.
ElementGuard
A guard providing mutable access to a single element.
ElementHandle
A handle granting access to a single element of a bimodal_array.

Enums§

BimodalArrayError
Errors that can occur when constructing or using a bimodal_array.

Functions§

bimodal_array
Constructs a bimodal array from an owned Vec<T>.
try_bimodal_array
Constructs a bimodal array from an owned Vec<T>.