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_mutex.cpp File Reference
#include <stk_config.h>
#include <stk.h>
#include <sync/stk_sync_mutex.h>
#include <assert.h>
#include <string.h>
#include "stktest_context.h"
Include dependency graph for test_mutex.cpp:

Go to the source code of this file.

Classes

class  stk::test::mutex::BasicLockUnlockTask< _AccessMode >
 Tests basic lock/unlock functionality. More...
class  stk::test::mutex::RecursiveLockTask< _AccessMode >
 Tests recursive locking capability. More...
class  stk::test::mutex::TryLockTask< _AccessMode >
 Tests TryLock() non-blocking behavior. More...
class  stk::test::mutex::TimedLockTask< _AccessMode >
 Tests TimedLock() timeout behavior. More...
class  stk::test::mutex::FIFOOrderTask< _AccessMode >
 Tests FIFO ordering of waiting threads. More...
class  stk::test::mutex::StressTestTask< _AccessMode >
 Stress test with many lock/unlock cycles. More...
class  stk::test::mutex::RecursiveDepthTask< _AccessMode >
 Tests deep recursive locking. More...
class  stk::test::mutex::InterTaskCoordinationTask< _AccessMode >
 Tests mutex for coordinating work between tasks. More...

Namespaces

namespace  stk
 Namespace of STK package.
namespace  stk::test
 Namespace of the test inventory.
namespace  stk::test::mutex
 Namespace of Mutex test.

Macros

#define _STK_MUTEX_TEST_TASKS_MAX   5
#define _STK_MUTEX_TEST_TIMEOUT   1000
#define _STK_MUTEX_TEST_SHORT_SLEEP   10
#define _STK_MUTEX_TEST_LONG_SLEEP   100
#define _STK_MUTEX_STACK_SIZE   256
#define STK_TASK   static

Functions

