crossync 0.1.2

A fast concurrent programming suite for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use crate::sync::{Backoff, RawMutex, WatchGuardMut, WatchGuardRef};
use crossbeam_utils::CachePadded;
use std::cell::UnsafeCell;
use std::fmt;
use std::iter::FromIterator;
use std::mem::{self, MaybeUninit};
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering, fence};

/// Default capacity for the array
const DEFAULT_ARRAY_CAP: usize = 32;

/// Slot state flags (u32 for memory efficiency and cache usage)
const EMPTY: usize = 0;
const WRITE: usize = 1;
const READ: usize = 2;

/// Represents a single slot in the array.
struct Slot<T> {
    value: UnsafeCell<MaybeUninit<T>>,
    state: AtomicUsize,
    lock: RawMutex,
}

impl<T> Slot<T> {
    #[inline(always)]
    fn new() -> Self {
        Self {
            value: UnsafeCell::new(MaybeUninit::uninit()),
            state: AtomicUsize::new(EMPTY),
            lock: RawMutex::new(),
        }
    }

    /// Wait until this slot is written, with fast-path check and backoff spin
    #[inline(always)]
    fn wait_write(&self) {
        if self.state.load(Ordering::Acquire) & WRITE != 0 {
            return;
        }
        let backoff = Backoff::new();
        while self.state.load(Ordering::Acquire) & WRITE == 0 {
            backoff.snooze();
        }
    }

    /// Reset slot to EMPTY state
    #[inline(always)]
    fn reset(&self) {
        self.state.store(EMPTY, Ordering::Relaxed);
    }

    /// Return true if slot contains written data
    #[inline(always)]
    fn is_written(&self) -> bool {
        self.state.load(Ordering::Relaxed) & WRITE != 0
    }
}

/// Internal structure representing the array
struct ArrayStorage<T> {
    slots: *mut Slot<T>,
    capacity: usize,
}

struct InnerArray<T> {
    storage: UnsafeCell<ArrayStorage<T>>,
    head: CachePadded<AtomicUsize>,
    tail: CachePadded<AtomicUsize>,
    len: CachePadded<AtomicUsize>,
    coord_lock: RawMutex,
    ref_count: CachePadded<AtomicUsize>,
}

struct ArrayCoordGuard<'a> {
    lock: &'a RawMutex,
    exclusive: bool,
}

impl<'a> ArrayCoordGuard<'a> {
    fn shared(lock: &'a RawMutex) -> Self {
        lock.lock_shared();
        Self {
            lock,
            exclusive: false,
        }
    }

    fn exclusive(lock: &'a RawMutex) -> Self {
        lock.lock_exclusive();
        Self {
            lock,
            exclusive: true,
        }
    }
}

impl Drop for ArrayCoordGuard<'_> {
    fn drop(&mut self) {
        if self.exclusive {
            self.lock.unlock_exclusive();
        } else {
            self.lock.unlock_shared();
        }
    }
}

/// Thread-safe atomic array wrapper
#[repr(transparent)]
pub struct AtomicArray<T> {
    inner: *const InnerArray<T>,
}

unsafe impl<T: Send> Send for AtomicArray<T> {}
unsafe impl<T: Send> Sync for AtomicArray<T> {}

impl<T> AtomicArray<T> {
    /// Create new array with default capacity
    #[inline]
    pub fn new() -> Self {
        Self::with_capacity(DEFAULT_ARRAY_CAP)
    }

    /// Create new array with specific capacity
    pub fn with_capacity(capacity: usize) -> Self {
        assert!(capacity > 0, "Capacity must be greater than 0");

        let inner = InnerArray {
            storage: UnsafeCell::new(ArrayStorage {
                slots: Self::allocate_slots(capacity),
                capacity,
            }),
            head: CachePadded::new(AtomicUsize::new(0)),
            tail: CachePadded::new(AtomicUsize::new(0)),
            len: CachePadded::new(AtomicUsize::new(0)),
            coord_lock: RawMutex::new(),
            ref_count: CachePadded::new(AtomicUsize::new(1)),
        };

        Self {
            inner: Box::into_raw(Box::new(inner)),
        }
    }

    /// Initialize array with given capacity and initializer function
    pub fn init_with<F: FnMut() -> T>(cap: usize, mut initializer: F) -> Self {
        let arr = Self::with_capacity(cap);
        for _ in 0..cap {
            let _ = arr.push(initializer());
        }
        arr
    }

