1
- // Copyright (c) 2023 Contributors to the Eclipse Foundation
1
+ // Copyright (c) 2024 Contributors to the Eclipse Foundation
2
2
//
3
3
// See the NOTICE file(s) distributed with this work for additional
4
4
// information regarding copyright ownership.
33
33
//! let _guard_1 = waitset.attach(&listener_1)?;
34
34
//! let _guard_2 = waitset.attach(&listener_2)?;
35
35
//!
36
- //! while waitset.timed_wait( |attachment_id| {
36
+ //! let event_handler = |attachment_id| {
37
37
//! let listener = if attachment_id.originates_from(&listener_1) {
38
38
//! &listener_1
39
39
//! } else {
43
43
//! while let Ok(Some(event_id)) = listener.try_wait_one() {
44
44
//! println!("received notification {:?}", event_id);
45
45
//! }
46
- //! }, Duration::from_secs(1)) != Ok(WaitEvent::TerminationRequest) {}
46
+ //! };
47
+ //!
48
+ //! while waitset.timed_wait(event_handler, Duration::from_secs(1))
49
+ //! != Ok(WaitEvent::TerminationRequest) {}
47
50
//!
48
51
//! # Ok(())
49
52
//! # }
94
97
use std:: { fmt:: Debug , time:: Duration } ;
95
98
96
99
use iceoryx2_bb_log:: fail;
97
- use iceoryx2_bb_posix:: { file_descriptor_set:: SynchronousMultiplexing , signal:: SignalHandler } ;
100
+ use iceoryx2_bb_posix:: {
101
+ file_descriptor_set:: SynchronousMultiplexing ,
102
+ periodic_timer:: { PeriodicTimer , PeriodicTimerBuilder , PeriodicTimerGuard } ,
103
+ signal:: SignalHandler ,
104
+ } ;
98
105
use iceoryx2_cal:: reactor:: * ;
99
106
100
107
/// Defines the type of that triggered [`WaitSet::try_wait()`], [`WaitSet::timed_wait()`] or
@@ -183,11 +190,13 @@ impl AttachmentId {
183
190
184
191
/// Is returned when something is attached to the [`WaitSet`]. As soon as it goes out
185
192
/// of scope, the attachment is detached.
186
- pub struct Guard < ' waitset , ' attachment , Service : crate :: service:: Service > (
187
- <Service :: Reactor as Reactor >:: Guard < ' waitset , ' attachment > ,
188
- )
193
+ pub struct Guard < ' waitset , ' attachment , Service : crate :: service:: Service >
189
194
where
190
- Service :: Reactor : ' waitset ;
195
+ Service :: Reactor : ' waitset ,
196
+ {
197
+ reactor_guard : Option < <Service :: Reactor as Reactor >:: Guard < ' waitset , ' attachment > > ,
198
+ periodic_timer_guard : Option < PeriodicTimerGuard < ' waitset > > ,
199
+ }
191
200
192
201
/// The builder for the [`WaitSet`].
193
202
#[ derive( Debug ) ]
@@ -211,8 +220,16 @@ impl WaitSetBuilder {
211
220
) -> Result < WaitSet < Service > , WaitSetCreateError > {
212
221
let msg = "Unable to create WaitSet" ;
213
222
223
+ let periodic_timer = fail ! ( from self ,
224
+ when PeriodicTimerBuilder :: new( ) . create( ) ,
225
+ with WaitSetCreateError :: InternalError ,
226
+ "{} since the PeriodicTimer could not be created." , msg) ;
227
+
214
228
match <Service :: Reactor as Reactor >:: Builder :: new ( ) . create ( ) {
215
- Ok ( reactor) => Ok ( WaitSet { reactor } ) ,
229
+ Ok ( reactor) => Ok ( WaitSet {
230
+ reactor,
231
+ periodic_timer,
232
+ } ) ,
216
233
Err ( ReactorCreateError :: UnknownError ( e) ) => {
217
234
fail ! ( from self , with WaitSetCreateError :: InternalError ,
218
235
"{msg} due to an internal error (error code = {})" , e) ;
@@ -232,18 +249,22 @@ impl WaitSetBuilder {
232
249
#[ derive( Debug ) ]
233
250
pub struct WaitSet < Service : crate :: service:: Service > {
234
251
reactor : Service :: Reactor ,
252
+ periodic_timer : PeriodicTimer ,
235
253
}
236
254
237
255
impl < Service : crate :: service:: Service > WaitSet < Service > {
238
256
/// Attaches an object to the [`WaitSet`]. The object cannot be attached twice and the
239
257
/// [`WaitSet::capacity()`] is limited by the underlying implementation.
240
- pub fn attach < ' waitset , ' attachment , T : SynchronousMultiplexing + Debug > (
258
+ pub fn notification < ' waitset , ' attachment , T : SynchronousMultiplexing + Debug > (
241
259
& ' waitset self ,
242
260
attachment : & ' attachment T ,
243
261
) -> Result < Guard < ' waitset , ' attachment , Service > , WaitSetAttachmentError > {
244
262
let msg = "Unable to attach the attachment" ;
245
263
match self . reactor . attach ( attachment) {
246
- Ok ( guard) => Ok ( Guard ( guard) ) ,
264
+ Ok ( guard) => Ok ( Guard {
265
+ reactor_guard : Some ( guard) ,
266
+ periodic_timer_guard : None ,
267
+ } ) ,
247
268
Err ( ReactorAttachError :: AlreadyAttached ) => {
248
269
fail ! ( from self , with WaitSetAttachmentError :: AlreadyAttached ,
249
270
"{msg} {:?} since it is already attached." , attachment) ;
0 commit comments