Skip to content

Commit

Permalink
[#51] Use blocking_wait_while to make test more robust, rename wait_w…
Browse files Browse the repository at this point in the history
…hile into blocking_wait_while to be consistent with other blocking functions
  • Loading branch information
elfenpiff committed Dec 24, 2023
1 parent af0ec81 commit a8cb41f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
28 changes: 14 additions & 14 deletions iceoryx2-bb/posix/src/condition_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl<'mutex, 'handle, T: Debug> From<MutexLockError<'mutex, 'handle, T>>
/// use iceoryx2_bb_posix::mutex::*;
///
///
/// // create a condition variable which allows multiple predicates in wait_while nad
/// // create a condition variable which allows multiple predicates in blocking_wait_while nad
/// // timed_wait_while
/// let mtx_handle = MutexHandle::<i32>::new();
/// let condvar = ConditionVariableBuilder::new()
Expand Down Expand Up @@ -237,7 +237,7 @@ impl ConditionVariableBuilder {
/// modified and used for triggering.
///
/// The condition variable can use
/// multiple conditions in [`MultiConditionVariable::wait_while()`] and
/// multiple conditions in [`MultiConditionVariable::blocking_wait_while()`] and
/// [`MultiConditionVariable::timed_wait_while()`] but is only able to trigger all waiters.
pub fn create_multi_condition_variable<T: Debug>(
self,
Expand All @@ -252,7 +252,7 @@ impl ConditionVariableBuilder {
///
/// The condition variable has one fixed
/// condition which has to be provided on construction. The methods
/// [`ConditionVariable::wait_while()`] and
/// [`ConditionVariable::blocking_wait_while()`] and
/// [`ConditionVariable::timed_wait_while()`] will wait on that preset condition until
/// it is satisfied.
/// The restriction to a preset fixed condition comes with the feature to signal single waiters
Expand Down Expand Up @@ -489,7 +489,7 @@ pub trait BasicConditionVariableInterface<T: Debug>:
}

/// Condition variable which allows to use multiple conditions in
/// [`MultiConditionVariable::wait_while()`] and
/// [`MultiConditionVariable::blocking_wait_while()`] and
/// [`MultiConditionVariable::timed_wait_while()`] concurrently but with the draw
/// back that only all waiters can be triggered and not one.
/// The reason is when one waits on multiple
Expand All @@ -505,7 +505,7 @@ pub trait BasicConditionVariableInterface<T: Debug>:
/// is written.
/// The condition variable provides the following features:
/// * wait on the condition variable: [wait](BasicConditionVariableInterface::wait()), [timed_wait](BasicConditionVariableInterface::timed_wait())
/// * wait until a defined condition occurs: [wait_while](MultiConditionVariable::wait_while()), [timed_wait_while](MultiConditionVariable::timed_wait_while())
/// * wait until a defined condition occurs: [blocking_wait_while](MultiConditionVariable::blocking_wait_while()), [timed_wait_while](MultiConditionVariable::timed_wait_while())
/// * modify condition variable and then notify waiters:
/// [notify_all](MultiConditionVariable::notify_all()), [modify_notify_all](MultiConditionVariable::modify_notify_all())
/// * trigger waiters without changing condition variable: [trigger_all](BasicConditionVariableInterface::trigger_all())
Expand All @@ -530,7 +530,7 @@ pub trait BasicConditionVariableInterface<T: Debug>:
/// thread::scope(|s| {
/// let t1 = s.spawn(|| {
/// // wait until value is 5000
/// let guard = cv.wait_while(|t| *t == 5000).expect("failed to wait");
/// let guard = cv.blocking_wait_while(|t| *t == 5000).expect("failed to wait");
/// println!("cv value changed to 5000");
/// });
///
Expand Down Expand Up @@ -589,12 +589,11 @@ impl<'mtx_handle, T: Debug> MultiConditionVariable<'mtx_handle, T> {
/// [`MultiConditionVariable::modify_notify_all()`] or
/// [`BasicConditionVariableInterface::trigger_all()`]
/// and the provided predicate returns true.
pub fn wait_while<P: FnMut(&mut T) -> bool>(
pub fn blocking_wait_while<P: FnMut(&mut T) -> bool>(
&self,
mut predicate: P,
) -> Result<MutexGuard<'_, '_, T>, ConditionVariableWaitError<'_, '_, T>> {
let mut guard =
fail!(from self, when self.mutex.lock(), "Failed to lock mutex in wait_while.");
let mut guard = fail!(from self, when self.mutex.lock(), "Failed to lock mutex in blocking_wait_while.");

while !(predicate)(&mut *guard) {
self.condvar.pthread_wait(&self.mutex)?;
Expand Down Expand Up @@ -742,7 +741,7 @@ impl<T: Debug> Drop for ConditionVariableGuard<'_, '_, '_, T> {
}

/// Condition variable which requires a fixed predicate on creation which is then used in
/// [`ConditionVariable::wait_while()`] and
/// [`ConditionVariable::blocking_wait_while()`] and
/// [`ConditionVariable::timed_wait_while()`] concurrently with the benefit of triggering
/// single waiters.
/// The reason is when one waits on multiple
Expand All @@ -758,7 +757,7 @@ impl<T: Debug> Drop for ConditionVariableGuard<'_, '_, '_, T> {
/// is written.
/// The condition variable provides the following features:
/// * wait on the condition variable: [wait](BasicConditionVariableInterface::wait()), [timed_wait](BasicConditionVariableInterface::timed_wait())
/// * wait until a defined condition occurs: [wait_while](ConditionVariable::wait_while()), [timed_wait_while](ConditionVariable::timed_wait_while())
/// * wait until a defined condition occurs: [blocking_wait_while](ConditionVariable::blocking_wait_while()), [timed_wait_while](ConditionVariable::timed_wait_while())
/// * modify condition variable and then notify waiters:
/// [notify_all](ConditionVariable::notify_all()), [modify_notify_all](ConditionVariable::modify_notify_all()),
/// [notify_one](ConditionVariable::notify_one()), [modify_notify_one](ConditionVariable::modify_notify_one())
Expand Down Expand Up @@ -786,7 +785,7 @@ impl<T: Debug> Drop for ConditionVariableGuard<'_, '_, '_, T> {
/// thread::scope(|s| {
/// let t1 = s.spawn(|| {
/// // wait until value is 5000
/// let guard = cv.wait_while().expect("failed to wait");
/// let guard = cv.blocking_wait_while().expect("failed to wait");
/// println!("cv value is greater or equal 5000");
/// });
///
Expand Down Expand Up @@ -856,13 +855,14 @@ impl<'mtx_handle, T: Debug> ConditionVariable<'mtx_handle, T> {
/// [`BasicConditionVariableInterface::trigger_all()`] or
/// [`ConditionVariable::trigger_one()`]
/// and the provided predicate returns true.
pub fn wait_while(
pub fn blocking_wait_while(
&self,
) -> Result<
MutexGuard<'_, '_, ConditionVariableData<T>>,
ConditionVariableWaitError<'_, '_, ConditionVariableData<T>>,
> {
let guard = fail!(from self, when self.mutex.lock(), "failed to lock mutex in wait_while");
let guard =
fail!(from self, when self.mutex.lock(), "failed to lock mutex in blocking_wait_while");

while !self.call_underlying_predicate(&guard) {
self.condvar.pthread_wait(&self.mutex)?;
Expand Down
18 changes: 9 additions & 9 deletions iceoryx2-bb/posix/tests/condition_variable_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn multi_condition_variable_wait_while_is_signalled_by_notify_all() {

thread::scope(|s| {
let t1 = s.spawn(|| {
let guard = sut.wait_while(|t| *t == 4456).unwrap();
let guard = sut.blocking_wait_while(|t| *t == 4456).unwrap();
assert_that!(*guard, eq 4456);
});

Expand Down Expand Up @@ -78,7 +78,7 @@ fn multi_condition_variable_wait_while_is_signalled_by_modify_notify_all() {

thread::scope(|s| {
let t1 = s.spawn(|| {
let guard = sut.wait_while(|t| *t == 4456).unwrap();
let guard = sut.blocking_wait_while(|t| *t == 4456).unwrap();
assert_that!(*guard, eq 4456);
});

Expand Down Expand Up @@ -254,14 +254,14 @@ fn condition_variable_notify_all_signals_all_waiters() {
let sut_thread1 = Arc::clone(&sut);
let counter_thread1 = Arc::clone(&counter);
let t1 = s.spawn(move || {
sut_thread1.wait_while().unwrap();
sut_thread1.blocking_wait_while().unwrap();
counter_thread1.fetch_add(1, Ordering::Relaxed);
});

let sut_thread2 = Arc::clone(&sut);
let counter_thread2 = Arc::clone(&counter);
let t2 = s.spawn(move || {
sut_thread2.wait_while().unwrap();
sut_thread2.blocking_wait_while().unwrap();
counter_thread2.fetch_add(1, Ordering::Relaxed);
});

Expand Down Expand Up @@ -300,14 +300,14 @@ fn condition_variable_notify_one_signals_one_waiter() {
let sut_thread1 = Arc::clone(&sut);
let counter_thread1 = Arc::clone(&counter);
let t1 = s.spawn(move || {
sut_thread1.wait_while().unwrap();
sut_thread1.blocking_wait_while().unwrap();
counter_thread1.fetch_add(1, Ordering::Relaxed);
});

let sut_thread2 = Arc::clone(&sut);
let counter_thread2 = Arc::clone(&counter);
let t2 = s.spawn(move || {
sut_thread2.wait_while().unwrap();
sut_thread2.blocking_wait_while().unwrap();
counter_thread2.fetch_add(1, Ordering::Relaxed);
});

Expand Down Expand Up @@ -383,14 +383,14 @@ fn condition_variable_modify_notify_one_signals_one_waiter() {
let sut_thread1 = Arc::clone(&sut);
let counter_thread1 = Arc::clone(&counter);
let t1 = s.spawn(move || {
sut_thread1.timed_wait_while(TIMEOUT * 10).unwrap();
sut_thread1.blocking_wait_while().unwrap();
counter_thread1.fetch_add(1, Ordering::Relaxed);
});

let sut_thread2 = Arc::clone(&sut);
let counter_thread2 = Arc::clone(&counter);
let t2 = s.spawn(move || {
sut_thread2.timed_wait_while(TIMEOUT * 10).unwrap();
sut_thread2.blocking_wait_while().unwrap();
counter_thread2.fetch_add(1, Ordering::Relaxed);
});

Expand Down Expand Up @@ -466,5 +466,5 @@ fn condition_variable_wait_while_does_not_wait_when_predicate_is_fulfilled() {
);
sut.lock().unwrap().value = 9999999;

assert_that!(sut.wait_while(), is_ok);
assert_that!(sut.blocking_wait_while(), is_ok);
}
2 changes: 1 addition & 1 deletion iceoryx2-cal/src/event/process_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<Id: crate::event::TriggerId + 'static> Listener<Id> for Duplex<Id> {

fn blocking_wait(&self) -> Result<Option<Id>, ListenerWaitError> {
let msg = "Failed to blocking_wait";
match self.management.as_ref().borrow_cvar().wait_while() {
match self.management.as_ref().borrow_cvar().blocking_wait_while() {
Err(v) => {
fail!(from self, with ListenerWaitError::InternalFailure,
"{} due to an internal failure in the underlying condition variable ({:?}).", msg, v);
Expand Down

0 comments on commit a8cb41f

Please sign in to comment.