@@ -73,7 +73,7 @@ pub trait TimerBase: Send + Sync {
7373/// [3]: crate::Node::create_timer
7474/// [4]: crate::Node
7575pub struct Timer {
76- callback : Arc < dyn Fn ( & mut Timer ) + Send + Sync > ,
76+ callback : Arc < Mutex < dyn FnMut ( & mut Timer ) + Send + Sync > > ,
7777 handle : TimerHandle ,
7878}
7979
@@ -87,7 +87,7 @@ impl Timer {
8787 callback : F ,
8888 ) -> Result < Self , RclrsError >
8989 where
90- F : Fn ( & mut Timer ) + ' static + Send + Sync ,
90+ F : FnMut ( & mut Timer ) + ' static + Send + Sync ,
9191 {
9292 Timer :: new_with_context_handle ( Arc :: clone ( & context. handle ) , clock, period, callback)
9393 }
@@ -100,10 +100,10 @@ impl Timer {
100100 callback : F ,
101101 ) -> Result < Self , RclrsError >
102102 where
103- F : Fn ( & mut Timer ) + ' static + Send + Sync ,
103+ F : FnMut ( & mut Timer ) + ' static + Send + Sync ,
104104 {
105105 // Move the callback to our reference counted container so rcl_callback can use it
106- let callback = Arc :: new ( callback) ;
106+ let callback = Arc :: new ( Mutex :: new ( callback) ) ;
107107
108108 // SAFETY: Getting a zero-initialized value is always safe.
109109 let mut rcl_timer = unsafe { rcl_get_zero_initialized_timer ( ) } ;
@@ -227,6 +227,11 @@ impl Timer {
227227 }
228228
229229 /// Set the period of the timer. Periods greater than i64::MAX nanoseconds will saturate to i64::MAX.
230+ ///
231+ /// The updated period will not take affect until either [`reset`][1] is called
232+ /// or the timer next expires, whichever comes first.
233+ ///
234+ /// [1]: crate::Timer::reset
230235 pub fn set_period ( & self , period : Duration ) {
231236 let timer = self . handle . lock ( ) ;
232237 let new_period = i64:: try_from ( period. as_nanos ( ) ) . unwrap_or ( i64:: MAX ) ;
@@ -304,7 +309,7 @@ impl TimerBase for Timer {
304309 self . call_rcl ( ) ?;
305310
306311 let callback = self . callback . clone ( ) ;
307- callback ( self ) ;
312+ ( * callback. lock ( ) . unwrap ( ) ) ( self ) ;
308313
309314 Ok ( ( ) )
310315 }
0 commit comments