Skip to content

Commit 2cf41fb

Browse files
committed
Replace parking_lot Mutex with std::sync::Mutex
1 parent 214470a commit 2cf41fb

File tree

11 files changed

+51
-65
lines changed

11 files changed

+51
-65
lines changed

rclrs/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ path = "src/lib.rs"
1515
[dependencies]
1616
# Needed for FFI
1717
libc = "0.2.43"
18-
# Provides better concurrency primitives than std
19-
parking_lot = "0.11.2"
2018
# Needed for the Message trait, among others
2119
rosidl_runtime_rs = "0.2.0"
2220
# Needed for clients

rclrs/src/context.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ use crate::{RclrsError, ToResult};
44
use std::ffi::CString;
55
use std::os::raw::c_char;
66
use std::string::String;
7-
use std::sync::Arc;
7+
use std::sync::{Arc, Mutex};
88
use std::vec::Vec;
99

10-
use parking_lot::Mutex;
11-
1210
impl Drop for rcl_context_t {
1311
fn drop(&mut self) {
1412
unsafe {
@@ -112,7 +110,7 @@ impl Context {
112110
pub fn ok(&self) -> bool {
113111
// This will currently always return true, but once we have a signal handler, the signal
114112
// handler could call `rcl_shutdown()`, hence making the context invalid.
115-
let rcl_context = &mut *self.rcl_context_mtx.lock();
113+
let rcl_context = &mut *self.rcl_context_mtx.lock().unwrap();
116114
// SAFETY: No preconditions for this function.
117115
unsafe { rcl_context_is_valid(rcl_context) }
118116
}

rclrs/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ pub fn spin(node: &Node) -> Result<(), RclrsError> {
8989
// The context_is_valid functions exists only to abstract away ROS distro differences
9090
#[cfg(ros_distro = "foxy")]
9191
// SAFETY: No preconditions for this function.
92-
let context_is_valid = || unsafe { rcl_context_is_valid(&mut *node.rcl_context_mtx.lock()) };
92+
let context_is_valid =
93+
|| unsafe { rcl_context_is_valid(&mut *node.rcl_context_mtx.lock().unwrap()) };
9394
#[cfg(not(ros_distro = "foxy"))]
9495
// SAFETY: No preconditions for this function.
95-
let context_is_valid = || unsafe { rcl_context_is_valid(&*node.rcl_context_mtx.lock()) };
96+
let context_is_valid =
97+
|| unsafe { rcl_context_is_valid(&*node.rcl_context_mtx.lock().unwrap()) };
9698

9799
while context_is_valid() {
98100
match spin_once(node, None) {

rclrs/src/node.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ use crate::{Context, ParameterOverrideMap, QoSProfile, RclrsError, ToResult};
1515
use std::cmp::PartialEq;
1616
use std::ffi::CStr;
1717
use std::fmt;
18-
use std::sync::{Arc, Weak};
18+
use std::sync::{Arc, Mutex, Weak};
1919
use std::vec::Vec;
2020

2121
use libc::c_char;
22-
use parking_lot::Mutex;
23-
2422
use rosidl_runtime_rs::Message;
2523

2624
impl Drop for rcl_node_t {
@@ -177,7 +175,7 @@ impl Node {
177175
&self,
178176
getter: unsafe extern "C" fn(*const rcl_node_t) -> *const c_char,
179177
) -> String {
180-
unsafe { call_string_getter_with_handle(&*self.rcl_node_mtx.lock(), getter) }
178+
unsafe { call_string_getter_with_handle(&*self.rcl_node_mtx.lock().unwrap(), getter) }
181179
}
182180

183181
/// Creates a [`Client`][1].
@@ -291,7 +289,7 @@ impl Node {
291289
// add description about this function is for getting actual domain_id
292290
// and about override of domain_id via node option
293291
pub fn domain_id(&self) -> usize {
294-
let rcl_node = &*self.rcl_node_mtx.lock();
292+
let rcl_node = &*self.rcl_node_mtx.lock().unwrap();
295293
let mut domain_id: usize = 0;
296294
let ret = unsafe {
297295
// SAFETY: No preconditions for this function.

rclrs/src/node/builder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use crate::{
55
};
66

77
use std::ffi::CString;
8-
use std::sync::Arc;
9-
10-
use parking_lot::Mutex;
8+
use std::sync::{Arc, Mutex};
119

1210
/// A builder for creating a [`Node`][1].
1311
///
@@ -245,7 +243,7 @@ impl NodeBuilder {
245243
s: self.namespace.clone(),
246244
})?;
247245
let rcl_node_options = self.create_rcl_node_options()?;
248-
let rcl_context = &mut *self.context.lock();
246+
let rcl_context = &mut *self.context.lock().unwrap();
249247

250248
// SAFETY: Getting a zero-initialized value is always safe.
251249
let mut rcl_node = unsafe { rcl_get_zero_initialized_node() };

rclrs/src/node/client.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ use std::boxed::Box;
33
use std::collections::HashMap;
44
use std::ffi::CString;
55
use std::sync::atomic::AtomicBool;
6-
use std::sync::Arc;
6+
use std::sync::{Arc, Mutex, MutexGuard};
77

88
use crate::error::{RclReturnCode, ToResult};
99
use crate::MessageCow;
1010
use crate::Node;
1111
use crate::{rcl_bindings::*, RclrsError};
1212

13-
use parking_lot::{Mutex, MutexGuard};
1413
use rosidl_runtime_rs::Message;
1514

1615
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
@@ -26,17 +25,17 @@ pub struct ClientHandle {
2625

2726
impl ClientHandle {
2827
pub(crate) fn lock(&self) -> MutexGuard<rcl_client_t> {
29-
self.rcl_client_mtx.lock()
28+
self.rcl_client_mtx.lock().unwrap()
3029
}
3130
}
3231

3332
impl Drop for ClientHandle {
3433
fn drop(&mut self) {
35-
let handle = self.rcl_client_mtx.get_mut();
36-
let rcl_node_mtx = &mut *self.rcl_node_mtx.lock();
34+
let rcl_client = self.rcl_client_mtx.get_mut().unwrap();
35+
let rcl_node_mtx = &mut *self.rcl_node_mtx.lock().unwrap();
3736
// SAFETY: No preconditions for this function
3837
unsafe {
39-
rcl_client_fini(handle, rcl_node_mtx);
38+
rcl_client_fini(rcl_client, rcl_node_mtx);
4039
}
4140
}
4241
}
@@ -87,7 +86,7 @@ where
8786
err,
8887
s: topic.into(),
8988
})?;
90-
let rcl_node = { &mut *node.rcl_node_mtx.lock() };
89+
let rcl_node = { &mut *node.rcl_node_mtx.lock().unwrap() };
9190

9291
// SAFETY: No preconditions for this function.
9392
let client_options = unsafe { rcl_client_get_default_options() };
@@ -153,7 +152,7 @@ where
153152
)
154153
}
155154
.ok()?;
156-
let requests = &mut *self.requests.lock();
155+
let requests = &mut *self.requests.lock().unwrap();
157156
requests.insert(sequence_number, Box::new(callback));
158157
Ok(())
159158
}
@@ -189,7 +188,7 @@ where
189188
}
190189
.ok()?;
191190
let (tx, rx) = oneshot::channel::<T::Response>();
192-
self.futures.lock().insert(sequence_number, tx);
191+
self.futures.lock().unwrap().insert(sequence_number, tx);
193192
// It is safe to call unwrap() here since the `Canceled` error will only happen when the
194193
// `Sender` is dropped
195194
// https://docs.rs/futures/latest/futures/channel/oneshot/struct.Canceled.html
@@ -261,8 +260,8 @@ where
261260
}
262261
Err(e) => return Err(e),
263262
};
264-
let requests = &mut *self.requests.lock();
265-
let futures = &mut *self.futures.lock();
263+
let requests = &mut *self.requests.lock().unwrap();
264+
let futures = &mut *self.futures.lock().unwrap();
266265
if let Some(callback) = requests.remove(&req_id.sequence_number) {
267266
callback(res);
268267
} else if let Some(future) = futures.remove(&req_id.sequence_number) {

rclrs/src/node/publisher.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use std::borrow::Cow;
77
use std::ffi::CStr;
88
use std::ffi::CString;
99
use std::marker::PhantomData;
10-
use std::sync::Arc;
11-
12-
use parking_lot::Mutex;
10+
use std::sync::{Arc, Mutex};
1311

1412
use rosidl_runtime_rs::{Message, RmwMessage};
1513

@@ -44,8 +42,8 @@ where
4442
unsafe {
4543
// SAFETY: No preconditions for this function (besides the arguments being valid).
4644
rcl_publisher_fini(
47-
self.rcl_publisher_mtx.get_mut(),
48-
&mut *self.rcl_node_mtx.lock(),
45+
self.rcl_publisher_mtx.get_mut().unwrap(),
46+
&mut *self.rcl_node_mtx.lock().unwrap(),
4947
);
5048
}
5149
}
@@ -70,7 +68,7 @@ where
7068
err,
7169
s: topic.into(),
7270
})?;
73-
let rcl_node = &mut *node.rcl_node_mtx.lock();
71+
let rcl_node = &mut *node.rcl_node_mtx.lock().unwrap();
7472

7573
// SAFETY: No preconditions for this function.
7674
let mut publisher_options = unsafe { rcl_publisher_get_default_options() };
@@ -106,7 +104,8 @@ where
106104
// SAFETY: No preconditions for the functions called.
107105
// The unsafe variables created get converted to safe types before being returned
108106
unsafe {
109-
let raw_topic_pointer = rcl_publisher_get_topic_name(&*self.rcl_publisher_mtx.lock());
107+
let raw_topic_pointer =
108+
rcl_publisher_get_topic_name(&*self.rcl_publisher_mtx.lock().unwrap());
110109
CStr::from_ptr(raw_topic_pointer)
111110
.to_string_lossy()
112111
.into_owned()
@@ -131,7 +130,7 @@ where
131130
/// [1]: https://github.com/ros2/ros2/issues/255
132131
pub fn publish<'a, M: MessageCow<'a, T>>(&self, message: M) -> Result<(), RclrsError> {
133132
let rmw_message = T::into_rmw_message(message.into_cow());
134-
let rcl_publisher = &mut *self.rcl_publisher_mtx.lock();
133+
let rcl_publisher = &mut *self.rcl_publisher_mtx.lock().unwrap();
135134
unsafe {
136135
// SAFETY: The message type is guaranteed to match the publisher type by the type system.
137136
// The message does not need to be valid beyond the duration of this function call.

rclrs/src/node/service.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::boxed::Box;
22
use std::ffi::CString;
33
use std::sync::atomic::AtomicBool;
4-
use std::sync::Arc;
4+
use std::sync::{Arc, Mutex, MutexGuard};
55

66
use crate::error::{RclReturnCode, ToResult};
77
use crate::Node;
@@ -11,32 +11,30 @@ use rosidl_runtime_rs::Message;
1111

1212
use crate::node::publisher::MessageCow;
1313

14-
use parking_lot::{Mutex, MutexGuard};
15-
1614
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
1715
// they are running in. Therefore, this type can be safely sent to another thread.
1816
unsafe impl Send for rcl_service_t {}
1917

2018
/// Internal struct used by services.
2119
pub struct ServiceHandle {
22-
handle: Mutex<rcl_service_t>,
23-
node_handle: Arc<Mutex<rcl_node_t>>,
20+
rcl_service_mtx: Mutex<rcl_service_t>,
21+
rcl_node_mtx: Arc<Mutex<rcl_node_t>>,
2422
pub(crate) in_use_by_wait_set: Arc<AtomicBool>,
2523
}
2624

2725
impl ServiceHandle {
2826
pub(crate) fn lock(&self) -> MutexGuard<rcl_service_t> {
29-
self.handle.lock()
27+
self.rcl_service_mtx.lock().unwrap()
3028
}
3129
}
3230

3331
impl Drop for ServiceHandle {
3432
fn drop(&mut self) {
35-
let handle = self.handle.get_mut();
36-
let node_handle = &mut *self.node_handle.lock();
33+
let rcl_service = self.rcl_service_mtx.get_mut().unwrap();
34+
let rcl_node = &mut *self.rcl_node_mtx.lock().unwrap();
3735
// SAFETY: No preconditions for this function
3836
unsafe {
39-
rcl_service_fini(handle, node_handle);
37+
rcl_service_fini(rcl_service, rcl_node);
4038
}
4139
}
4240
}
@@ -80,14 +78,14 @@ where
8078
F: Fn(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,
8179
{
8280
// SAFETY: Getting a zero-initialized value is always safe.
83-
let mut service_handle = unsafe { rcl_get_zero_initialized_service() };
81+
let mut rcl_service = unsafe { rcl_get_zero_initialized_service() };
8482
let type_support = <T as rosidl_runtime_rs::Service>::get_type_support()
8583
as *const rosidl_service_type_support_t;
8684
let topic_c_string = CString::new(topic).map_err(|err| RclrsError::StringContainsNul {
8785
err,
8886
s: topic.into(),
8987
})?;
90-
let node_handle = &mut *node.rcl_node_mtx.lock();
88+
let rcl_node = &mut *node.rcl_node_mtx.lock().unwrap();
9189

9290
// SAFETY: No preconditions for this function.
9391
let service_options = unsafe { rcl_service_get_default_options() };
@@ -98,8 +96,8 @@ where
9896
// The topic name and the options are copied by this function, so they can be dropped
9997
// afterwards.
10098
rcl_service_init(
101-
&mut service_handle as *mut _,
102-
node_handle as *mut _,
99+
&mut rcl_service as *mut _,
100+
rcl_node as *mut _,
103101
type_support,
104102
topic_c_string.as_ptr(),
105103
&service_options as *const _,
@@ -108,8 +106,8 @@ where
108106
}
109107

110108
let handle = Arc::new(ServiceHandle {
111-
handle: Mutex::new(service_handle),
112-
node_handle: node.rcl_node_mtx.clone(),
109+
rcl_service_mtx: Mutex::new(rcl_service),
110+
rcl_node_mtx: node.rcl_node_mtx.clone(),
113111
in_use_by_wait_set: Arc::new(AtomicBool::new(false)),
114112
});
115113

@@ -184,7 +182,7 @@ where
184182
}
185183
Err(e) => return Err(e),
186184
};
187-
let res = (*self.callback.lock())(&req_id, req);
185+
let res = (*self.callback.lock().unwrap())(&req_id, req);
188186
let rmw_message = <T::Response as Message>::into_rmw_message(res.into_cow());
189187
let handle = &*self.handle.lock();
190188
unsafe {

rclrs/src/node/subscription.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ use std::ffi::CStr;
88
use std::ffi::CString;
99
use std::marker::PhantomData;
1010
use std::sync::atomic::AtomicBool;
11-
use std::sync::Arc;
11+
use std::sync::{Arc, Mutex, MutexGuard};
1212

1313
use rosidl_runtime_rs::{Message, RmwMessage};
1414

15-
use parking_lot::{Mutex, MutexGuard};
16-
1715
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
1816
// they are running in. Therefore, this type can be safely sent to another thread.
1917
unsafe impl Send for rcl_subscription_t {}
@@ -27,14 +25,14 @@ pub struct SubscriptionHandle {
2725

2826
impl SubscriptionHandle {
2927
pub(crate) fn lock(&self) -> MutexGuard<rcl_subscription_t> {
30-
self.rcl_subscription_mtx.lock()
28+
self.rcl_subscription_mtx.lock().unwrap()
3129
}
3230
}
3331

3432
impl Drop for SubscriptionHandle {
3533
fn drop(&mut self) {
36-
let rcl_subscription = self.rcl_subscription_mtx.get_mut();
37-
let rcl_node = &mut *self.rcl_node_mtx.lock();
34+
let rcl_subscription = self.rcl_subscription_mtx.get_mut().unwrap();
35+
let rcl_node = &mut *self.rcl_node_mtx.lock().unwrap();
3836
// SAFETY: No preconditions for this function (besides the arguments being valid).
3937
unsafe {
4038
rcl_subscription_fini(rcl_subscription, rcl_node);
@@ -99,7 +97,7 @@ where
9997
err,
10098
s: topic.into(),
10199
})?;
102-
let rcl_node = &mut *node.rcl_node_mtx.lock();
100+
let rcl_node = &mut *node.rcl_node_mtx.lock().unwrap();
103101

104102
// SAFETY: No preconditions for this function.
105103
let mut subscription_options = unsafe { rcl_subscription_get_default_options() };
@@ -210,7 +208,7 @@ where
210208
}
211209
Err(e) => return Err(e),
212210
};
213-
(*self.callback.lock())(msg);
211+
(*self.callback.lock().unwrap())(msg);
214212
Ok(())
215213
}
216214
}

rclrs/src/parameter/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ mod tests {
154154
let mut rcl_params = std::ptr::null_mut();
155155
unsafe {
156156
rcl_arguments_get_param_overrides(
157-
&ctx.rcl_context_mtx.lock().global_arguments,
157+
&ctx.rcl_context_mtx.lock().unwrap().global_arguments,
158158
&mut rcl_params,
159159
)
160160
.ok()?;

0 commit comments

Comments
 (0)