    #[inline(always)]
    fn inner(&self) -> &InnerArray<T> {
        unsafe { &*self.inner }
    }

    #[inline(always)]
    unsafe fn storage(inner: &InnerArray<T>) -> &ArrayStorage<T> {
        unsafe { &*inner.storage.get() }
    }

    fn allocate_slots(capacity: usize) -> *mut Slot<T> {
        let layout = std::alloc::Layout::from_size_align(
            capacity * mem::size_of::<Slot<T>>(),
            mem::align_of::<Slot<T>>(),
        )
        .expect("Failed to create layout");

        unsafe {
            let ptr = std::alloc::alloc_zeroed(layout) as *mut Slot<T>;
            if ptr.is_null() {
                std::alloc::handle_alloc_error(layout);
            }
            for i in 0..capacity {
                ptr.add(i).write(Slot::new());
            }
            ptr
        }
    }

    unsafe fn deallocate_slots(slots: *mut Slot<T>, capacity: usize) {
        let layout = std::alloc::Layout::from_size_align(
            capacity * mem::size_of::<Slot<T>>(),
            mem::align_of::<Slot<T>>(),
        )
        .expect("Failed to create layout");
        unsafe {
            std::alloc::dealloc(slots as *mut u8, layout);
        }
    }

    #[inline(always)]
    pub fn len(&self) -> usize {
        self.inner().len.load(Ordering::Relaxed)
    }

    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline(always)]
    pub fn capacity(&self) -> usize {
        let inner = self.inner();
        let _coord = ArrayCoordGuard::shared(&inner.coord_lock);
        unsafe { Self::storage(inner).capacity }
    }

    /// Push a new element (fast path)
    #[inline]
    pub fn push(&self, value: T) -> Result<(), T> {
        let inner = self.inner();
        let _coord = ArrayCoordGuard::shared(&inner.coord_lock);
        let storage = unsafe { Self::storage(inner) };
        let tail = inner.tail.load(Ordering::Relaxed);
        if tail >= storage.capacity {
            return Err(value);
        }

        if inner
            .tail
            .compare_exchange(tail, tail + 1, Ordering::Release, Ordering::Acquire)
            .is_ok()
        {
            unsafe {
                let slot = &*storage.slots.add(tail);
                ptr::write(slot.value.get(), MaybeUninit::new(value));
                slot.state.store(WRITE, Ordering::Release);
                inner.len.fetch_add(1, Ordering::Relaxed);
            }
            return Ok(());
        }

        self.push_slow(inner, value)
    }

    /// Slow path push using backoff
    #[cold]
    fn push_slow(&self, inner: &InnerArray<T>, value: T) -> Result<(), T> {
        let backoff = Backoff::new();
        let mut tail = inner.tail.load(Ordering::Relaxed);
        let storage = unsafe { Self::storage(inner) };

        loop {
            if tail >= storage.capacity {
                return Err(value);
            }
            match inner
                .tail
                .compare_exchange(tail, tail + 1, Ordering::Release, Ordering::Acquire)
            {
                Ok(_) => {
                    unsafe {
                        let slot = &*storage.slots.add(tail);
                        ptr::write(slot.value.get(), MaybeUninit::new(value));
                        slot.state.store(WRITE, Ordering::Release);
                        inner.len.fetch_add(1, Ordering::Relaxed);
                    }
                    return Ok(());
                }
                Err(t) => {
                    tail = t;
                    backoff.snooze();
                }
            }
        }
    }

    /// Access element by index (shared)
    #[inline]
    pub fn get(&self, index: usize) -> Option<WatchGuardRef<'_, T>> {
        let inner = self.inner();
        let coord = ArrayCoordGuard::shared(&inner.coord_lock);
        let storage = unsafe { Self::storage(inner) };
        let len = inner.len.load(Ordering::Relaxed);
        if index >= len {
            return None;
        }

        let target = inner.head.load(Ordering::Relaxed) + index;
        if target >= storage.capacity {
            return None;
        }

        unsafe {
            let slot = &*storage.slots.add(target);
            slot.wait_write();
            slot.lock.lock_shared();
            let guard = WatchGuardRef::new((*slot.value.get()).assume_init_ref(), &slot.lock);
            drop(coord);
            Some(guard)
        }
    }

    /// Executes a closure while holding a shared guard on the element at `index`.
    #[inline]
    pub fn with<R>(&self, index: usize, f: impl FnOnce(&T) -> R) -> Option<R> {
        self.get(index).map(|guard| f(&guard))
    }

    /// Access element by index (mutable/exclusive)
    #[inline]
    pub fn get_mut(&self, index: usize) -> Option<WatchGuardMut<'_, T>> {
        let inner = self.inner();
        let coord = ArrayCoordGuard::shared(&inner.coord_lock);
        let storage = unsafe { Self::storage(inner) };
        let len = inner.len.load(Ordering::Relaxed);
        if index >= len {
            return None;
        }

        let target = inner.head.load(Ordering::Relaxed) + index;
        if target >= storage.capacity {
            return None;
        }

        unsafe {
            let slot = &*storage.slots.add(target);
            slot.wait_write();
            slot.lock.lock_exclusive();
            let guard = WatchGuardMut::new((*slot.value.get()).assume_init_mut(), &slot.lock);
            drop(coord);
            Some(guard)
        }
    }

    /// Executes a closure while holding an exclusive guard on the element at `index`.
    #[inline]
    pub fn with_mut<R>(&self, index: usize, f: impl FnOnce(&mut T) -> R) -> Option<R> {
        self.get_mut(index).map(|mut guard| f(&mut guard))
    }

    /// Reset array with new capacity and initializer
    pub fn reset_with(
        &self,
        new_cap: usize,
        mut initializer: impl FnMut() -> T,
    ) -> Result<usize, usize> {
        assert!(new_cap > 0, "Capacity must be greater than 0");

        let inner_ptr = self.inner as *mut InnerArray<T>;
        let _coord = unsafe { ArrayCoordGuard::exclusive(&(*inner_ptr).coord_lock) };
        let storage = unsafe { &mut *(*inner_ptr).storage.get() };
        let old_capacity = storage.capacity;
        let old_len = unsafe { (*inner_ptr).len.load(Ordering::Relaxed) };
        let old_head = unsafe { (*inner_ptr).head.load(Ordering::Relaxed) };
        let old_slots = storage.slots;

        unsafe {
            for i in 0..old_len {
                let idx = old_head + i;
                if idx >= old_capacity {
                    continue;
                }

                let slot = &*old_slots.add(idx);
                if !slot.is_written() {
                    continue;
                }

                slot.lock.lock_exclusive();
                if mem::needs_drop::<T>() {
                    ptr::drop_in_place((*slot.value.get()).as_mut_ptr());
                }
                slot.reset();
                slot.lock.unlock_exclusive();
            }

            for i in 0..old_capacity {
                ptr::drop_in_place(old_slots.add(i));
            }
        }
        unsafe { Self::deallocate_slots(old_slots, old_capacity) };

        unsafe {
            let slots = Self::allocate_slots(new_cap);
            storage.slots = slots;
            storage.capacity = new_cap;
            (*inner_ptr).head.store(0, Ordering::Relaxed);
            (*inner_ptr).tail.store(0, Ordering::Relaxed);
            (*inner_ptr).len.store(0, Ordering::Relaxed);

            for i in 0..new_cap {
                let value = initializer();
                let slot = &*slots.add(i);
                ptr::write(slot.value.get(), MaybeUninit::new(value));
                slot.state.store(WRITE, Ordering::Release);
            }

            (*inner_ptr).tail.store(new_cap, Ordering::Relaxed);
            (*inner_ptr).len.store(new_cap, Ordering::Relaxed);
        }

        Ok(new_cap)
    }

    /// Convert to Vec<T> (requires Clone)
    pub fn as_vec(&self) -> Vec<T>
    where
        T: Clone,
    {
        let inner = self.inner();
        let _coord = ArrayCoordGuard::shared(&inner.coord_lock);
        let storage = unsafe { Self::storage(inner) };
        let len = inner.len.load(Ordering::Relaxed);
        if len == 0 {
            return Vec::new();
        }

        let mut out = Vec::with_capacity(len);
        let head = inner.head.load(Ordering::Relaxed);

        unsafe {
            for i in 0..len {
                let target = head + i;
                if target < storage.capacity {
                    let slot = &*storage.slots.add(target);
                    slot.wait_write();
                    slot.lock.lock_shared();
                    out.push((*slot.value.get()).assume_init_ref().clone());
                    slot.lock.unlock_shared();
                }
            }
        }

        out
    }

    /// Apply shared function to each element
    #[inline]
    pub fn for_each<F>(&self, mut f: F)
    where
        F: FnMut(&T),
    {
        let inner = self.inner();
        let _coord = ArrayCoordGuard::shared(&inner.coord_lock);
        let storage = unsafe { Self::storage(inner) };
        let len = inner.len.load(Ordering::Relaxed);
        let head = inner.head.load(Ordering::Relaxed);

        unsafe {
            for i in 0..len {
                let slot = &*storage.slots.add(head + i);
                slot.wait_write();
                slot.lock.lock_shared();
                f((*slot.value.get()).assume_init_ref());
                slot.lock.unlock_shared();
            }
        }
    }

    /// Apply exclusive mutable function to each element
    #[inline]
    pub fn for_each_mut<F>(&self, mut f: F)
    where
        F: FnMut(&mut T),
    {
        let inner = self.inner();
        let _coord = ArrayCoordGuard::shared(&inner.coord_lock);
        let storage = unsafe { Self::storage(inner) };
        let len = inner.len.load(Ordering::Relaxed);
        let head = inner.head.load(Ordering::Relaxed);

        unsafe {
            for i in 0..len {
                let slot = &*storage.slots.add(head + i);
                slot.wait_write();
                slot.lock.lock_exclusive();
                f((*slot.value.get()).assume_init_mut());
                slot.lock.unlock_exclusive();
            }
        }
    }

    /// Split indices into chunks for parallel processing
    #[inline]
    pub fn chunk_indices(&self, num_chunks: usize) -> Vec<(usize, usize)> {
        let len = self.len();
        if len == 0 || num_chunks == 0 {
            return vec![];
        }

        let chunk_size = (len + num_chunks - 1) / num_chunks;
        let mut chunks = Vec::with_capacity(num_chunks);
        for i in 0..num_chunks {
            let start = i * chunk_size;
            let end = ((i + 1) * chunk_size).min(len);
            if start < len {
                chunks.push((start, end));
            }
        }
        chunks
    }
}

