Skip to content

Commit

Permalink
reworked rcpputils unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
WideAwakeTN committed Jan 5, 2023
1 parent 6f7bb1e commit 21d0b55
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions test/test_mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,30 @@ TEST(test_mutex, pimutex_lockthread) {
test_mutex.lock();
result = 1; // this line should not be reached as long as the mutex is locked
});
std::this_thread::sleep_for(20ms);
std::this_thread::sleep_for(10ms);
EXPECT_EQ(0, result);

test_mutex.unlock();
test_thread.join();
}

TEST(test_mutex, pimutex_priority_inversion) {
rcpputils::PIMutex test_mutex;
template <class MutexClass>
void priority_inheritance_test() {
MutexClass test_mutex;
std::atomic<bool> end_low_prio_thread {false};
std::atomic<bool> end_medium_prio_thread {false};
const unsigned int cpu_bitmask = 1; // allow only one cpu core to be used!

// create low prio thread & take mutex
std::thread low_prio_thread([&test_mutex, &end_low_prio_thread]() {
std::cout << "Low prio thread starts.\n" << std::flush;
test_mutex.lock();
std::cout << "Low prio thread locked.\n" << std::flush;
while (!end_low_prio_thread) {
std::this_thread::sleep_for(1ms);
}
test_mutex.unlock();
std::cout << "Low prio thread unlocked and ends.\n" << std::flush;
});
if (rcpputils::configure_realtime_thread(
low_prio_thread, THREAD_PRIORITY_LOW,
Expand All @@ -125,11 +129,15 @@ TEST(test_mutex, pimutex_priority_inversion) {
test_mutex.unlock();
std::this_thread::sleep_for(1ms);
}
std::cout << "Test thread try_lock failed as expected.\n" << std::flush;

// create high prio thread & take mutex
std::thread high_prio_thread([&test_mutex]() {
std::cout << "High prio thread starts.\n" << std::flush;
test_mutex.lock(); // this call will block initially
std::cout << "High prio thread locked.\n" << std::flush;
test_mutex.unlock();
std::cout << "High prio thread unlocked and ends.\n" << std::flush;
});
EXPECT_TRUE(
rcpputils::configure_realtime_thread(
Expand All @@ -138,30 +146,55 @@ TEST(test_mutex, pimutex_priority_inversion) {

// create medium priority thread that would block the low prio thread
// if there is no priority inheritance
std::thread medium_prio_thread([&end_medium_prio_thread]() {
std::atomic<bool> medium_thread_started {false};
std::thread medium_prio_thread([&end_medium_prio_thread, &medium_thread_started]() {
std::cout << "Medium prio thread starts.\n" << std::flush;
medium_thread_started = true;
// create 100% workload on assigned cpu core
while (!end_medium_prio_thread) {}
std::cout << "Medium prio thread ends.\n" << std::flush;
});
EXPECT_TRUE(
rcpputils::configure_realtime_thread(
medium_prio_thread,
THREAD_PRIORITY_MEDIUM,
cpu_bitmask)) << "THREAD_PRIORITY_MEDIUM could not be set.";
while (medium_thread_started == false) {
std::this_thread::sleep_for(1ms);
}

// do the actual test: see if the low prio thread gets unblocked (through priority inheritance)
std::cout << "Signalling end low prio thread.\n" << std::flush;
end_low_prio_thread = true;
std::this_thread::sleep_for(20ms);

// if priority inheritance worked the mutex should not be locked anymore
bool try_lock_result = test_mutex.try_lock();
EXPECT_TRUE(try_lock_result) << "Mutex should not be locked anymore.";
bool try_lock_result;
int count = 0;
while((try_lock_result = test_mutex.try_lock()) == false)
{
std::this_thread::sleep_for(1ms);
if(count++ >= 20)
{
EXPECT_TRUE(try_lock_result) << "Mutex should not be locked anymore.";
break;
}
}
if (try_lock_result) {
test_mutex.unlock();
}

// cleanup
low_prio_thread.detach();
high_prio_thread.detach();
std::cout << "Signalling end medium prio thread.\n" << std::flush;
end_medium_prio_thread = true;
medium_prio_thread.join();
high_prio_thread.join();
low_prio_thread.join();
}

TEST(test_mutex, pimutex_priority_inversion) {
priority_inheritance_test<rcpputils::PIMutex>();
}

TEST(test_mutex, rpimutex_priority_inversion) {
priority_inheritance_test<rcpputils::RecursivePIMutex>();
}

0 comments on commit 21d0b55

Please sign in to comment.