Skip to content

Commit b7dd6a2

Browse files
committed
Reworking the lifecycle management of rcl bindings
Signed-off-by: Michael X. Grey <[email protected]>
1 parent 0578a76 commit b7dd6a2

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

rclrs/src/context.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ unsafe impl Send for rcl_context_t {}
3939
/// - the allocator used (left as the default by `rclrs`)
4040
///
4141
pub struct Context {
42-
pub(crate) rcl_context_mtx: Arc<Mutex<rcl_context_t>>,
42+
pub(crate) handle: Arc<ContextHandle>,
43+
}
44+
45+
/// This struct manages the lifetime and access to the rcl context. It will also
46+
/// account for the lifetimes of any dependencies, if we need to add
47+
/// dependencies in the future (currently there are none). It is not strictly
48+
/// necessary to decompose `Context` and `ContextHandle` like this, but we are
49+
/// doing it to be consistent with the lifecycle management of other rcl
50+
/// bindings in this library.
51+
pub(crate) struct ContextHandle {
52+
handle: Mutex<rcl_context_t>,
4353
}
4454

4555
impl Context {

rclrs/src/node.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::rcl_bindings::*;
1515
use crate::{
1616
Client, ClientBase, Clock, Context, GuardCondition, ParameterBuilder, ParameterInterface,
1717
ParameterVariant, Parameters, Publisher, QoSProfile, RclrsError, Service, ServiceBase,
18-
Subscription, SubscriptionBase, SubscriptionCallback, TimeSource, ToResult,
18+
Subscription, SubscriptionBase, SubscriptionCallback, TimeSource, ToResult, ContextHandle,
1919
};
2020

2121
impl Drop for rcl_node_t {
@@ -71,18 +71,21 @@ pub struct Node {
7171
pub(crate) subscriptions_mtx: Mutex<Vec<Weak<dyn SubscriptionBase>>>,
7272
time_source: TimeSource,
7373
parameter: ParameterInterface,
74-
// Note: it's important to have those last since `drop` will be called in order of declaration
75-
// in the struct and both `TimeSource` and `ParameterInterface` contain subscriptions /
76-
// services that will fail to be dropped if the context or node is destroyed first.
77-
pub(crate) rcl_node_mtx: Arc<Mutex<rcl_node_t>>,
78-
pub(crate) rcl_context_mtx: Arc<Mutex<rcl_context_t>>,
74+
pub(crate) rcl_node: Arc<NodeHandle>,
75+
}
76+
77+
/// This struct manages the lifetime of the rcl node, and accounts for its
78+
/// dependency on the lifetime of its context.
79+
pub(crate) struct NodeHandle {
80+
handle: Mutex<rcl_node_t>,
81+
rcl_context: Arc<ContextHandle>,
7982
}
8083

8184
impl Eq for Node {}
8285

8386
impl PartialEq for Node {
8487
fn eq(&self, other: &Self) -> bool {
85-
Arc::ptr_eq(&self.rcl_node_mtx, &other.rcl_node_mtx)
88+
Arc::ptr_eq(&self.rcl_node, &other.rcl_node)
8689
}
8790
}
8891

@@ -182,7 +185,7 @@ impl Node {
182185
&self,
183186
getter: unsafe extern "C" fn(*const rcl_node_t) -> *const c_char,
184187
) -> String {
185-
unsafe { call_string_getter_with_handle(&self.rcl_node_mtx.lock().unwrap(), getter) }
188+
unsafe { call_string_getter_with_handle(&self.rcl_node.handle.lock().unwrap(), getter) }
186189
}
187190

188191
/// Creates a [`Client`][1].

0 commit comments

Comments
 (0)