impl<T> Clone for AtomicArray<T> {
    #[inline]
    fn clone(&self) -> Self {
        self.inner().ref_count.fetch_add(1, Ordering::Relaxed);
        Self { inner: self.inner }
    }
}

impl<T> Drop for AtomicArray<T> {
    fn drop(&mut self) {
        let inner = unsafe { &*self.inner };
        if inner.ref_count.fetch_sub(1, Ordering::Release) != 1 {
            return;
        }
        fence(Ordering::Acquire);

        unsafe {
            let storage = &*inner.storage.get();
            // Drop dei valori contenuti
            if mem::needs_drop::<T>() {
                let len = inner.len.load(Ordering::Acquire);
                let head = inner.head.load(Ordering::Acquire);
                for i in 0..len {
                    let idx = head + i;
                    if idx < storage.capacity {
                        let slot = &*storage.slots.add(idx);
                        if slot.is_written() {
                            // FIX: drop il valore T, non MaybeUninit<T>
                            ptr::drop_in_place((*slot.value.get()).as_mut_ptr());
                        }
                    }
                }
            }

            // Drop degli slot (RawMutex, AtomicUsize, etc.)
            for i in 0..storage.capacity {
                ptr::drop_in_place(storage.slots.add(i));
            }

            // Dealloca memoria slot
            let layout = std::alloc::Layout::from_size_align(
                storage.capacity * mem::size_of::<Slot<T>>(),
                mem::align_of::<Slot<T>>(),
            )
            .expect("Failed to create layout");
            std::alloc::dealloc(storage.slots as *mut u8, layout);

            // Drop InnerArray
            drop(Box::from_raw(self.inner as *mut InnerArray<T>));
        }
    }
}

impl<T> FromIterator<T> for AtomicArray<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let iter = iter.into_iter();
        let capacity = iter.size_hint().0.max(DEFAULT_ARRAY_CAP);
        let arr = Self::with_capacity(capacity);
        for item in iter {
            let _ = arr.push(item);
        }
        arr
    }
}

impl<T> Default for AtomicArray<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: fmt::Debug> fmt::Debug for AtomicArray<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AtomicArray")
            .field("len", &self.len())
            .field("capacity", &self.capacity())
            .finish()
    }
}