1 /*
2 * Copyright (c) 2006-2024, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2024-08-13 RV the first version
9 */
10
11 #include <rtthread.h>
12 #include "utest.h"
13 #include <interrupt.h>
14
15 /**
16 * @brief Spinlock testcase.
17 *
18 * @note Create multiple threads and use spinlocks to protect shared memory
19 */
20
21 #define THREAD_PRIORITY 20
22 #define THREAD_TIMESLICE 20
23 #define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
24 static rt_thread_t thread1;
25 static rt_thread_t thread2;
26 static rt_uint8_t finsh_flag = 0;
27 static struct rt_spinlock lock;
28 static rt_uint8_t number1, number2 = 0;
29
rt_thread_entry1(void * parameter)30 static void rt_thread_entry1(void *parameter)
31 {
32 while (1)
33 {
34 rt_spin_lock(&lock);
35 number1++;
36 rt_thread_yield();
37 number2++;
38 rt_spin_unlock(&lock);
39 rt_thread_delay(5);
40 }
41 }
42
rt_thread_entry2(void * parameter)43 static void rt_thread_entry2(void *parameter)
44 {
45 while (1)
46 {
47 rt_spin_lock(&lock);
48 uassert_int_equal(number1, number2);
49 number1++;
50 number2++;
51 rt_spin_unlock(&lock);
52 if (number1 >= 10)
53 {
54 finsh_flag = 1;
55 }
56 rt_thread_delay(5);
57 }
58 }
59
smp_spinlock_tc(void)60 static void smp_spinlock_tc(void)
61 {
62 thread1 = rt_thread_create("T1", rt_thread_entry1, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, 20);
63 if (thread1 != RT_NULL)
64 {
65 rt_thread_startup(thread1);
66 }
67
68 thread2 = rt_thread_create("T2", rt_thread_entry2, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY - 1, 20);
69 if (thread2 != RT_NULL)
70 {
71 rt_thread_startup(thread2);
72 }
73 while (finsh_flag == 0);
74 }
75
utest_tc_init(void)76 static rt_err_t utest_tc_init(void)
77 {
78 rt_spin_lock_init(&lock);
79 return RT_EOK;
80 }
81
utest_tc_cleanup(void)82 static rt_err_t utest_tc_cleanup(void)
83 {
84 rt_thread_delete(thread1);
85 rt_thread_delete(thread2);
86 return RT_EOK;
87 }
88
testcase(void)89 static void testcase(void)
90 {
91 UTEST_UNIT_RUN(smp_spinlock_tc);
92 }
93 UTEST_TC_EXPORT(testcase, "testcases.smp.spinlock_tc", utest_tc_init, utest_tc_cleanup, 10);
94