static void stk::test::mutex::ResetTestState ()
static bool NeedsExtendedTasks (const char *test_name)
static bool NeedsThreeTasks (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::mutex::g_TestResult = 0
static volatile int32_t stk::test::mutex::g_SharedCounter = 0
static volatile int32_t stk::test::mutex::g_AcquisitionOrder [5] = {0}
static volatile int32_t stk::test::mutex::g_OrderIndex = 0
static volatile bool stk::test::mutex::g_TestComplete = false
static volatile int32_t stk::test::mutex::g_InstancesDone = 0
static Kernel< KERNEL_DYNAMIC|KERNEL_SYNC|(STK_TICKLESS_IDLE ? KERNEL_TICKLESS :0), 5, SwitchStrategyRR, PlatformDefaultstk::test::mutex::g_Kernel
static sync::Mutex stk::test::mutex::g_TestMutex

Macro Definition Documentation

◆ _STK_MUTEX_STACK_SIZE

#define _STK_MUTEX_STACK_SIZE   256

Definition at line 31 of file test_mutex.cpp.

◆ _STK_MUTEX_TEST_LONG_SLEEP

◆ _STK_MUTEX_TEST_SHORT_SLEEP

◆ _STK_MUTEX_TEST_TASKS_MAX

◆ _STK_MUTEX_TEST_TIMEOUT

#define _STK_MUTEX_TEST_TIMEOUT   1000

Definition at line 24 of file test_mutex.cpp.

◆ STK_TASK

#define STK_TASK   static

Definition at line 32 of file test_mutex.cpp.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 588 of file test_mutex.cpp.

589{
590 (void)argc;
591 (void)argv;
592
593 using namespace stk::test::mutex;
594
596
597 int total_failures = 0, total_success = 0;
598
599 printf("--------------\n");
600
601 g_Kernel.Initialize();
602
603#ifndef __ARM_ARCH_6M__
604
605 // Test 1: Basic Lock/Unlock with mutual exclusion
607 total_failures++;
608 else
609 total_success++;
610
611 // Test 2: Recursive locking
613 total_failures++;
614 else
615 total_success++;
616
617 // Test 3: TryLock non-blocking behavior
619 total_failures++;
620 else
621 total_success++;
622
623 // Test 4: TimedLock timeout behavior
625 total_failures++;
626 else
627 total_success++;
628
629 // Test 5: FIFO ordering
631 total_failures++;
632 else
633 total_success++;
634
635 // Test 6: Deep recursion
637 total_failures++;
638 else
639 total_success++;
640
641 // Test 7: Inter-task coordination
643 total_failures++;
644 else
645 total_success++;
646
647#endif // __ARM_ARCH_6M__
648
649 // Test 8: Stress test
651 total_failures++;
652 else
653 total_success++;
654
655 int32_t final_result = (total_failures == 0 ? TestContext::SUCCESS_EXIT_CODE : TestContext::DEFAULT_FAILURE_EXIT_CODE);
656
657 printf("##############\n");
658 printf("Total tests: %d\n", total_failures + total_success);
659 printf("Failures: %d\n", (int)total_failures);
660
662 return final_result;
663}
static int32_t RunTest(const char *test_name, int32_t param=0)
Namespace of Mutex test.
static Kernel< KERNEL_DYNAMIC|KERNEL_SYNC|(STK_TICKLESS_IDLE ? KERNEL_TICKLESS :0), 5, SwitchStrategyRR, PlatformDefault > g_Kernel
Tests basic lock/unlock functionality.
Tests recursive locking capability.
Tests TryLock() non-blocking behavior.
Tests TimedLock() timeout behavior.
Tests FIFO ordering of waiting threads.
Stress test with many lock/unlock cycles.
Tests deep recursive locking.
Tests mutex for coordinating work between tasks.
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::mutex::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 527 of file test_mutex.cpp.

528{
529 return (strcmp(test_name, "TryLock") != 0) &&
530 (strcmp(test_name, "TimedLock") != 0);
531}

Referenced by RunTest().

Here is the caller graph for this function:

◆ NeedsThreeTasks()

bool NeedsThreeTasks ( const char * test_name)
static

Definition at line 537 of file test_mutex.cpp.

538{
539 return (strcmp(test_name, "TryLock") != 0);
540}

Referenced by RunTest().

Here is the caller graph for this function:

◆ RunTest()

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

Definition at line 546 of file test_mutex.cpp.

547{
548 using namespace stk;
549 using namespace stk::test;
550 using namespace stk::test::mutex;
551
552 printf("Test: %s\n", test_name);
553
555
556 // Create tasks based on test type
557 STK_TASK TaskType task0(0, param);
558 STK_TASK TaskType task1(1, param);
559 TaskType task2(2, param);
560 TaskType task3(3, param);
561 TaskType task4(4, param);
562
563 g_Kernel.AddTask(&task0);
564 g_Kernel.AddTask(&task1);
565
566 if (NeedsThreeTasks(test_name))
567 g_Kernel.AddTask(&task2);
568
569 if (NeedsExtendedTasks(test_name))
570 {
571 g_Kernel.AddTask(&task3);
572 g_Kernel.AddTask(&task4);
573 }
574
575 g_Kernel.Start();
576
578
579 printf("Result: %s\n", result == TestContext::SUCCESS_EXIT_CODE ? "PASS" : "FAIL");
580 printf("--------------\n");
581
582 return result;
583}
#define STK_TASK
static bool NeedsThreeTasks(const char *test_name)
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::mutex::g_Kernel, stk::test::mutex::g_TestResult, NeedsExtendedTasks(), NeedsThreeTasks(), stk::test::mutex::ResetTestState(), STK_TASK, and stk::test::TestContext::SUCCESS_EXIT_CODE.

Here is the call graph for this function:

Variable Documentation

◆ STK_TEST_DECL_ASSERT

STK_TEST_DECL_ASSERT

Definition at line 21 of file test_mutex.cpp.