Skip to content

Commit

Permalink
[#56] Make mutex test more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Dec 25, 2023
1 parent fe7d8d9 commit face950
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 6 additions & 1 deletion iceoryx2-bb/posix/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,12 @@ pub enum MutexThreadTerminationBehavior {
/// mutex owning
/// thread/process dies the mutex is put into an inconsistent state which can be recovered with
/// [`Mutex::make_consistent()`]. The inconsistent state is detected by the next instance which
/// calls [`Mutex::lock()`], [`Mutex::try_lock()`] or [`Mutex::timed_lock()`].
/// calls [`Mutex::try_lock()`] or [`Mutex::timed_lock()`].
///
/// **Important:** If the owner dies after another thread has already locked the [`Mutex`] it
/// may become impossible to recover the [`Mutex`]. Therefore, this feature should be used
/// only in combination with either [`Mutex::try_lock`] or [`Mutex::timed_lock()`] and
/// never with [`Mutex::lock()`].
///
/// This is also known as robust mutex.
ReleaseWhenLocked = posix::PTHREAD_MUTEX_ROBUST,
Expand Down
16 changes: 9 additions & 7 deletions iceoryx2-bb/posix/tests/mutex_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,16 @@ fn mutex_can_be_recovered_when_thread_died() {
});
});

let guard = sut.lock();
assert_that!(guard, is_err);
match guard.as_ref().err().as_ref().unwrap() {
MutexLockError::LockAcquiredButOwnerDied(_) => (),
_ => assert_that!(true, eq false),
loop {
let guard = sut.try_lock();

if guard.is_ok() {
assert_that!(guard.as_ref().unwrap(), is_none);
} else if let Err(MutexLockError::LockAcquiredButOwnerDied(_)) = guard {
sut.make_consistent();
break;
}
}
sut.make_consistent();
drop(guard);

let guard = sut.try_lock();
assert_that!(guard, is_ok);
Expand Down

0 comments on commit face950

Please sign in to comment.