64
64
//! }
65
65
//! };
66
66
//!
67
- //! while waitset.run(event_handler) != Ok(WaitEvent::TerminationRequest ) {}
67
+ //! while waitset.run(event_handler).is_ok( ) {}
68
68
//!
69
69
//! # Ok(())
70
70
//! # }
97
97
//! }
98
98
//! };
99
99
//!
100
- //! while waitset.run(event_handler) != Ok(WaitEvent::TerminationRequest ) {}
100
+ //! while waitset.run(event_handler).is_ok( ) {}
101
101
//!
102
102
//! # Ok(())
103
103
//! # }
132
132
//! }
133
133
//! };
134
134
//!
135
- //! while waitset.run(event_handler) != Ok(WaitEvent::TerminationRequest ) {}
135
+ //! while waitset.run(event_handler).is_ok( ) {}
136
136
//!
137
137
//! # Ok(())
138
138
//! # }
172
172
//! println!("received notification {:?}", event_id);
173
173
//! }
174
174
//! }
175
- //! }) != Ok(WaitEvent::TerminationRequest ) {}
175
+ //! }).is_ok( ) {}
176
176
//!
177
177
//! # Ok(())
178
178
//! # }
@@ -193,20 +193,6 @@ use iceoryx2_bb_posix::{
193
193
use iceoryx2_cal:: reactor:: * ;
194
194
use iceoryx2_pal_concurrency_sync:: iox_atomic:: IoxAtomicUsize ;
195
195
196
- /// Defines the type of that triggered [`WaitSet::try_wait()`], [`WaitSet::timed_wait()`] or
197
- /// [`WaitSet::blocking_wait()`].
198
- #[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
199
- pub enum WaitEvent {
200
- /// A termination signal `SIGTERM` was received.
201
- TerminationRequest ,
202
- /// An interrupt signal `SIGINT` was received.
203
- Interrupt ,
204
- /// No event was triggered.
205
- Tick ,
206
- /// One or more event notifications were received.
207
- Notification ,
208
- }
209
-
210
196
/// Defines the failures that can occur when attaching something with [`WaitSet::attach()`].
211
197
#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
212
198
pub enum WaitSetAttachmentError {
@@ -226,27 +212,28 @@ impl std::fmt::Display for WaitSetAttachmentError {
226
212
227
213
impl std:: error:: Error for WaitSetAttachmentError { }
228
214
229
- /// Defines the failures that can occur when calling
230
- /// * [`WaitSet::try_wait()`]
231
- /// * [`WaitSet::timed_wait()`]
232
- /// * [`WaitSet::blocking_wait()`]
215
+ /// Defines the failures that can occur when calling [`WaitSet::run()`].
233
216
#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
234
- pub enum WaitSetWaitError {
217
+ pub enum WaitSetRunError {
235
218
/// The process has not sufficient permissions to wait on the attachments.
236
219
InsufficientPermissions ,
237
220
/// An internal error has occurred.
238
221
InternalError ,
239
222
/// Waiting on an empty [`WaitSet`] would lead to a deadlock therefore it causes an error.
240
223
NoAttachments ,
224
+ /// A termination signal `SIGTERM` was received.
225
+ TerminationRequest ,
226
+ /// An interrupt signal `SIGINT` was received.
227
+ Interrupt ,
241
228
}
242
229
243
- impl std:: fmt:: Display for WaitSetWaitError {
230
+ impl std:: fmt:: Display for WaitSetRunError {
244
231
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
245
- std:: write!( f, "WaitSetWaitError ::{:?}" , self )
232
+ std:: write!( f, "WaitSetRunError ::{:?}" , self )
246
233
}
247
234
}
248
235
249
- impl std:: error:: Error for WaitSetWaitError { }
236
+ impl std:: error:: Error for WaitSetRunError { }
250
237
251
238
/// Defines the failures that can occur when calling [`WaitSetBuilder::create()`].
252
239
#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
@@ -327,12 +314,20 @@ impl<Service: crate::service::Service> AttachmentId<Service> {
327
314
}
328
315
}
329
316
330
- /// Returns true if an event was emitted from the attachment corresponding to [`Guard`].
317
+ /// Returns true if an event was emitted from a notification or deadline attachment
318
+ /// corresponding to [`Guard`].
331
319
pub fn event_from ( & self , other : & Guard < Service > ) -> bool {
332
- if let AttachmentIdType :: Deadline ( ..) = self . attachment_type {
333
- false
320
+ let other_attachment = other. to_attachment_id ( ) ;
321
+ if let AttachmentIdType :: Deadline ( other_waitset, other_reactor_idx, _) =
322
+ other_attachment. attachment_type
323
+ {
324
+ if let AttachmentIdType :: Notification ( waitset, reactor_idx) = self . attachment_type {
325
+ waitset == other_waitset && reactor_idx == other_reactor_idx
326
+ } else {
327
+ false
328
+ }
334
329
} else {
335
- self . attachment_type == other . to_attachment_id ( ) . attachment_type
330
+ self . attachment_type == other_attachment . attachment_type
336
331
}
337
332
}
338
333
@@ -481,19 +476,15 @@ impl<Service: crate::service::Service> WaitSet<Service> {
481
476
. remove ( & deadline_queue_idx) ;
482
477
}
483
478
484
- fn contains_deadlines ( & self ) -> bool {
485
- !self . attachment_to_deadline . borrow ( ) . is_empty ( )
486
- }
487
-
488
479
fn reset_deadline (
489
480
& self ,
490
481
reactor_idx : i32 ,
491
- ) -> Result < Option < DeadlineQueueIndex > , WaitSetWaitError > {
482
+ ) -> Result < Option < DeadlineQueueIndex > , WaitSetRunError > {
492
483
let msg = "Unable to reset deadline" ;
493
484
if let Some ( deadline_queue_idx) = self . attachment_to_deadline . borrow ( ) . get ( & reactor_idx) {
494
485
fail ! ( from self ,
495
486
when self . deadline_queue. reset( * deadline_queue_idx) ,
496
- with WaitSetWaitError :: InternalError ,
487
+ with WaitSetRunError :: InternalError ,
497
488
"{msg} since the deadline_queue guard could not be reset for the attachment {reactor_idx}. Continuing operations will lead to invalid deadline failures." ) ;
498
489
Ok ( Some ( * deadline_queue_idx) )
499
490
} else {
@@ -505,7 +496,7 @@ impl<Service: crate::service::Service> WaitSet<Service> {
505
496
& self ,
506
497
fn_call : & mut F ,
507
498
error_msg : & str ,
508
- ) -> Result < ( ) , WaitSetWaitError > {
499
+ ) -> Result < ( ) , WaitSetRunError > {
509
500
let deadline_to_attachment = self . deadline_to_attachment . borrow ( ) ;
510
501
let call = |idx : DeadlineQueueIndex | {
511
502
if let Some ( reactor_idx) = deadline_to_attachment. get ( & idx) {
@@ -515,12 +506,10 @@ impl<Service: crate::service::Service> WaitSet<Service> {
515
506
}
516
507
} ;
517
508
518
- if self . contains_deadlines ( ) {
519
- fail ! ( from self ,
509
+ fail ! ( from self ,
520
510
when self . deadline_queue. missed_deadlines( call) ,
521
- with WaitSetWaitError :: InternalError ,
511
+ with WaitSetRunError :: InternalError ,
522
512
"{error_msg} since the missed deadlines could not be acquired." ) ;
523
- }
524
513
525
514
Ok ( ( ) )
526
515
}
@@ -530,32 +519,22 @@ impl<Service: crate::service::Service> WaitSet<Service> {
530
519
triggered_file_descriptors : & Vec < i32 > ,
531
520
fn_call : & mut F ,
532
521
error_msg : & str ,
533
- ) -> Result < ( ) , WaitSetWaitError > {
522
+ ) -> Result < ( ) , WaitSetRunError > {
534
523
// we need to reset the deadlines first, otherwise a long fn_call may extend the
535
524
// deadline unintentionally
536
- if self . contains_deadlines ( ) {
537
- let mut fd_and_deadline_queue_idx = Vec :: new ( ) ;
538
- fd_and_deadline_queue_idx. reserve ( triggered_file_descriptors. len ( ) ) ;
525
+ let mut fd_and_deadline_queue_idx = Vec :: new ( ) ;
526
+ fd_and_deadline_queue_idx. reserve ( triggered_file_descriptors. len ( ) ) ;
539
527
540
- for fd in triggered_file_descriptors {
541
- fd_and_deadline_queue_idx. push ( ( fd, self . reset_deadline ( * fd) ?) ) ;
542
- }
528
+ for fd in triggered_file_descriptors {
529
+ fd_and_deadline_queue_idx. push ( ( fd, self . reset_deadline ( * fd) ?) ) ;
530
+ }
543
531
544
- // must be called after the deadlines have been reset, in the case that the
545
- // event has been received shortly before the deadline ended.
546
- self . handle_deadlines ( fn_call, error_msg) ?;
532
+ // must be called after the deadlines have been reset, in the case that the
533
+ // event has been received shortly before the deadline ended.
534
+ self . handle_deadlines ( fn_call, error_msg) ?;
547
535
548
- for ( fd, deadline_queue_idx) in fd_and_deadline_queue_idx {
549
- if let Some ( deadline_queue_idx) = deadline_queue_idx {
550
- fn_call ( AttachmentId :: deadline ( self , * fd, deadline_queue_idx) ) ;
551
- } else {
552
- fn_call ( AttachmentId :: notification ( self , * fd) ) ;
553
- }
554
- }
555
- } else {
556
- for fd in triggered_file_descriptors {
557
- fn_call ( AttachmentId :: notification ( self , * fd) ) ;
558
- }
536
+ for fd in triggered_file_descriptors {
537
+ fn_call ( AttachmentId :: notification ( self , * fd) ) ;
559
538
}
560
539
561
540
Ok ( ( ) )
@@ -591,10 +570,15 @@ impl<Service: crate::service::Service> WaitSet<Service> {
591
570
let reactor_guard = self . attach_to_reactor ( attachment) ?;
592
571
let deadline_queue_guard = self . attach_to_deadline_queue ( deadline) ?;
593
572
594
- self . attachment_to_deadline . borrow_mut ( ) . insert (
595
- unsafe { reactor_guard. file_descriptor ( ) . native_handle ( ) } ,
596
- deadline_queue_guard. index ( ) ,
597
- ) ;
573
+ let reactor_idx = unsafe { reactor_guard. file_descriptor ( ) . native_handle ( ) } ;
574
+ let deadline_idx = deadline_queue_guard. index ( ) ;
575
+
576
+ self . attachment_to_deadline
577
+ . borrow_mut ( )
578
+ . insert ( reactor_idx, deadline_idx) ;
579
+ self . deadline_to_attachment
580
+ . borrow_mut ( )
581
+ . insert ( deadline_idx, reactor_idx) ;
598
582
self . attach ( ) ?;
599
583
600
584
Ok ( Guard {
@@ -625,21 +609,22 @@ impl<Service: crate::service::Service> WaitSet<Service> {
625
609
pub fn run < F : FnMut ( AttachmentId < Service > ) > (
626
610
& self ,
627
611
mut fn_call : F ,
628
- ) -> Result < WaitEvent , WaitSetWaitError > {
612
+ ) -> Result < ( ) , WaitSetRunError > {
629
613
let msg = "Unable to call WaitSet::run()" ;
630
614
631
615
if SignalHandler :: termination_requested ( ) {
632
- return Ok ( WaitEvent :: TerminationRequest ) ;
616
+ fail ! ( from self , with WaitSetRunError :: TerminationRequest ,
617
+ "{msg} since a termination request was received." ) ;
633
618
}
634
619
635
620
if self . is_empty ( ) {
636
- fail ! ( from self , with WaitSetWaitError :: NoAttachments ,
621
+ fail ! ( from self , with WaitSetRunError :: NoAttachments ,
637
622
"{msg} since the WaitSet has no attachments, therefore the call would end up in a deadlock." ) ;
638
623
}
639
624
640
625
let next_timeout = fail ! ( from self ,
641
626
when self . deadline_queue. duration_until_next_deadline( ) ,
642
- with WaitSetWaitError :: InternalError ,
627
+ with WaitSetRunError :: InternalError ,
643
628
"{msg} since the next timeout could not be acquired." ) ;
644
629
645
630
let mut triggered_file_descriptors = vec ! [ ] ;
@@ -655,19 +640,22 @@ impl<Service: crate::service::Service> WaitSet<Service> {
655
640
) {
656
641
Ok ( 0 ) => {
657
642
self . handle_deadlines ( & mut fn_call, msg) ?;
658
- Ok ( WaitEvent :: Tick )
643
+ Ok ( ( ) )
659
644
}
660
645
Ok ( _) => {
661
646
self . handle_all_attachments ( & triggered_file_descriptors, & mut fn_call, msg) ?;
662
- Ok ( WaitEvent :: Notification )
647
+ Ok ( ( ) )
648
+ }
649
+ Err ( ReactorWaitError :: Interrupt ) => {
650
+ fail ! ( from self , with WaitSetRunError :: Interrupt ,
651
+ "{msg} since an interrupt signal was received." ) ;
663
652
}
664
- Err ( ReactorWaitError :: Interrupt ) => Ok ( WaitEvent :: Interrupt ) ,
665
653
Err ( ReactorWaitError :: InsufficientPermissions ) => {
666
- fail ! ( from self , with WaitSetWaitError :: InsufficientPermissions ,
654
+ fail ! ( from self , with WaitSetRunError :: InsufficientPermissions ,
667
655
"{msg} due to insufficient permissions." ) ;
668
656
}
669
657
Err ( ReactorWaitError :: UnknownError ) => {
670
- fail ! ( from self , with WaitSetWaitError :: InternalError ,
658
+ fail ! ( from self , with WaitSetRunError :: InternalError ,
671
659
"{msg} due to an internal error." ) ;
672
660
}
673
661
}
0 commit comments