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
33 #include "it_los_atomic.h"
34
35 #define DB_ATOMIC_MUTI_TASK_NUM (ATOMIC_MUTI_TASK_NUM * 2) // 2 is coefficients
36
TaskF01(VOID)37 static VOID TaskF01(VOID)
38 {
39 UINT32 i;
40 for (i = 0; i < 10; ++i) { // run 10 times.
41 LOS_Atomic64Dec(&g_testAtomicID05);
42 }
43
44 ++g_testCount;
45 }
46
TaskF02(VOID)47 static VOID TaskF02(VOID)
48 {
49 UINT64 i;
50 for (i = 0; i < 10; ++i) { // run 10 times.
51 LOS_AtomicCmpXchg64bits(&g_testAtomicID05, g_testAtomicID05, g_testAtomicID05);
52 }
53
54 ++g_testCount;
55 }
56
TestCase(VOID)57 static UINT32 TestCase(VOID)
58 {
59 UINT32 ret, i;
60 UINT32 taskId[DB_ATOMIC_MUTI_TASK_NUM];
61 TSK_INIT_PARAM_S task[DB_ATOMIC_MUTI_TASK_NUM] = {0, };
62 CHAR taskName[DB_ATOMIC_MUTI_TASK_NUM][20] = {"", }; // max taskName size is 20.
63 CHAR buf[10] = ""; // max buf size is 10.
64 Atomic uCount = 0;
65 g_testCount = 0;
66 g_testAtomicID05 = 0xffffffffff;
67 UINT32 uLoop = g_taskMaxNum - TASK_EXISTED_NUM;
68
69 if (uLoop > DB_ATOMIC_MUTI_TASK_NUM) {
70 uLoop = DB_ATOMIC_MUTI_TASK_NUM;
71 }
72
73 for (i = 0; i < uLoop; i++) {
74 ret = memset_s(buf, 10, 0, 10); // max buf size is 10.
75 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
76 ret = memset_s(taskName[i], 20, 0, 20); // max taskName size is 20.
77 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
78
79 if (i % 2 == 0) { // 2 is index.
80 uCount++;
81 task[i].pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF01;
82 } else {
83 task[i].pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF02;
84 }
85 task[i].pcName = taskName[i];
86 task[i].uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
87 task[i].usTaskPrio = TASK_PRIO_TEST - 2; // TASK_PRIO_TEST - 2 has higher priority than TASK_PRIO_TEST
88 task[i].uwResved = LOS_TASK_STATUS_DETACHED;
89 }
90 for (i = 0; i < uLoop; i++) {
91 ret = LOS_TaskCreate(&taskId[i], &task[i]);
92 ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
93 }
94
95 LOS_TaskDelay(20); // delay 20 ticks.
96 ICUNIT_GOTO_EQUAL(g_testCount, uLoop, g_testCount, EXIT);
97 ICUNIT_GOTO_EQUAL(g_testAtomicID05, (0xffffffffff - 10 * uCount), g_testAtomicID05, EXIT); // run 10 times.
98 EXIT:
99 for (i = 0; i < uLoop; i++) {
100 (VOID)LOS_TaskDelete(taskId[i]);
101 }
102 return LOS_OK;
103 }
104
105 /**
106 * @ingroup TEST_ATO
107 * @par TestCase_Number
108 * ItLosAtomic009
109 * @par TestCase_TestCase_Type
110 * Function test
111 * @brief Test interface LOS_Atomic64Dec and LOS_AtomicCmpXchg64bits in different task.
112 * @par TestCase_Pretreatment_Condition
113 * NA.
114 * @par TestCase_Test_Steps
115 * step1: Invoke the LOS_Atomic64Dec interface in TaskF01.
116 * step2: Invoke the LOS_AtomicCmpXchg64bits interface in TaskF02.
117 * @par TestCase_Expected_Result
118 * 1.LOS_Atomic64Dec and LOS_AtomicCmpXchg64bits return expected result.
119 * @par TestCase_Level
120 * Level 0
121 * @par TestCase_Automated
122 * true
123 * @par TestCase_Remark
124 * null
125 */
126
ItLosAtomic009(VOID)127 VOID ItLosAtomic009(VOID)
128 {
129 TEST_ADD_CASE("ItLosAtomic009", TestCase, TEST_LOS, TEST_ATO, TEST_LEVEL0, TEST_FUNCTION);
130 }
131