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 #include "it_mutex_test.h"
32
33 static pthread_mutex_t g_muxLock001;
34 static pthread_mutex_t g_muxLock002;
35 static pthread_mutex_t g_muxLock003;
36
37 static const unsigned int THREAD_COUNT = 10;
38
39 static int g_testInfo[3][10] = { 0 };
40 static int g_testToCount001 = 0;
41 static int g_testBackCount001 = 0;
42 static int g_testToCount002 = 0;
43 static int g_testBackCount002 = 0;
44 static int g_testToCount003 = 0;
45 static int g_testBackCount003 = 0;
46
ThreadFuncTest3(void * a)47 static void *ThreadFuncTest3(void *a)
48 {
49 int ret;
50 int tid;
51 pthread_t thread = pthread_self();
52 int currThreadPri, currThreadPolicy;
53 struct sched_param param = { 0 };
54
55 ret = pthread_detach(thread);
56 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
57
58 g_testInfo[2][g_testToCount003] = Gettid(); // 2, max indx
59 g_testToCount003++;
60
61 ret = pthread_mutex_lock(&g_muxLock003);
62 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
63
64 tid = Gettid();
65 ICUNIT_GOTO_EQUAL(g_testInfo[2][g_testBackCount003], tid, tid, EXIT); // 2, max indx
66 g_testBackCount003++;
67
68 ret = pthread_mutex_unlock(&g_muxLock003);
69 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
70
71 EXIT:
72 return nullptr;
73 }
74
ThreadFuncTest2(void * a)75 static void *ThreadFuncTest2(void *a)
76 {
77 int ret;
78 int tid;
79 pthread_t thread = pthread_self();
80
81 ret = pthread_detach(thread);
82 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
83
84 g_testInfo[1][g_testToCount002] = Gettid();
85 g_testToCount002++;
86
87 ret = pthread_mutex_lock(&g_muxLock002);
88 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
89
90 tid = Gettid();
91 ICUNIT_GOTO_EQUAL(g_testInfo[1][g_testBackCount002], tid, tid, EXIT);
92 g_testBackCount002++;
93
94 ret = pthread_mutex_unlock(&g_muxLock002);
95 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
96
97 EXIT:
98 return nullptr;
99 }
100
ThreadFuncTest1(void * a)101 static void *ThreadFuncTest1(void *a)
102 {
103 int ret;
104 int tid;
105 pthread_t thread = pthread_self();
106
107 ret = pthread_detach(thread);
108 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
109
110 g_testInfo[0][g_testToCount001] = Gettid();
111 g_testToCount001++;
112
113 ret = pthread_mutex_lock(&g_muxLock001);
114 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
115
116 tid = Gettid();
117 ICUNIT_GOTO_EQUAL(g_testInfo[0][g_testBackCount001], tid, tid, EXIT);
118 g_testBackCount001++;
119
120 ret = pthread_mutex_unlock(&g_muxLock001);
121 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
122
123 EXIT:
124 return nullptr;
125 }
126
Testcase(void)127 static int Testcase(void)
128 {
129 struct sched_param param = { 0 };
130 int ret;
131 void *res = nullptr;
132 pthread_attr_t a = { 0 };
133 pthread_t thread = pthread_self();
134 pthread_t newPthread, newPthread1;
135 pthread_mutexattr_t mutex;
136 int index = 0;
137 pthread_mutexattr_settype(&mutex, PTHREAD_MUTEX_NORMAL);
138 pthread_mutex_init(&g_muxLock001, &mutex);
139 pthread_mutex_init(&g_muxLock002, &mutex);
140 pthread_mutex_init(&g_muxLock003, &mutex);
141
142 g_testToCount001 = 0;
143 g_testBackCount001 = 0;
144 g_testToCount002 = 0;
145 g_testBackCount002 = 0;
146 g_testToCount003 = 0;
147 g_testBackCount003 = 0;
148
149 ret = pthread_mutex_lock(&g_muxLock001);
150 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
151
152 ret = pthread_mutex_lock(&g_muxLock002);
153 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
154
155 ret = pthread_mutex_lock(&g_muxLock003);
156 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
157
158 for (int count = 0; count < THREAD_COUNT; count++) {
159 ret = pthread_create(&newPthread, nullptr, ThreadFuncTest3, 0);
160 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
161
162 SLEEP_AND_YIELD(1);
163 }
164
165 for (int count = 0; count < THREAD_COUNT; count++) {
166 ret = pthread_create(&newPthread, nullptr, ThreadFuncTest2, 0);
167 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
168
169 SLEEP_AND_YIELD(1);
170 }
171
172 for (int count = 0; count < THREAD_COUNT; count++) {
173 ret = pthread_create(&newPthread, nullptr, ThreadFuncTest1, 0);
174 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
175
176 SLEEP_AND_YIELD(1);
177 }
178
179 ret = pthread_mutex_unlock(&g_muxLock001);
180 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
181
182 SLEEP_AND_YIELD(1);
183 ret = pthread_mutex_unlock(&g_muxLock002);
184 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
185
186 SLEEP_AND_YIELD(1);
187 ret = pthread_mutex_unlock(&g_muxLock003);
188 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
189
190 SLEEP_AND_YIELD(1);
191
192 pthread_mutex_destroy(&g_muxLock001);
193 pthread_mutex_destroy(&g_muxLock002);
194 pthread_mutex_destroy(&g_muxLock003);
195 return 0;
196 }
197
ItTestPthreadMutex005(void)198 void ItTestPthreadMutex005(void)
199 {
200 TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_005", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
201 }
202