SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
test_condvar.cpp File Reference
#include <stk_config.h>
#include <stk.h>
#include <sync/stk_sync_mutex.h>
#include <sync/stk_sync_cv.h>
#include <assert.h>
#include <string.h>
#include "stktest_context.h"
Include dependency graph for test_condvar.cpp:

Go to the source code of this file.

Classes

class  stk::test::condvar::NotifyOneWakesTask< _AccessMode >
 Tests that NotifyOne() wakes exactly one waiting task per call. More...
class  stk::test::condvar::NotifyAllWakesTask< _AccessMode >
 Tests that NotifyAll() wakes every waiting task in one call. More...
class  stk::test::condvar::TimeoutExpiresTask< _AccessMode >
 Tests that Wait() returns false within the expected time when no notification arrives. More...
class  stk::test::condvar::MutexReacquiredTask< _AccessMode >
 Tests that Wait() atomically releases the mutex and re-acquires it before returning. More...
class  stk::test::condvar::PredicateLoopTask< _AccessMode >
 Tests the canonical spurious-wakeup-safe predicate loop pattern. More...
class  stk::test::condvar::NotifyOneOrderTask< _AccessMode >
 Tests that NotifyOne() releases waiters in FIFO arrival order. More...
class  stk::test::condvar::NoWaitTimeoutTask< _AccessMode >
 Tests that Wait(NO_WAIT) returns false immediately without blocking. More...
class  stk::test::condvar::StressTestTask< _AccessMode >
 Stress test of ConditionVariable under full five-task contention. More...

Namespaces

namespace  stk
 Namespace of STK package.
namespace  stk::test
 Namespace of the test inventory.
namespace  stk::test::condvar
 Namespace of ConditionVariable test.

Macros

#define _STK_CV_TEST_TASKS_MAX   5
#define _STK_CV_TEST_TIMEOUT   300
#define _STK_CV_TEST_SHORT_SLEEP   10
#define _STK_CV_TEST_LONG_SLEEP   100
#define _STK_CV_STACK_SIZE   256
#define STK_TASK   static

Functions

void * operator new (std::size_t, void *ptr) noexcept
void operator delete (void *, void *) noexcept
static void stk::test::condvar::ResetTestState ()
static bool NeedsExtendedTasks (const char *test_name)
template<class TaskType>
static int32_t RunTest (const char *test_name, int32_t param=0)
int main (int argc, char **argv)

Variables

 STK_TEST_DECL_ASSERT
static volatile int32_t stk::test::condvar::g_TestResult = 0
static volatile int32_t stk::test::condvar::g_SharedCounter = 0
static volatile int32_t stk::test::condvar::g_AcquisitionOrder [5] = {0}
static volatile int32_t stk::test::condvar::g_OrderIndex = 0
static volatile bool stk::test::condvar::g_TestComplete = false
static volatile int32_t stk::test::condvar::g_InstancesDone = 0
static Kernel< KERNEL_DYNAMIC|KERNEL_SYNC, 5, SwitchStrategyRR, PlatformDefaultstk::test::condvar::g_Kernel
static sync::Mutex stk::test::condvar::g_TestMutex
static sync::ConditionVariable stk::test::condvar::g_TestCond

Macro Definition Documentation

◆ _STK_CV_STACK_SIZE

#define _STK_CV_STACK_SIZE   256

Definition at line 32 of file test_condvar.cpp.

◆ _STK_CV_TEST_LONG_SLEEP

◆ _STK_CV_TEST_SHORT_SLEEP

◆ _STK_CV_TEST_TASKS_MAX

◆ _STK_CV_TEST_TIMEOUT

◆ STK_TASK

#define STK_TASK   static

Definition at line 33 of file test_condvar.cpp.

Referenced by RunTest(), RunTest(), RunTest(), RunTest(), RunTest(), RunTest(), RunTest(), RunTest(), and RunTest().

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 635 of file test_condvar.cpp.

