1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "It_los_mux.h"
33
34 #ifdef __cplusplus
35 #if __cplusplus
36 extern "C" {
37 #endif /* __cplusplus */
38 #endif /* __cplusplus */
39
TaskD1Func(VOID)40 static VOID TaskD1Func(VOID)
41 {
42 UINT32 ret;
43 g_testCount++;
44
45 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 4, g_testCount); // 4, here assert the result.
46
47 ret = LOS_MuxLock(&g_mutexTest1, LOS_WAIT_FOREVER);
48 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
49
50 ret = LOS_MuxUnlock(&g_mutexTest1);
51 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
52
53 g_testCount++;
54 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 5, g_testCount); // 5, here assert the result.
55 UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
56 ICUNIT_ASSERT_EQUAL_VOID(priority, 2, priority); // 2, here assert the result.
57 g_testCount++;
58 }
59
TaskC1Func(VOID)60 static VOID TaskC1Func(VOID)
61 {
62 UINT32 ret;
63 g_testCount++;
64
65 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 3, g_testCount); // 3, here assert the result.
66
67 ret = LOS_MuxLock(&g_mutexTest1, LOS_WAIT_FOREVER);
68 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
69
70 ret = LOS_MuxUnlock(&g_mutexTest1);
71 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
72
73 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 6, g_testCount); // 6, here assert the result.
74
75 UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
76 ICUNIT_ASSERT_EQUAL_VOID(priority, 3, priority); // 3, here assert the result.
77 g_testCount++;
78 }
79
TaskB1Func(VOID)80 static VOID TaskB1Func(VOID)
81 {
82 UINT32 ret;
83 g_testCount++;
84
85 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 2, g_testCount); // 2, here assert the result.
86
87 ret = LOS_MuxLock(&g_mutexTest1, LOS_WAIT_FOREVER);
88 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
89
90 ret = LOS_MuxUnlock(&g_mutexTest1);
91 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
92
93 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 7, g_testCount); // 7, here assert the result.
94
95 UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
96 ICUNIT_ASSERT_EQUAL_VOID(priority, 5, priority); // 5, here assert the result.
97 g_testCount++;
98 }
99
TaskA1Func(VOID)100 static VOID TaskA1Func(VOID)
101 {
102 UINT32 ret;
103 TSK_INIT_PARAM_S task1 = {0};
104 TSK_INIT_PARAM_S task2 = {0};
105 TSK_INIT_PARAM_S task3 = {0};
106 g_testCount++;
107
108 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 1, g_testCount);
109
110 ret = LOS_MuxLock(&g_mutexTest1, 0);
111 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
112
113 task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskB1Func;
114 task1.usTaskPrio = 5; // 5, set reasonable priority.
115 task1.pcName = "TaskB";
116 task1.uwStackSize = TASK_STACK_SIZE_TEST;
117 task1.uwResved = LOS_TASK_STATUS_DETACHED;
118 #ifdef LOSCFG_KERNEL_SMP
119 task1.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
120 #endif
121 ret = LOS_TaskCreate(&g_testTaskID02, &task1);
122 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
123
124 task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskC1Func;
125 task2.usTaskPrio = 3; // 3, set reasonable priority.
126 task2.pcName = "TaskC";
127 task2.uwStackSize = TASK_STACK_SIZE_TEST;
128 task2.uwResved = LOS_TASK_STATUS_DETACHED;
129 #ifdef LOSCFG_KERNEL_SMP
130 task2.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
131 #endif
132 ret = LOS_TaskCreate(&g_testTaskID03, &task2);
133 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
134
135 task3.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskD1Func;
136 task3.usTaskPrio = 2; // 2, set reasonable priority.
137 task3.pcName = "TaskD";
138 task3.uwStackSize = TASK_STACK_SIZE_TEST;
139 task3.uwResved = LOS_TASK_STATUS_DETACHED;
140 #ifdef LOSCFG_KERNEL_SMP
141 task3.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
142 #endif
143 ret = LOS_TaskCreate(&g_testTaskID04, &task3);
144 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
145
146 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 4, g_testCount); // 4, here assert the result.
147
148 ret = LOS_MuxUnlock(&g_mutexTest1);
149 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
150
151 ret = LOS_MuxDestroy(&g_mutexTest1);
152 ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
153
154 UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
155 ICUNIT_ASSERT_EQUAL_VOID(priority, 10, priority); // 10, here assert the result.
156
157 ICUNIT_ASSERT_EQUAL_VOID(g_testCount, 8, g_testCount); // 8, here assert the result.
158 }
159
Testcase(void)160 static UINT32 Testcase(void)
161 {
162 UINT32 ret;
163 TSK_INIT_PARAM_S task = {0};
164 g_testCount = 0;
165
166 ret = LosMuxCreate(&g_mutexTest1);
167 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
168
169 task.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskA1Func;
170 task.usTaskPrio = 10; // 10, set reasonable priority.
171 task.pcName = "TaskA";
172 task.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
173 task.uwResved = LOS_TASK_STATUS_DETACHED;
174 #ifdef LOSCFG_KERNEL_SMP
175 task.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
176 #endif
177 ret = LOS_TaskCreate(&g_testTaskID01, &task);
178 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
179
180 ICUNIT_ASSERT_EQUAL(g_testCount, 8, g_testCount); // 8, here assert the result.
181 UINT16 priority = LOS_TaskPriGet(OsCurrTaskGet()->taskID);
182 ICUNIT_ASSERT_EQUAL(priority, 25, priority); // 25, here assert the result.
183 return LOS_OK;
184 }
185
ItLosMux027(void)186 VOID ItLosMux027(void)
187 {
188 TEST_ADD_CASE("ItLosMux027", Testcase, TEST_LOS, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
189 }
190
191 #ifdef __cplusplus
192 #if __cplusplus
193 }
194 #endif /* __cplusplus */
195 #endif /* __cplusplus */
196