|
| 1 | +use std::cell::UnsafeCell; |
| 2 | +use std::collections::hash_map::{Entry, HashMap}; |
| 3 | +use std::ops::{Deref, DerefMut}; |
| 4 | + |
| 5 | +use ndarray::{ArrayView, ArrayViewMut, Dimension}; |
| 6 | +use pyo3::{FromPyObject, PyAny, PyResult}; |
| 7 | + |
| 8 | +use crate::array::PyArray; |
| 9 | +use crate::dtype::Element; |
| 10 | + |
| 11 | +thread_local! { |
| 12 | + static BORROW_FLAGS: UnsafeCell<HashMap<usize, isize>> = UnsafeCell::new(HashMap::new()); |
| 13 | +} |
| 14 | + |
| 15 | +pub struct PyArrayRef<'a, T, D> { |
| 16 | + array: &'a PyArray<T, D>, |
| 17 | + view: ArrayView<'a, T, D>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'a, T, D> Deref for PyArrayRef<'a, T, D> { |
| 21 | + type Target = ArrayView<'a, T, D>; |
| 22 | + |
| 23 | + fn deref(&self) -> &Self::Target { |
| 24 | + &self.view |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl<'py, T: Element, D: Dimension> FromPyObject<'py> for PyArrayRef<'py, T, D> { |
| 29 | + fn extract(obj: &'py PyAny) -> PyResult<Self> { |
| 30 | + let array: &'py PyArray<T, D> = obj.extract()?; |
| 31 | + Ok(array.as_array()) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<'a, T, D> PyArrayRef<'a, T, D> |
| 36 | +where |
| 37 | + T: Element, |
| 38 | + D: Dimension, |
| 39 | +{ |
| 40 | + pub(crate) fn try_new(array: &'a PyArray<T, D>) -> Option<Self> { |
| 41 | + let address = array as *const PyArray<T, D> as usize; |
| 42 | + |
| 43 | + BORROW_FLAGS.with(|borrow_flags| { |
| 44 | + // SAFETY: Called on a thread local variable in a leaf function. |
| 45 | + let borrow_flags = unsafe { &mut *borrow_flags.get() }; |
| 46 | + |
| 47 | + match borrow_flags.entry(address) { |
| 48 | + Entry::Occupied(entry) => { |
| 49 | + let readers = entry.into_mut(); |
| 50 | + |
| 51 | + let new_readers = readers.wrapping_add(1); |
| 52 | + |
| 53 | + if new_readers <= 0 { |
| 54 | + cold(); |
| 55 | + return None; |
| 56 | + } |
| 57 | + |
| 58 | + *readers = new_readers; |
| 59 | + } |
| 60 | + Entry::Vacant(entry) => { |
| 61 | + entry.insert(1); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // SAFETY: Thread-local borrow flags ensure aliasing discipline on this thread, |
| 66 | + // and `PyArray` is neither `Send` nor `Sync` |
| 67 | + let view = unsafe { array.as_array_unchecked() }; |
| 68 | + |
| 69 | + Some(Self { array, view }) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<'a, T, D> Drop for PyArrayRef<'a, T, D> { |
| 75 | + fn drop(&mut self) { |
| 76 | + let address = self.array as *const PyArray<T, D> as usize; |
| 77 | + |
| 78 | + BORROW_FLAGS.with(|borrow_flags| { |
| 79 | + // SAFETY: Called on a thread local variable in a leaf function. |
| 80 | + let borrow_flags = unsafe { &mut *borrow_flags.get() }; |
| 81 | + |
| 82 | + let readers = borrow_flags.get_mut(&address).unwrap(); |
| 83 | + |
| 84 | + *readers -= 1; |
| 85 | + |
| 86 | + if *readers == 0 { |
| 87 | + borrow_flags.remove(&address).unwrap(); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +pub struct PyArrayRefMut<'a, T, D> { |
| 94 | + array: &'a PyArray<T, D>, |
| 95 | + view: ArrayViewMut<'a, T, D>, |
| 96 | +} |
| 97 | + |
| 98 | +impl<'a, T, D> Deref for PyArrayRefMut<'a, T, D> { |
| 99 | + type Target = ArrayViewMut<'a, T, D>; |
| 100 | + |
| 101 | + fn deref(&self) -> &Self::Target { |
| 102 | + &self.view |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl<'a, T, D> DerefMut for PyArrayRefMut<'a, T, D> { |
| 107 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 108 | + &mut self.view |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl<'py, T: Element, D: Dimension> FromPyObject<'py> for PyArrayRefMut<'py, T, D> { |
| 113 | + fn extract(obj: &'py PyAny) -> PyResult<Self> { |
| 114 | + let array: &'py PyArray<T, D> = obj.extract()?; |
| 115 | + Ok(array.as_array_mut()) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<'a, T, D> PyArrayRefMut<'a, T, D> |
| 120 | +where |
| 121 | + T: Element, |
| 122 | + D: Dimension, |
| 123 | +{ |
| 124 | + pub(crate) fn try_new(array: &'a PyArray<T, D>) -> Option<Self> { |
| 125 | + let address = array as *const PyArray<T, D> as usize; |
| 126 | + |
| 127 | + BORROW_FLAGS.with(|borrow_flags| { |
| 128 | + // SAFETY: Called on a thread local variable in a leaf function. |
| 129 | + let borrow_flags = unsafe { &mut *borrow_flags.get() }; |
| 130 | + |
| 131 | + match borrow_flags.entry(address) { |
| 132 | + Entry::Occupied(entry) => { |
| 133 | + let writers = entry.into_mut(); |
| 134 | + |
| 135 | + if *writers != 0 { |
| 136 | + cold(); |
| 137 | + return None; |
| 138 | + } |
| 139 | + |
| 140 | + *writers = -1; |
| 141 | + } |
| 142 | + Entry::Vacant(entry) => { |
| 143 | + entry.insert(-1); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // SAFETY: Thread-local borrow flags ensure aliasing discipline on this thread, |
| 148 | + // and `PyArray` is neither `Send` nor `Sync` |
| 149 | + let view = unsafe { array.as_array_mut_unchecked() }; |
| 150 | + |
| 151 | + Some(Self { array, view }) |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +impl<'a, T, D> Drop for PyArrayRefMut<'a, T, D> { |
| 157 | + fn drop(&mut self) { |
| 158 | + let address = self.array as *const PyArray<T, D> as usize; |
| 159 | + |
| 160 | + BORROW_FLAGS.with(|borrow_flags| { |
| 161 | + // SAFETY: Called on a thread local variable in a leaf function. |
| 162 | + let borrow_flags = unsafe { &mut *borrow_flags.get() }; |
| 163 | + |
| 164 | + borrow_flags.remove(&address).unwrap(); |
| 165 | + }); |
| 166 | + } |
| 167 | +} |
| 168 | +#[cold] |
| 169 | +#[inline(always)] |
| 170 | +fn cold() {} |
0 commit comments