Skip to content

Commit af71418

Browse files
committed
Use rcl-allocated goal handle pointer in ServerGoalHandle
The `rcl_action_accept_new_goal()` function returns a pre-allocated `rcl_action_goal_handle_t` pointer, which is also stored within the action server proper. This means we cannot store a non-pointer version of this in the `ServerGoalHandle`. Instead, we'll keep a mutex-guarded mutable pointer. The `Arc` is unnecessary since this pointer is never shared with anyone. Also, we need to clean up the goal handle by calling `rcl_action_goal_handle_fini()` when the `ServerGoalHandle` is dropped.
1 parent 5bfe921 commit af71418

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

rclrs/src/action/server_goal_handle.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct ServerGoalHandle<ActionT>
3030
where
3131
ActionT: rosidl_runtime_rs::Action,
3232
{
33-
rcl_handle: Arc<Mutex<rcl_action_goal_handle_t>>,
33+
rcl_handle: Mutex<*mut rcl_action_goal_handle_t>,
3434
goal_request: Arc<ActionT::Goal>,
3535
uuid: GoalUuid,
3636
}
@@ -40,12 +40,12 @@ where
4040
ActionT: rosidl_runtime_rs::Action,
4141
{
4242
pub(crate) fn new(
43-
rcl_handle: Arc<Mutex<rcl_action_goal_handle_t>>,
43+
rcl_handle: *mut rcl_action_goal_handle_t,
4444
goal_request: Arc<ActionT::Goal>,
4545
uuid: GoalUuid,
4646
) -> Self {
4747
Self {
48-
rcl_handle,
48+
rcl_handle: Mutex::new(rcl_handle),
4949
goal_request: Arc::clone(&goal_request),
5050
uuid,
5151
}
@@ -57,7 +57,7 @@ where
5757
{
5858
let rcl_handle = self.rcl_handle.lock().unwrap();
5959
// SAFETY: The provided goal handle is properly initialized by construction.
60-
unsafe { rcl_action_goal_handle_get_status(&*rcl_handle, &mut state).ok()? }
60+
unsafe { rcl_action_goal_handle_get_status(*rcl_handle, &mut state).ok()? }
6161
}
6262
// SAFETY: state is initialized to a valid GoalStatus value and will only ever by set by
6363
// rcl_action_goal_handle_get_status to a valid GoalStatus value.
@@ -74,7 +74,7 @@ where
7474
pub fn is_active(&self) -> bool {
7575
let rcl_handle = self.rcl_handle.lock().unwrap();
7676
// SAFETY: The provided goal handle is properly initialized by construction.
77-
unsafe { rcl_action_goal_handle_is_active(&*rcl_handle) }
77+
unsafe { rcl_action_goal_handle_is_active(*rcl_handle) }
7878
}
7979

8080
/// Returns whether the goal is executing.
@@ -86,7 +86,7 @@ where
8686
fn update_state(&self, event: rcl_action_goal_event_t) -> Result<(), RclrsError> {
8787
let mut rcl_handle = self.rcl_handle.lock().unwrap();
8888
// SAFETY: The provided goal handle is properly initialized by construction.
89-
unsafe { rcl_action_update_goal_state(&mut *rcl_handle, event).ok() }
89+
unsafe { rcl_action_update_goal_state(*rcl_handle, event).ok() }
9090
}
9191

9292
/// Indicate that the goal could not be reached and has been aborted.
@@ -147,7 +147,7 @@ where
147147

148148
// If the goal is in a cancelable state, transition to canceling.
149149
// SAFETY: The provided goal handle is properly initialized by construction.
150-
let is_cancelable = unsafe { rcl_action_goal_handle_is_cancelable(&*rcl_handle) };
150+
let is_cancelable = unsafe { rcl_action_goal_handle_is_cancelable(*rcl_handle) };
151151
if is_cancelable {
152152
self.update_state(rcl_action_goal_event_t::GOAL_EVENT_CANCEL_GOAL)?;
153153
}
@@ -191,5 +191,11 @@ where
191191
if self.try_canceling() == Ok(true) {
192192
// TODO: Invoke on_terminal_state callback
193193
}
194+
{
195+
let rcl_handle = self.rcl_handle.lock().unwrap();
196+
// SAFETY: The provided goal handle is properly initialized by construction. It will
197+
// not be accessed beyond this point.
198+
unsafe { rcl_action_goal_handle_fini(*rcl_handle); }
199+
}
194200
}
195201
}

0 commit comments

Comments
 (0)