636{
637 (void)argc;
638 (void)argv;
639
640 using namespace stk::test::condvar;
641
643
644 int total_failures = 0, total_success = 0;
645
646 printf("--------------\n");
647
648 g_Kernel.Initialize();
649
650#ifndef __ARM_ARCH_6M__
651
652 // Test 1: NotifyOne() wakes exactly one waiting task per call
654 total_failures++;
655 else
656 total_success++;
657
658 // Test 2: NotifyAll() wakes every waiting task in a single call
660 total_failures++;
661 else
662 total_success++;
663
664 // Test 3: Wait() returns false within correct time window when no notification arrives (tasks 0-2 only)
666 total_failures++;
667 else
668 total_success++;
669
670 // Test 4: Wait() atomically releases mutex and re-acquires it before returning (tasks 0-2 only)
672 total_failures++;
673 else
674 total_success++;
675
676 // Test 5: Spurious-wakeup-safe predicate loop correctly drives all consumers to completion
678 total_failures++;
679 else
680 total_success++;
681
682 // Test 6: NotifyOne() releases waiters in FIFO arrival order
684 total_failures++;
685 else
686 total_success++;
687
688 // Test 7: Wait(NO_WAIT) returns false immediately without blocking (tasks 0-2 only)
690 total_failures++;
691 else
692 total_success++;
693
694#endif // __ARM_ARCH_6M__
695
696 // Test 8: Stress test under full five-task contention with alternating producer/consumer roles
698 total_failures++;
699 else
700 total_success++;
701
702 int32_t final_result = (total_failures == 0 ? TestContext::SUCCESS_EXIT_CODE : TestContext::DEFAULT_FAILURE_EXIT_CODE);
703
704 printf("##############\n");
705 printf("Total tests: %d\n", total_failures + total_success);
706 printf("Failures: %d\n", total_failures);
707
709 return final_result;
710}
static int32_t RunTest(const char *test_name, int32_t param=0)
Namespace of ConditionVariable test.
static Kernel< KERNEL_DYNAMIC|KERNEL_SYNC, 5, SwitchStrategyRR, PlatformDefault > g_Kernel
Tests that NotifyOne() wakes exactly one waiting task per call.
Tests that NotifyAll() wakes every waiting task in one call.
Tests that Wait() returns false within the expected time when no notification arrives.
Tests that Wait() atomically releases the mutex and re-acquires it before returning.
Tests the canonical spurious-wakeup-safe predicate loop pattern.
Tests that NotifyOne() releases waiters in FIFO arrival order.
Tests that Wait(NO_WAIT) returns false immediately without blocking.
Stress test of ConditionVariable under full five-task contention.
static void ShowTestSuitePrologue()
Show text string as prologue before tests start.
@ DEFAULT_FAILURE_EXIT_CODE
default exit code for exit() to denote failure of the test
@ SUCCESS_EXIT_CODE
exit code for exit() to denote the success of the test
static void ShowTestSuiteEpilogue(int32_t result)
Show text string as epilogue after tests end.

References stk::test::TestContext::DEFAULT_FAILURE_EXIT_CODE, stk::test::condvar::g_Kernel, RunTest(), stk::test::TestContext::ShowTestSuiteEpilogue(), stk::test::TestContext::ShowTestSuitePrologue(), and stk::test::TestContext::SUCCESS_EXIT_CODE.

Here is the call graph for this function:

◆ NeedsExtendedTasks()

bool NeedsExtendedTasks ( const char * test_name)
static

Definition at line 584 of file test_condvar.cpp.

585{
586 return (strcmp(test_name, "TimeoutExpires") != 0) &&
587 (strcmp(test_name, "MutexReacquired") != 0) &&
588 (strcmp(test_name, "NoWaitTimeout") != 0);
589}

Referenced by RunTest().

Here is the caller graph for this function:

◆ operator delete()

void operator delete ( void * ,
void *  )
inlinenoexcept

Definition at line 38 of file test_condvar.cpp.

38{ /* nothing for placement delete */ }

◆ operator new()

void * operator new ( std::size_t ,
void * ptr )
inlinenoexcept

Definition at line 37 of file test_condvar.cpp.

37{ return ptr; }

◆ RunTest()

template<class TaskType>
int32_t RunTest ( const char * test_name,
int32_t param = 0 )
static

Definition at line 595 of file test_condvar.cpp.

596{
597 using namespace stk;
598 using namespace stk::test;
599 using namespace stk::test::condvar;
600
601 printf("Test: %s\n", test_name);
602
604
605 // Create tasks based on test type
606 STK_TASK TaskType task0(0, param);
607 STK_TASK TaskType task1(1, param);
608 STK_TASK TaskType task2(2, param);
609 TaskType task3(3, param);
610 TaskType task4(4, param);
611
612 g_Kernel.AddTask(&task0);
613 g_Kernel.AddTask(&task1);
614 g_Kernel.AddTask(&task2);
615
616 if (NeedsExtendedTasks(test_name))
617 {
618 g_Kernel.AddTask(&task3);
619 g_Kernel.AddTask(&task4);
620 }
621
622 g_Kernel.Start();
623
625
626 printf("Result: %s\n", result == TestContext::SUCCESS_EXIT_CODE ? "PASS" : "FAIL");
627 printf("--------------\n");
628
629 return result;
630}
#define STK_TASK
static bool NeedsExtendedTasks(const char *test_name)
Namespace of STK package.
Namespace of the test inventory.
static TestTask< ACCESS_PRIVILEGED > task3(2)
static TestTask< ACCESS_PRIVILEGED > task2(1)
static volatile int32_t g_TestResult
static void ResetTestState()

References stk::test::TestContext::DEFAULT_FAILURE_EXIT_CODE, stk::test::condvar::g_Kernel, stk::test::condvar::g_TestResult, NeedsExtendedTasks(), stk::test::condvar::ResetTestState(), STK_TASK, and stk::test::TestContext::SUCCESS_EXIT_CODE.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ STK_TEST_DECL_ASSERT

STK_TEST_DECL_ASSERT

Definition at line 22 of file test_condvar.cpp.