@@ -67,10 +67,10 @@ unsafe impl Send for rcl_node_t {}
6767pub struct Node {
6868 pub ( crate ) rcl_node_mtx : Arc < Mutex < rcl_node_t > > ,
6969 pub ( crate ) rcl_context_mtx : Arc < Mutex < rcl_context_t > > ,
70- pub ( crate ) clients : Vec < Weak < dyn ClientBase > > ,
71- pub ( crate ) guard_conditions : Vec < Weak < GuardCondition > > ,
72- pub ( crate ) services : Vec < Weak < dyn ServiceBase > > ,
73- pub ( crate ) subscriptions : Vec < Weak < dyn SubscriptionBase > > ,
70+ pub ( crate ) clients_mtx : Mutex < Vec < Weak < dyn ClientBase > > > ,
71+ pub ( crate ) guard_conditions_mtx : Mutex < Vec < Weak < GuardCondition > > > ,
72+ pub ( crate ) services_mtx : Mutex < Vec < Weak < dyn ServiceBase > > > ,
73+ pub ( crate ) subscriptions_mtx : Mutex < Vec < Weak < dyn SubscriptionBase > > > ,
7474 _parameter_map : ParameterOverrideMap ,
7575}
7676
@@ -180,13 +180,12 @@ impl Node {
180180 ///
181181 /// [1]: crate::Client
182182 // TODO: make client's lifetime depend on node's lifetime
183- pub fn create_client < T > ( & mut self , topic : & str ) -> Result < Arc < Client < T > > , RclrsError >
183+ pub fn create_client < T > ( & self , topic : & str ) -> Result < Arc < Client < T > > , RclrsError >
184184 where
185185 T : rosidl_runtime_rs:: Service ,
186186 {
187187 let client = Arc :: new ( Client :: < T > :: new ( Arc :: clone ( & self . rcl_node_mtx ) , topic) ?) ;
188- self . clients
189- . push ( Arc :: downgrade ( & client) as Weak < dyn ClientBase > ) ;
188+ { self . clients_mtx . lock ( ) . unwrap ( ) } . push ( Arc :: downgrade ( & client) as Weak < dyn ClientBase > ) ;
190189 Ok ( client)
191190 }
192191
@@ -199,12 +198,12 @@ impl Node {
199198 ///
200199 /// [1]: crate::GuardCondition
201200 /// [2]: crate::spin_once
202- pub fn create_guard_condition ( & mut self ) -> Arc < GuardCondition > {
201+ pub fn create_guard_condition ( & self ) -> Arc < GuardCondition > {
203202 let guard_condition = Arc :: new ( GuardCondition :: new_with_rcl_context (
204203 & mut self . rcl_context_mtx . lock ( ) . unwrap ( ) ,
205204 None ,
206205 ) ) ;
207- self . guard_conditions
206+ { self . guard_conditions_mtx . lock ( ) . unwrap ( ) }
208207 . push ( Arc :: downgrade ( & guard_condition) as Weak < GuardCondition > ) ;
209208 guard_condition
210209 }
@@ -226,7 +225,7 @@ impl Node {
226225 & mut self . rcl_context_mtx . lock ( ) . unwrap ( ) ,
227226 Some ( Box :: new ( callback) as Box < dyn Fn ( ) + Send + Sync > ) ,
228227 ) ) ;
229- self . guard_conditions
228+ { self . guard_conditions_mtx . lock ( ) . unwrap ( ) }
230229 . push ( Arc :: downgrade ( & guard_condition) as Weak < GuardCondition > ) ;
231230 guard_condition
232231 }
@@ -251,7 +250,7 @@ impl Node {
251250 /// [1]: crate::Service
252251 // TODO: make service's lifetime depend on node's lifetime
253252 pub fn create_service < T , F > (
254- & mut self ,
253+ & self ,
255254 topic : & str ,
256255 callback : F ,
257256 ) -> Result < Arc < Service < T > > , RclrsError >
@@ -264,7 +263,7 @@ impl Node {
264263 topic,
265264 callback,
266265 ) ?) ;
267- self . services
266+ { self . services_mtx . lock ( ) . unwrap ( ) }
268267 . push ( Arc :: downgrade ( & service) as Weak < dyn ServiceBase > ) ;
269268 Ok ( service)
270269 }
@@ -274,7 +273,7 @@ impl Node {
274273 /// [1]: crate::Subscription
275274 // TODO: make subscription's lifetime depend on node's lifetime
276275 pub fn create_subscription < T , Args > (
277- & mut self ,
276+ & self ,
278277 topic : & str ,
279278 qos : QoSProfile ,
280279 callback : impl SubscriptionCallback < T , Args > ,
@@ -288,32 +287,39 @@ impl Node {
288287 qos,
289288 callback,
290289 ) ?) ;
291- self . subscriptions
290+ { self . subscriptions_mtx . lock ( ) }
291+ . unwrap ( )
292292 . push ( Arc :: downgrade ( & subscription) as Weak < dyn SubscriptionBase > ) ;
293293 Ok ( subscription)
294294 }
295295
296296 /// Returns the subscriptions that have not been dropped yet.
297297 pub ( crate ) fn live_subscriptions ( & self ) -> Vec < Arc < dyn SubscriptionBase > > {
298- self . subscriptions
298+ { self . subscriptions_mtx . lock ( ) . unwrap ( ) }
299299 . iter ( )
300300 . filter_map ( Weak :: upgrade)
301301 . collect ( )
302302 }
303303
304304 pub ( crate ) fn live_clients ( & self ) -> Vec < Arc < dyn ClientBase > > {
305- self . clients . iter ( ) . filter_map ( Weak :: upgrade) . collect ( )
305+ { self . clients_mtx . lock ( ) . unwrap ( ) }
306+ . iter ( )
307+ . filter_map ( Weak :: upgrade)
308+ . collect ( )
306309 }
307310
308311 pub ( crate ) fn live_guard_conditions ( & self ) -> Vec < Arc < GuardCondition > > {
309- self . guard_conditions
312+ { self . guard_conditions_mtx . lock ( ) . unwrap ( ) }
310313 . iter ( )
311314 . filter_map ( Weak :: upgrade)
312315 . collect ( )
313316 }
314317
315318 pub ( crate ) fn live_services ( & self ) -> Vec < Arc < dyn ServiceBase > > {
316- self . services . iter ( ) . filter_map ( Weak :: upgrade) . collect ( )
319+ { self . services_mtx . lock ( ) . unwrap ( ) }
320+ . iter ( )
321+ . filter_map ( Weak :: upgrade)
322+ . collect ( )
317323 }
318324
319325 /// Returns the ROS domain ID that the node is using.
0 commit comments