Skip to content

Commit 061f11f

Browse files
LuthafHaoZeke
authored andcommitted
Error out if we are unable to lock a RwLock/Mutex
1 parent 1b957ec commit 061f11f

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/ndarray/sync.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ where
7373
fn try_from(ReadWrite(array): ReadWrite<Arc<RwLock<Array<T, D>>>>) -> Result<Self, Self::Error> {
7474
let ctx = RwLockCtxWriteBuilder {
7575
array: array,
76-
lock_builder: move |array| { array.write().expect("could not lock the rwlock") },
76+
lock_builder: move |array| { array.try_write().expect("could not lock the rwlock") },
7777
shape: vec![],
7878
strides: vec![],
7979
};
@@ -146,7 +146,7 @@ where
146146
fn try_from(ReadOnly(array): ReadOnly<Arc<RwLock<Array<T, D>>>>) -> Result<Self, Self::Error> {
147147
let ctx = RwLockCtxReadBuilder {
148148
array: array,
149-
lock_builder: move |array| { array.read().expect("could not lock the rwlock") },
149+
lock_builder: move |array| { array.try_read().expect("could not lock the rwlock") },
150150
shape: vec![],
151151
strides: vec![],
152152
};
@@ -237,7 +237,7 @@ where
237237
fn try_from(array: Arc<Mutex<Array<T, D>>>) -> Result<Self, Self::Error> {
238238
let ctx = MutexCtxBuilder {
239239
array: array,
240-
lock_builder: move |array| { array.lock().expect("could not lock the mutex") },
240+
lock_builder: move |array| { array.try_lock().expect("could not lock the mutex") },
241241
shape: vec![],
242242
strides: vec![],
243243
};
@@ -319,7 +319,7 @@ mod tests {
319319
view[[1, 1]] = 42.0;
320320
}
321321

322-
let array = array.lock().unwrap();
322+
let array = array.try_lock().unwrap();
323323
assert_eq!(*array, arr2(&[[1.0, 2.0, 3.0], [4.0, 42.0, 6.0]]));
324324
}
325325

@@ -338,7 +338,7 @@ mod tests {
338338
view[[1, 1]] = 42.0;
339339
}
340340

341-
let array = array.read().unwrap();
341+
let array = array.try_read().unwrap();
342342
assert_eq!(*array, arr2(&[[1.0, 2.0, 3.0], [4.0, 42.0, 6.0]]));
343343
}
344344

src/sync.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<T> TryFrom<Arc<Mutex<Vec<T>>>> for DLPackTensor where T: GetDLPackDataType
5252
fn try_from(array: Arc<Mutex<Vec<T>>>) -> Result<DLPackTensor, Self::Error> {
5353
let ctx = MutexCtxBuilder {
5454
array: array,
55-
lock_builder: |array| { array.lock().expect("could not lock the mutex") },
55+
lock_builder: |array| { array.try_lock().expect("could not lock the mutex") },
5656
shape: Box::new(0),
5757
stride: Box::new(1),
5858
};
@@ -152,7 +152,7 @@ impl<T> TryFrom<ReadWrite<Arc<RwLock<Vec<T>>>>> for DLPackTensor where T: GetDLP
152152
fn try_from(ReadWrite(array): ReadWrite<Arc<RwLock<Vec<T>>>>) -> Result<DLPackTensor, Self::Error> {
153153
let ctx = RwLockCtxWriteBuilder {
154154
array: array,
155-
lock_builder: move |array| { array.write().expect("could not lock the rwlock") },
155+
lock_builder: move |array| { array.try_write().expect("could not lock the rwlock") },
156156
shape: Box::new(0),
157157
stride: Box::new(1),
158158
};
@@ -214,7 +214,7 @@ impl<T> TryFrom<ReadOnly<Arc<RwLock<Vec<T>>>>> for DLPackTensor where T: GetDLPa
214214
fn try_from(ReadOnly(array): ReadOnly<Arc<RwLock<Vec<T>>>>) -> Result<DLPackTensor, Self::Error> {
215215
let ctx = RwLockCtxReadBuilder {
216216
array: array,
217-
lock_builder: move |array| { array.read().expect("could not lock the rwlock") },
217+
lock_builder: move |array| { array.try_read().expect("could not lock the rwlock") },
218218
shape: Box::new(0),
219219
stride: Box::new(1),
220220
};
@@ -287,7 +287,7 @@ mod tests {
287287
slice[1] = 42;
288288
}
289289

290-
let lock = data.lock().unwrap();
290+
let lock = data.try_lock().unwrap();
291291
assert_eq!(&*lock, &[1, 42, 3]);
292292
}
293293

@@ -306,7 +306,7 @@ mod tests {
306306
slice[1] = 42;
307307
}
308308

309-
let lock = data.read().unwrap();
309+
let lock = data.try_read().unwrap();
310310
assert_eq!(&*lock, &[1, 42, 3]);
311311
}
312312

@@ -324,7 +324,7 @@ mod tests {
324324
assert_eq!(slice, &[1, 2, 3]);
325325
}
326326

327-
let lock = data.read().unwrap();
327+
let lock = data.try_read().unwrap();
328328
assert_eq!(&*lock, &[1, 2, 3]);
329329
}
330330

0 commit comments

Comments
 (0)