|
6 | 6 | * Change Logs: |
7 | 7 | * Date Author Notes |
8 | 8 | * 2024-01-17 Shell the first version |
| 9 | + * 2025-12-12 lhxj Add standardized utest documentation block |
9 | 10 | */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Test Case Name: Priority Based Semaphore Synchronization Test (core.scheduler_sem) |
| 14 | + * |
| 15 | + * Test Objectives: |
| 16 | + * - Verify the stability and correctness of the scheduler under high concurrency. |
| 17 | + * - Verify thread synchronization and execution order using Semaphore chains across different priority levels. |
| 18 | + * - Verify SMP (Symmetric Multiprocessing) load balancing and atomic operations in a multi-core environment. |
| 19 | + * - List specific functions or APIs to be tested: |
| 20 | + * - rt_sem_init |
| 21 | + * - rt_sem_take |
| 22 | + * - rt_sem_release |
| 23 | + * - rt_thread_create |
| 24 | + * - rt_thread_startup |
| 25 | + * - rt_atomic_add |
| 26 | + * |
| 27 | + * Test Scenarios: |
| 28 | + * - **Semaphore Chained Scheduling:** |
| 29 | + * 1. Initialize a "thread matrix" where threads are created across multiple priority levels (`TEST_LEVEL_COUNTS`). |
| 30 | + * 2. For each priority level, create multiple concurrent threads (`RT_CPUS_NR * 2`). |
| 31 | + * 3. Establish a dependency chain (Ring Topology): |
| 32 | + * - **Level 0 threads:** Notify Level 1, then wait for their own resource. |
| 33 | + * - **Middle Level threads:** Wait for their resource (notified by Level N-1), then notify Level N+1. |
| 34 | + * - **Last Level threads:** Wait for their resource, print status (CPU ID), delay, then notify Level 0. |
| 35 | + * 4. Each thread increments an atomic load counter for the specific CPU it is running on. |
| 36 | + * 5. The main test thread waits for all sub-threads to signal completion via `_thr_exit_sem`. |
| 37 | + * |
| 38 | + * Verification Metrics: |
| 39 | + * - **Pass:** All created threads must complete their execution loops without deadlocking. |
| 40 | + * - **Pass:** The sum of execution counts across all CPUs (`_load_average`) must equal the calculated expected total (`KERN_TEST_CONFIG_LOOP_TIMES * TEST_LEVEL_COUNTS * KERN_TEST_CONCURRENT_THREADS`). |
| 41 | + * |
| 42 | + * Dependencies: |
| 43 | + * - Hardware requirements |
| 44 | + * - No specific peripherals required, but multi-core CPU is recommended for SMP verification. |
| 45 | + * (This is met by the qemu-virt64-riscv BSP). |
| 46 | + * - Software configuration |
| 47 | + * - `RT_USING_UTEST` must be enabled (`RT-Thread Utestcases`). |
| 48 | + * - `Scheduler Test` must be enabled (`RT-Thread Utestcases` -> `Kernel Core` -> 'Scheduler Test'). |
| 49 | + * - (Optional) Enable SMP for parallel testing (Highly Recommended): |
| 50 | + * - Go to `RT-Thread Kernel` -> `Enable SMP (Symmetric multiprocessing)`. |
| 51 | + * - Set `Number of CPUs` to > 1 (e.g., 2 or 4). |
| 52 | + * - Environmental assumptions |
| 53 | + * - The system must support enough valid priority levels (`RT_THREAD_PRIORITY_MAX`) to accommodate `TEST_LEVEL_COUNTS`. |
| 54 | + * - Run the test case from the msh prompt: |
| 55 | + * `utest_run core.scheduler_sem` |
| 56 | + * |
| 57 | + * Expected Results: |
| 58 | + * - The console should print character patterns (e.g., `*0*1...`) indicating thread activity on specific CPUs. |
| 59 | + * - The final load statistics per CPU should be printed. |
| 60 | + * - Final Output: `[ PASSED ] [ result ] testcase (core.scheduler_sem)` |
| 61 | + */ |
| 62 | + |
10 | 63 | #define __RT_IPC_SOURCE__ |
11 | 64 |
|
12 | 65 | #include <rtthread.h> |
|
0 commit comments