Skip to content

Commit fde0f35

Browse files
Remove Mutex from node clock
Signed-off-by: Luca Della Vedova <[email protected]>
1 parent 19a73c3 commit fde0f35

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

examples/minimal_pub_sub/src/minimal_subscriber.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() -> Result<(), Error> {
1717
num_messages += 1;
1818
println!("I heard: '{}'", msg.data);
1919
println!("(Got {} messages so far)", num_messages);
20-
let now = inner_node.get_clock().lock().unwrap().now();
20+
let now = inner_node.get_clock().now();
2121
dbg!(now);
2222
},
2323
)?;

rclrs/src/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub struct Node {
7171
pub(crate) guard_conditions_mtx: Mutex<Vec<Weak<GuardCondition>>>,
7272
pub(crate) services_mtx: Mutex<Vec<Weak<dyn ServiceBase>>>,
7373
pub(crate) subscriptions_mtx: Mutex<Vec<Weak<dyn SubscriptionBase>>>,
74-
_clock: Arc<Mutex<Clock>>,
74+
_clock: Arc<Clock>,
7575
// TODO(luca) set to private
7676
pub _time_source: Arc<Mutex<Option<TimeSource>>>,
7777
_parameter_map: ParameterOverrideMap,
@@ -103,7 +103,7 @@ impl Node {
103103
}
104104

105105
/// Gets the clock associated with this node.
106-
pub fn get_clock(&self) -> Arc<Mutex<Clock>> {
106+
pub fn get_clock(&self) -> Arc<Clock> {
107107
self._clock.clone()
108108
}
109109

rclrs/src/node/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl NodeBuilder {
283283
guard_conditions_mtx: Mutex::new(vec![]),
284284
services_mtx: Mutex::new(vec![]),
285285
subscriptions_mtx: Mutex::new(vec![]),
286-
_clock: Arc::new(Mutex::new(clock)),
286+
_clock: Arc::new(clock),
287287
_time_source: Arc::new(Mutex::new(None)),
288288
_parameter_map,
289289
};

rclrs/src/time_source.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::sync::{Arc, Mutex};
1212
/// to the `/clock` topic and drive the attached clocks
1313
pub struct TimeSource {
1414
_node: Arc<Node>,
15-
_clocks: Arc<Mutex<Vec<Arc<Mutex<Clock>>>>>,
15+
_clocks: Arc<Mutex<Vec<Arc<Clock>>>>,
1616
_clock_qos: QoSProfile,
1717
// TODO(luca) implement clock threads, for now will run in main thread
1818
_use_clock_thread: bool,
@@ -39,14 +39,14 @@ pub struct TimeSourceBuilder {
3939
node: Arc<Node>,
4040
clock_qos: QoSProfile,
4141
use_clock_thread: bool,
42-
clock: Arc<Mutex<Clock>>,
42+
clock: Arc<Clock>,
4343
}
4444

4545
impl TimeSourceBuilder {
4646
/// Creates a builder for a time source that drives the given clock for the given node.
4747
/// The clock's ClockType must be set to `RosTime` to allow updates based on the `/clock`
4848
/// topic.
49-
pub fn new(node: Arc<Node>, clock: Arc<Mutex<Clock>>) -> Self {
49+
pub fn new(node: Arc<Node>, clock: Arc<Clock>) -> Self {
5050
Self {
5151
node,
5252
clock_qos: QOS_PROFILE_CLOCK,
@@ -102,13 +102,13 @@ impl fmt::Display for ClockMismatchError {
102102

103103
impl TimeSource {
104104
/// Creates a new time source with default parameters.
105-
pub fn new(node: Arc<Node>, clock: Arc<Mutex<Clock>>) -> Self {
105+
pub fn new(node: Arc<Node>, clock: Arc<Clock>) -> Self {
106106
TimeSourceBuilder::new(node, clock).build()
107107
}
108108

109109
/// Attaches the given clock to the `TimeSource`, enabling the `TimeSource` to control it.
110-
pub fn attach_clock(&self, clock: Arc<Mutex<Clock>>) -> Result<(), ClockMismatchError> {
111-
let clock_type = clock.clone().lock().unwrap().clock_type();
110+
pub fn attach_clock(&self, clock: Arc<Clock>) -> Result<(), ClockMismatchError> {
111+
let clock_type = clock.clock_type();
112112
if !matches!(clock_type, ClockType::RosTime) && *self._ros_time_active.lock().unwrap() {
113113
return Err(ClockMismatchError(clock_type));
114114
}
@@ -126,7 +126,7 @@ impl TimeSource {
126126

127127
/// Detaches the given clock from the `TimeSource`.
128128
// TODO(luca) should we return a result to denote whether the clock was removed?
129-
pub fn detach_clock(&self, clock: Arc<Mutex<Clock>>) {
129+
pub fn detach_clock(&self, clock: Arc<Clock>) {
130130
self._clocks
131131
.lock()
132132
.unwrap()
@@ -159,7 +159,7 @@ impl TimeSource {
159159
if enable != *ros_time_active {
160160
*ros_time_active = enable;
161161
for clock in self._clocks.lock().unwrap().iter() {
162-
clock.lock().unwrap().set_ros_time(enable);
162+
clock.set_ros_time(enable);
163163
}
164164
if enable {
165165
self._clock_subscription = Some(self.create_clock_sub()?);
@@ -170,12 +170,11 @@ impl TimeSource {
170170
Ok(())
171171
}
172172

173-
fn update_clock(clock: &Arc<Mutex<Clock>>, nanoseconds: i64) {
174-
let clock = clock.lock().unwrap();
173+
fn update_clock(clock: &Arc<Clock>, nanoseconds: i64) {
175174
clock.set_ros_time_override(nanoseconds);
176175
}
177176

178-
fn update_all_clocks(clocks: &Arc<Mutex<Vec<Arc<Mutex<Clock>>>>>, nanoseconds: i64) {
177+
fn update_all_clocks(clocks: &Arc<Mutex<Vec<Arc<Clock>>>>, nanoseconds: i64) {
179178
let clocks = clocks.lock().unwrap();
180179
for clock in clocks.iter() {
181180
Self::update_clock(clock, nanoseconds);

0 commit comments

Comments
 (0)