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