Skip to content

Commit e29d53a

Browse files
committed
[eclipse-iceoryx#139] Add documentation
1 parent 79ea3cb commit e29d53a

File tree

5 files changed

+61
-29
lines changed

5 files changed

+61
-29
lines changed

iceoryx2-cal/src/event/common.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub mod details {
259259
id, self.storage.get().id_tracker.trigger_id_max());
260260
}
261261

262-
self.storage.get().id_tracker.add(id)?;
262+
unsafe { self.storage.get().id_tracker.add(id)? };
263263
Ok(unsafe { self.storage.get().signal_mechanism.notify()? })
264264
}
265265
}
@@ -382,7 +382,7 @@ pub mod details {
382382
&self,
383383
) -> Result<Option<crate::event::TriggerId>, crate::event::ListenerWaitError> {
384384
while unsafe { self.storage.get().signal_mechanism.try_wait()? } {}
385-
Ok(self.storage.get().id_tracker.acquire())
385+
Ok(unsafe { self.storage.get().id_tracker.acquire() })
386386
}
387387

388388
fn timed_wait(
@@ -394,7 +394,7 @@ pub mod details {
394394
}
395395

396396
if unsafe { self.storage.get().signal_mechanism.timed_wait(timeout)? } {
397-
return Ok(self.storage.get().id_tracker.acquire());
397+
return Ok(unsafe { self.storage.get().id_tracker.acquire() });
398398
}
399399

400400
Ok(None)
@@ -408,7 +408,7 @@ pub mod details {
408408
}
409409

410410
unsafe { self.storage.get().signal_mechanism.blocking_wait()? };
411-
Ok(self.storage.get().id_tracker.acquire())
411+
Ok(unsafe { self.storage.get().id_tracker.acquire() })
412412
}
413413
}
414414

iceoryx2-cal/src/event/id_tracker/bit_set.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl IdTracker for RelocatableBitSet {
2121
TriggerId::new(self.capacity() as u64)
2222
}
2323

