Skip to content

Commit f8d78ad

Browse files
committed
[#390] Fixing first batch of review comments
1 parent da231db commit f8d78ad

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

examples/rust/event_multiplexing/wait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5151
println!("Waiting on the following services: {:?}", args.services);
5252

5353
// the callback that is called when a listener has received an event
54-
let trigger_call = |attachment| {
55-
if let Some((service_name, listener)) = listeners.get(&attachment) {
54+
let trigger_call = |attachment_id| {
55+
if let Some((service_name, listener)) = listeners.get(&attachment_id) {
5656
print!("Received trigger from \"{}\" ::", service_name);
5757

5858
while let Ok(Some(event_id)) = listener.try_wait_one() {

iceoryx2/src/port/waitset.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2023 Contributors to the Eclipse Foundation
1+
// Copyright (c) 2024 Contributors to the Eclipse Foundation
22
//
33
// See the NOTICE file(s) distributed with this work for additional
44
// information regarding copyright ownership.
@@ -33,7 +33,7 @@
3333
//! let _guard_1 = waitset.attach(&listener_1)?;
3434
//! let _guard_2 = waitset.attach(&listener_2)?;
3535
//!
36-
//! while waitset.timed_wait(|attachment_id| {
36+
//! let event_handler = |attachment_id| {
3737
//! let listener = if attachment_id.originates_from(&listener_1) {
3838
//! &listener_1
3939
//! } else {
@@ -43,7 +43,10 @@
4343
//! while let Ok(Some(event_id)) = listener.try_wait_one() {
4444
//! println!("received notification {:?}", event_id);
4545
//! }
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) {}
4750
//!
4851
//! # Ok(())
4952
//! # }
@@ -94,7 +97,11 @@
9497
use std::{fmt::Debug, time::Duration};
9598

9699
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+
};
98105
use iceoryx2_cal::reactor::*;
99106

100107
/// Defines the type of that triggered [`WaitSet::try_wait()`], [`WaitSet::timed_wait()`] or
@@ -183,11 +190,13 @@ impl AttachmentId {
183190

184191
/// Is returned when something is attached to the [`WaitSet`]. As soon as it goes out
185192
/// 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>
189194
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+
}
191200

192201
/// The builder for the [`WaitSet`].
193202
#[derive(Debug)]
@@ -211,8 +220,16 @@ impl WaitSetBuilder {
211220
) -> Result<WaitSet<Service>, WaitSetCreateError> {
212221
let msg = "Unable to create WaitSet";
213222

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+
214228
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+
}),
216233
Err(ReactorCreateError::UnknownError(e)) => {
217234
fail!(from self, with WaitSetCreateError::InternalError,
218235
"{msg} due to an internal error (error code = {})", e);
@@ -232,18 +249,22 @@ impl WaitSetBuilder {
232249
#[derive(Debug)]
233250
pub struct WaitSet<Service: crate::service::Service> {
234251
reactor: Service::Reactor,
252+
periodic_timer: PeriodicTimer,
235253
}
236254

237255
impl<Service: crate::service::Service> WaitSet<Service> {
238256
/// Attaches an object to the [`WaitSet`]. The object cannot be attached twice and the
239257
/// [`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>(
241259
&'waitset self,
242260
attachment: &'attachment T,
243261
) -> Result<Guard<'waitset, 'attachment, Service>, WaitSetAttachmentError> {
244262
let msg = "Unable to attach the attachment";
245263
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+
}),
247268
Err(ReactorAttachError::AlreadyAttached) => {
248269
fail!(from self, with WaitSetAttachmentError::AlreadyAttached,
249270
"{msg} {:?} since it is already attached.", attachment);

iceoryx2/tests/waitset_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ mod waitset {
374374
barrier.wait();
375375
let wait_event = sut.blocking_wait(|_| {}).unwrap();
376376

377-
t1.join().unwrap();
378-
379377
assert_that!(wait_event, eq WaitEvent::Notification);
380378
assert_that!(start.elapsed(), time_at_least TIMEOUT);
379+
380+
t1.join().unwrap();
381381
}
382382

383383
#[instantiate_tests(<iceoryx2::service::ipc::Service>)]

0 commit comments

Comments
 (0)