@@ -73,7 +73,7 @@ pub trait TimerBase: Send + Sync {
73
73
/// [3]: crate::Node::create_timer
74
74
/// [4]: crate::Node
75
75
pub struct Timer {
76
- callback : Arc < dyn Fn ( & mut Timer ) + Send + Sync > ,
76
+ callback : Arc < Mutex < dyn FnMut ( & mut Timer ) + Send + Sync > > ,
77
77
handle : TimerHandle ,
78
78
}
79
79
@@ -87,7 +87,7 @@ impl Timer {
87
87
callback : F ,
88
88
) -> Result < Self , RclrsError >
89
89
where
90
- F : Fn ( & mut Timer ) + ' static + Send + Sync ,
90
+ F : FnMut ( & mut Timer ) + ' static + Send + Sync ,
91
91
{
92
92
Timer :: new_with_context_handle ( Arc :: clone ( & context. handle ) , clock, period, callback)
93
93
}
@@ -100,10 +100,10 @@ impl Timer {
100
100
callback : F ,
101
101
) -> Result < Self , RclrsError >
102
102
where
103
- F : Fn ( & mut Timer ) + ' static + Send + Sync ,
103
+ F : FnMut ( & mut Timer ) + ' static + Send + Sync ,
104
104
{
105
105
// 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) ) ;
107
107
108
108
// SAFETY: Getting a zero-initialized value is always safe.
109
109
let mut rcl_timer = unsafe { rcl_get_zero_initialized_timer ( ) } ;
@@ -227,6 +227,11 @@ impl Timer {
227
227
}
228
228
229
229
/// 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
230
235
pub fn set_period ( & self , period : Duration ) {
231
236
let timer = self . handle . lock ( ) ;
232
237
let new_period = i64:: try_from ( period. as_nanos ( ) ) . unwrap_or ( i64:: MAX ) ;
@@ -304,7 +309,7 @@ impl TimerBase for Timer {
304
309
self . call_rcl ( ) ?;
305
310
306
311
let callback = self . callback . clone ( ) ;
307
- callback ( self ) ;
312
+ ( * callback. lock ( ) . unwrap ( ) ) ( self ) ;
308
313
309
314
Ok ( ( ) )
310
315
}
0 commit comments