24-
fn add(&self, id: TriggerId) -> Result<(), NotifierNotifyError> {
24+
unsafe fn add(&self, id: TriggerId) -> Result<(), NotifierNotifyError> {
2525
if self.trigger_id_max() <= id {
2626
fail!(from self, with NotifierNotifyError::TriggerIdOutOfBounds,
2727
"Unable to set bit {:?} since it is out of bounds (max = {:?}).",
@@ -32,11 +32,11 @@ impl IdTracker for RelocatableBitSet {
3232
Ok(())
3333
}
3434

35-
fn acquire_all<F: FnMut(TriggerId)>(&self, mut callback: F) {
35+
unsafe fn acquire_all<F: FnMut(TriggerId)>(&self, mut callback: F) {
3636
self.reset_all(|bit_index| callback(TriggerId::new(bit_index as u64)))
3737
}
3838

39-
fn acquire(&self) -> Option<TriggerId> {
39+
unsafe fn acquire(&self) -> Option<TriggerId> {
4040
match self.reset_next() {
4141
Some(id) => Some(TriggerId::new(id as u64)),
4242
None => None,

iceoryx2-cal/src/event/id_tracker/mod.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,34 @@ use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer;
1818

1919
use super::{NotifierNotifyError, TriggerId};
2020

21+
/// The [`SignalMechanism`] is a building block for [`crate::event::Event`]
22+
/// concept. Its task is to track the origin of the signal that was sent
23+
/// via the [`crate::event::signal_mechanism::SignalMechanism`].
2124
pub trait IdTracker: RelocatableContainer + Send + Sync + Debug {
25+
/// Returns the max value of a [`TriggerId`] that can be tracked with the
26+
/// [`IdTracker`].
2227
fn trigger_id_max(&self) -> TriggerId;
23-
fn add(&self, id: TriggerId) -> Result<(), NotifierNotifyError>;
24-
fn acquire(&self) -> Option<TriggerId>;
25-
fn acquire_all<F: FnMut(TriggerId)>(&self, callback: F);
28+
29+
/// Tracks the provided [`TriggerId`].
30+
///
31+
/// # Safety
32+
/// * underlying container must be initialized with [`RelocatableContainer::init()`]
33+
///
34+
unsafe fn add(&self, id: TriggerId) -> Result<(), NotifierNotifyError>;
35+
36+
/// Acquires a tracked [`TriggerId`]. When no [`TriggerId`]s are tracked
37+
/// or all tracked [`TriggerId`]s have been acquired it returns [`None`].
38+
///
39+
/// # Safety
40+
/// * underlying container must be initialized with [`RelocatableContainer::init()`]
41+
///
42+
unsafe fn acquire(&self) -> Option<TriggerId>;
43+
44+
/// Acquires all tracked [`TriggerId`]s and calls for everyone the user
45+
/// provided callback with the [`TriggerId`] as input argument.
46+
///
47+
/// # Safety
48+
/// * underlying container must be initialized with [`RelocatableContainer::init()`]
49+
///
50+
unsafe fn acquire_all<F: FnMut(TriggerId)>(&self, callback: F);
2651
}

iceoryx2-cal/src/event/signal_mechanism/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use super::{ListenerCreateError, ListenerWaitError, NotifierNotifyError};
1616

1717
pub mod semaphore;
1818

19+
/// The [`SignalMechanism`] is a building block for [`crate::event::Event`]
20+
/// concept. Its task is to
21+
/// wake up another process/thread with a signal.
1922
pub trait SignalMechanism: Send + Sync + Debug {
2023
/// Creates a [`SignalMechanism`]. It cannot be used until
2124
/// [`SignalMechanism::init()`] was called.

iceoryx2-cal/tests/event_id_tracker_tests.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ mod event_id_tracker {
4242
let sut = unsafe { Sut::new_uninit(CAPACITY) };
4343
assert_that!(unsafe { sut.init(memory.allocator()) }, is_ok);
4444

45-
assert_that!(sut.acquire(), eq None);
45+
assert_that!(unsafe { sut.acquire() }, eq None);
4646
for i in 0..CAPACITY {
4747
let id = TriggerId::new(i as u64);
48-
assert_that!(sut.add(id), is_ok);
49-
assert_that!(sut.acquire(), eq Some(id));
50-
assert_that!(sut.acquire(), is_none);
48+
assert_that!(unsafe { sut.add(id) }, is_ok);
49+
assert_that!(unsafe { sut.acquire() }, eq Some(id));
50+
assert_that!(unsafe { sut.acquire() }, is_none);
5151
}
5252
}
5353

@@ -61,17 +61,17 @@ mod event_id_tracker {
6161

6262
for i in 0..CAPACITY {
6363
let id = TriggerId::new((i as u64).min(sut.trigger_id_max().as_u64()));
64-
assert_that!(sut.add(id), is_ok);
64+
assert_that!(unsafe { sut.add(id) }, is_ok);
6565
}
6666

6767
let mut ids = HashSet::new();
6868
for _ in 0..CAPACITY {
69-
let result = sut.acquire().unwrap();
69+
let result = unsafe { sut.acquire().unwrap() };
7070
assert_that!(result, le sut.trigger_id_max());
7171
assert_that!(ids.insert(result), eq true);
7272
}
7373

74-
assert_that!(sut.acquire(), is_none);
74+
assert_that!(unsafe { sut.acquire() }, is_none);
7575
}
7676

7777
#[test]
@@ -84,17 +84,19 @@ mod event_id_tracker {
8484

8585
for i in 0..CAPACITY {
8686
let id = TriggerId::new((i as u64).min(sut.trigger_id_max().as_u64()));
87-
assert_that!(sut.add(id), is_ok);
87+
assert_that!(unsafe { sut.add(id) }, is_ok);
8888
}
8989

9090
let mut ids = HashSet::new();
91-
sut.acquire_all(|id| {
92-
assert_that!(id, le sut.trigger_id_max());
93-
assert_that!(ids.insert(id), eq true);
94-
});
91+
unsafe {
92+
sut.acquire_all(|id| {
93+
assert_that!(id, le sut.trigger_id_max());
94+
assert_that!(ids.insert(id), eq true);
95+
})
96+
};
9597

9698
let mut callback_called = false;
97-
sut.acquire_all(|_| callback_called = true);
99+
unsafe { sut.acquire_all(|_| callback_called = true) };
98100
assert_that!(callback_called, eq false);
99101

100102
assert_that!(ids, len CAPACITY);
@@ -110,20 +112,22 @@ mod event_id_tracker {
110112

111113
for i in 0..CAPACITY {
112114
let id = TriggerId::new((i as u64).min(sut.trigger_id_max().as_u64()));
113-
assert_that!(sut.add(id), is_ok);
115+
assert_that!(unsafe { sut.add(id) }, is_ok);
114116
}
115117

116118
let mut ids = HashSet::new();
117119
for _ in 0..CAPACITY / 2 {
118-
let result = sut.acquire().unwrap();
120+
let result = unsafe { sut.acquire().unwrap() };
119121
assert_that!(result, le sut.trigger_id_max());
120122
assert_that!(ids.insert(result), eq true);
121123
}
122124

123-
sut.acquire_all(|id| {
124-
assert_that!(id, le sut.trigger_id_max());
125-
assert_that!(ids.insert(id), eq true);
126-
});
125+
unsafe {
126+
sut.acquire_all(|id| {
127+
assert_that!(id, le sut.trigger_id_max());
128+
assert_that!(ids.insert(id), eq true);
129+
})
130+
};
127131

128132
assert_that!(ids, len CAPACITY);
129133
}

0 commit comments

Comments
 (0)