1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "It_posix_mutex.h"
32
33 static pthread_mutex_t g_mutex075;
34 static sem_t g_sem075;
35 static int g_pshared075;
36
TaskF01(void * arg)37 static VOID *TaskF01(void *arg)
38 {
39 UINT32 ret;
40 ret = pthread_mutex_lock(&g_mutex075);
41 ICUNIT_TRACK_EQUAL(ret, 0, ret);
42
43 g_testCount++;
44 ICUNIT_TRACK_EQUAL(g_testCount, 3, g_testCount); // 3, Here, assert that g_testCount is equal to 3.
45
46 ret = sem_wait(&g_sem075);
47 ICUNIT_TRACK_EQUAL(ret, 0, ret);
48
49 g_testCount++;
50 ICUNIT_TRACK_EQUAL(g_testCount, 6, g_testCount); // 6, Here, assert that g_testCount is equal to 6.
51
52 ret = sem_post(&g_sem075);
53 ICUNIT_TRACK_EQUAL(ret, 0, ret);
54
55 g_testCount++;
56 ICUNIT_TRACK_EQUAL(g_testCount, 7, g_testCount); // 7, Here, assert that g_testCount is equal to 7.
57
58 ret = pthread_mutex_unlock(&g_mutex075);
59 ICUNIT_TRACK_EQUAL(ret, 0, ret);
60
61 return NULL;
62 }
63
TaskF02(void * arg)64 static VOID *TaskF02(void *arg)
65 {
66 UINT32 ret;
67 LOS_TaskDelay(2); // 2, set delay time.
68
69 g_testCount++;
70 ICUNIT_TRACK_EQUAL(g_testCount, 4, g_testCount); // 4, Here, assert that g_testCount is equal to 4.
71
72 ret = pthread_mutex_lock(&g_mutex075);
73 ICUNIT_TRACK_EQUAL(ret, 0, ret); //
74
75 g_testCount++;
76 ICUNIT_TRACK_EQUAL(g_testCount, 8, g_testCount); // 8, Here, assert that g_testCount is equal to 8.
77
78 ret = pthread_mutex_unlock(&g_mutex075);
79 ICUNIT_TRACK_EQUAL(ret, 0, ret);
80
81 return NULL;
82 }
83
TaskF03(void * arg)84 static VOID *TaskF03(void *arg)
85 {
86 UINT32 ret;
87
88 g_testCount++;
89 ICUNIT_TRACK_EQUAL(g_testCount, 1, g_testCount);
90
91 g_testCount++;
92 ICUNIT_TRACK_EQUAL(g_testCount, 2, g_testCount); // 2, Here, assert that g_testCount is equal to 2.
93
94 LOS_TaskDelay(3); // 3, set delay time.
95
96 g_testCount++;
97 ICUNIT_TRACK_EQUAL(g_testCount, 5, g_testCount); // 5, Here, assert that g_testCount is equal to 5.
98
99 ret = sem_post(&g_sem075);
100 ICUNIT_TRACK_EQUAL(ret, 0, ret);
101
102 LOS_TaskDelay(1);
103 g_testCount++;
104 ICUNIT_TRACK_EQUAL(g_testCount, 9, g_testCount); // 9, Here, assert the g_testCount.
105
106 return NULL;
107 }
108
Testcase(VOID)109 static UINT32 Testcase(VOID)
110 {
111 UINT32 ret;
112 pthread_t newTh;
113 pthread_attr_t attr;
114 pthread_t newTh2;
115 pthread_attr_t attr2;
116 pthread_t newTh3;
117 pthread_attr_t attr3;
118 g_testCount = 0;
119
120 ret = pthread_mutex_init(&g_mutex075, NULL);
121 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
122
123 ret = sem_init(&g_sem075, g_pshared075, 0);
124 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
125
126 ret = PosixPthreadInit(&attr, MUTEX_TEST_DEFAULT_PRIO);
127 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
128
129 ret = pthread_create(&newTh, &attr, TaskF03, NULL);
130 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
131
132 ret = PosixPthreadInit(&attr2, MUTEX_TEST_LOW_PRIO);
133 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
134
135 ret = pthread_create(&newTh2, &attr2, TaskF01, NULL);
136 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
137
138 ret = PosixPthreadInit(&attr3, MUTEX_TEST_HIGH_PRIO);
139 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
140
141 ret = pthread_create(&newTh3, &attr3, TaskF02, NULL);
142 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
143
144 LOS_TaskDelay(5); // 5, set delay time.
145 ret = pthread_mutex_destroy(&g_mutex075);
146 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
147
148 ret = sem_destroy(&g_sem075);
149 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
150
151 ret = PosixPthreadDestroy(&attr, newTh);
152 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
153
154 ret = PosixPthreadDestroy(&attr2, newTh2);
155 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
156
157 ret = PosixPthreadDestroy(&attr3, newTh3);
158 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
159
160 return LOS_OK;
161 }
162
163 /**
164 * @tc.name: ItPosixMux049
165 * @tc.desc: Test interface sem_wait
166 * @tc.type: FUNC
167 * @tc.require: issueI5YAEX
168 */
169
ItPosixMux049(void)170 VOID ItPosixMux049(void)
171 {
172 TEST_ADD_CASE("ItPosixMux049", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
173 }
174