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_mutex074;
34 static sem_t g_sem074;
35 static int g_pshared074;
36
TaskF01(void * arg)37 static VOID *TaskF01(void *arg)
38 {
39 UINT32 ret;
40 ret = pthread_mutex_lock(&g_mutex074);
41 ICUNIT_TRACK_EQUAL(ret, 0, ret);
42
43 g_testCount++;
44 ICUNIT_TRACK_EQUAL(g_testCount, 1, g_testCount);
45
46 LOS_TaskDelay(5); // 5, set delay time.
47
48 ret = sem_wait(&g_sem074);
49 ICUNIT_TRACK_EQUAL(ret, 0, ret);
50
51 g_testCount++;
52 ICUNIT_TRACK_EQUAL(g_testCount, 5, g_testCount); // 5, Here, assert that g_testCount is equal to 5.
53
54 ret = sem_post(&g_sem074);
55 ICUNIT_TRACK_EQUAL(ret, 0, ret);
56
57 g_testCount++;
58 ICUNIT_TRACK_EQUAL(g_testCount, 6, g_testCount); // 6, Here, assert that g_testCount is equal to 6.
59
60 ret = pthread_mutex_unlock(&g_mutex074);
61 ICUNIT_TRACK_EQUAL(ret, 0, ret);
62
63 return NULL;
64 }
65
TaskF02(void * arg)66 static VOID *TaskF02(void *arg)
67 {
68 UINT32 ret;
69 LOS_TaskDelay(1);
70
71 g_testCount++;
72 ICUNIT_TRACK_EQUAL(g_testCount, 2, g_testCount); // 2, Here, assert that g_testCount is equal to 2.
73
74 ret = pthread_mutex_lock(&g_mutex074);
75 ICUNIT_TRACK_EQUAL(ret, 0, ret);
76
77 g_testCount++;
78 ICUNIT_TRACK_EQUAL(g_testCount, 7, g_testCount); // 7, Here, assert that g_testCount is equal to 7.
79
80 ret = pthread_mutex_unlock(&g_mutex074);
81 ICUNIT_TRACK_EQUAL(ret, 0, ret);
82
83 return NULL;
84 }
85
TaskF03(void * arg)86 static VOID *TaskF03(void *arg)
87 {
88 UINT32 ret;
89
90 LOS_TaskDelay(2); // 2, set delay time.
91 g_testCount++;
92 ICUNIT_TRACK_EQUAL(g_testCount, 3, g_testCount); // 3, Here, assert that g_testCount is equal to 3.
93
94 ret = sem_wait(&g_sem074);
95 ICUNIT_TRACK_EQUAL(ret, 0, ret);
96
97 g_testCount++;
98 ICUNIT_TRACK_EQUAL(g_testCount, 4, g_testCount); // 4, Here, assert that g_testCount is equal to 4.
99
100 LOS_TaskDelay(4); // 4, set delay time.
101 ret = sem_post(&g_sem074);
102 ICUNIT_TRACK_EQUAL(ret, 0, ret);
103
104 g_testCount++;
105 ICUNIT_TRACK_EQUAL(g_testCount, 8, g_testCount); // 8, Here, assert that g_testCount is equal to 8.
106
107 return NULL;
108 }
109
Testcase(VOID)110 static UINT32 Testcase(VOID)
111 {
112 UINT32 ret;
113 pthread_t newTh;
114 pthread_attr_t attr;
115 pthread_t newTh2;
116 pthread_attr_t attr2;
117 pthread_t newTh3;
118 pthread_attr_t attr3;
119 g_testCount = 0;
120
121 ret = pthread_mutex_init(&g_mutex074, NULL);
122 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
123
124 ret = sem_init(&g_sem074, g_pshared074, 2); // 2, The initial number of available semaphores.
125 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
126
127 ret = PosixPthreadInit(&attr, MUTEX_TEST_LOW_PRIO);
128 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
129
130 ret = pthread_create(&newTh, &attr, TaskF01, NULL);
131 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
132
133 ret = PosixPthreadInit(&attr2, MUTEX_TEST_HIGH_PRIO);
134 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
135
136 ret = pthread_create(&newTh2, &attr2, TaskF02, NULL);
137 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
138
139 ret = PosixPthreadInit(&attr3, MUTEX_TEST_DEFAULT_PRIO);
140 ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
141
142 ret = pthread_create(&newTh3, &attr3, TaskF03, NULL);
143 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
144
145 LOS_TaskDelay(10); // 10, set delay time.
146 ret = pthread_mutex_destroy(&g_mutex074);
147 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
148
149 ret = sem_destroy(&g_sem074);
150 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
151
152 ret = PosixPthreadDestroy(&attr, newTh);
153 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
154
155 ret = PosixPthreadDestroy(&attr2, newTh2);
156 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
157
158 ret = PosixPthreadDestroy(&attr3, newTh3);
159 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
160
161 return LOS_OK;
162 }
163
164 /**
165 * @tc.name: ItPosixMux048
166 * @tc.desc: Test interface sem_post
167 * @tc.type: FUNC
168 * @tc.require: issueI5YAEX
169 */
170
ItPosixMux048(void)171 VOID ItPosixMux048(void)
172 {
173 TEST_ADD_CASE("ItPosixMux048", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
174 }
175