summaryrefslogtreecommitdiff
path: root/src/utest/smp/smp_spinlock_tc.c
blob: e6aa702bf0f8e9c365e9340718ba03ca6e86e680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-08-13     RV           the first version
 */

#include <rtthread.h>
#include "utest.h"
#include <interrupt.h>

/**
 * @brief   Spinlock testcase.
 *
 * @note    Create multiple threads and use spinlocks to protect shared memory
 *
 * Test Case Name: [smp_spinlock_tc]
 *
 * Test Objectives:
 * - Test the protection effect of spin locks on shared memory under the SMP architecture.
 *
 * Test Scenarios:
 * - This utest creates two threads. Thread 1 acquires the spin lock, performs the number1++ operation,
 * - then voluntarily enters a sleep state. After being awakened, it executes number2++ and finally
 * - releases the spin lock. Thread 2, upon acquiring the spin lock, first checks whether number1 is
 * - equal to number2, then performs number1++ and number2++ operations, and ultimately releases the
 * - spin lock. Within Thread 2, if the judgment condition number >= 10 is satisfied, finsh_flag is set to 1.
 *
 * Verification Metrics:
 * - Output message: [ PASSED ] [ result ] testcase (core.smp_spinlock)
 *
 * Dependencies:
 * - RT_USING_SMP needs to be enabled.
 *
 * Expected Results:
 * - You will see the PASS message of smp_spinlock_tc.
 */

#define THREAD_PRIORITY   20
#define THREAD_TIMESLICE  20
#define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
static rt_thread_t        thread1;
static rt_thread_t        thread2;
static rt_uint8_t         finsh_flag = 0;
static struct rt_spinlock lock;
static rt_uint8_t         number1, number2 = 0;

static void rt_thread_entry1(void *parameter)
{
    while (1)
    {
        rt_spin_lock(&lock);
        number1++;
        rt_thread_yield();
        number2++;
        rt_spin_unlock(&lock);
        rt_thread_delay(5);
    }
}

static void rt_thread_entry2(void *parameter)
{
    while (1)
    {
        rt_spin_lock(&lock);
        uassert_int_equal(number1, number2);
        number1++;
        number2++;
        rt_spin_unlock(&lock);
        if (number1 >= 10)
        {
            finsh_flag = 1;
        }
        rt_thread_delay(5);
    }
}

static void smp_spinlock_tc(void)
{
    thread1 = rt_thread_create("T1", rt_thread_entry1, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, 20);
    if (thread1 != RT_NULL)
    {
        rt_thread_startup(thread1);
    }

    thread2 = rt_thread_create("T2", rt_thread_entry2, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY - 1, 20);
    if (thread2 != RT_NULL)
    {
        rt_thread_startup(thread2);
    }
    while (finsh_flag == 0);
}

static rt_err_t utest_tc_init(void)
{
    rt_spin_lock_init(&lock);
    return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
    rt_thread_delete(thread1);
    rt_thread_delete(thread2);
    return RT_EOK;
}

static void testcase(void)
{
    UTEST_UNIT_RUN(smp_spinlock_tc);
}
UTEST_TC_EXPORT(testcase, "core.smp_spinlock", utest_tc_init, utest_tc_cleanup, 10);