• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = 10;
38 static const unsigned int NEW_THREAD_COUNT = 10;
39 
40 static volatile int g_testToCount001 = 0;
41 static volatile int g_testToCount002 = 0;
42 static volatile int g_testToCount000 = 0;
43 
ThreadFuncTest2(void * a)44 static void *ThreadFuncTest2(void *a)
45 {
46     int ret;
47     pthread_t thread = pthread_self();
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     ret = pthread_mutex_lock(&g_muxLock001);
55     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
56 
57     ret = pthread_mutex_unlock(&g_muxLock001);
58     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
59 
60     g_testToCount002 = 1;
61 
62     return nullptr;
63 
64 EXIT:
65     g_testToCount002 = -1;
66     return nullptr;
67 }
68 
ThreadFuncTest1(void * a)69 static void *ThreadFuncTest1(void *a)
70 {
71     int ret;
72     pthread_t thread = pthread_self();
73     struct timespec time;
74     struct timeval timeVal = { 0 };
75 
76     ret = pthread_mutex_lock(&g_muxLock001);
77     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
78 
79     while (g_testToCount002 != 1) {
80         if (g_testToCount002 == -1) {
81             return nullptr;
82         }
83     }
84 
85     ret = pthread_mutex_unlock(&g_muxLock001);
86     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
87 
88     g_testToCount001++;
89     return nullptr;
90 
91 EXIT:
92     g_testToCount001 = -1;
93     return nullptr;
94 }
95 
ThreadFuncTest0(void * a)96 static void *ThreadFuncTest0(void *a)
97 {
98     int ret;
99     pthread_t thread = pthread_self();
100     struct timespec time;
101     struct timeval timeVal = { 0 };
102 
103     gettimeofday(&timeVal, nullptr);
104 
105     time.tv_sec = timeVal.tv_sec + 0;
106     time.tv_nsec = (timeVal.tv_usec + 1000 * 10 * 10) * 1000; // 1000, 10 ms to ns
107 
108     ret = pthread_mutex_timedlock(&g_muxLock001, &time);
109 
110     while (g_testToCount002 != 1) {
111         if (g_testToCount002 == -1) {
112             return nullptr;
113         }
114     }
115 
116     if (ret == 0) {
117         ret = pthread_mutex_unlock(&g_muxLock001);
118         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
119     }
120 
121     g_testToCount000++;
122     return nullptr;
123 
124 EXIT:
125     g_testToCount000 = -1;
126     return nullptr;
127 }
128 
129 /* futexlist:
130  * 21(20) -> -1(-1) -> 28(22) -> 27(22) -> 26(22) -> 25(22) -> 24(22) -> 23(22) ->
131  * -1(-1) -> 28(22) -> 27(22) -> 26(22) -> 25(22) -> 24(22) -> 23(22) ->
132  * 27(22) -> 26(22) -> 25(22) -> 24(22) -> 23(22) ->
133  */
Testcase(void)134 static int Testcase(void)
135 {
136     struct sched_param param = { 0 };
137     int ret;
138     int threadCount;
139     void *res = nullptr;
140     pthread_attr_t a = { 0 };
141     pthread_t thread = pthread_self();
142     pthread_t newPthread[10], newPthread1;
143     pthread_mutexattr_t mutex;
144     int index = TEST_COUNT;
145     int gCurrThreadPri, gCurrThreadPolicy;
146     struct timeval time = { 0 };
147     struct timeval timeVal = { 0 };
148 
149     ret = pthread_getschedparam(pthread_self(), &gCurrThreadPolicy, &param);
150     ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
151 
152     gCurrThreadPri = param.sched_priority;
153 
154     ret = pthread_attr_init(&a);
155     // 2, Set the priority according to the task purpose,a smaller number means a higher priority.
156     param.sched_priority = gCurrThreadPri + 2;
157     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
158     pthread_attr_setschedparam(&a, &param);
159 
160     while (index) {
161         pthread_mutexattr_settype(&mutex, PTHREAD_MUTEX_NORMAL);
162         pthread_mutex_init(&g_muxLock001, &mutex);
163 
164         g_testToCount001 = 0;
165         g_testToCount002 = 0;
166         g_testToCount000 = 0;
167         threadCount = 0;
168 
169         ret = pthread_mutex_lock(&g_muxLock001);
170         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
171 
172         ret = pthread_create(&newPthread[threadCount], &a, ThreadFuncTest0, 0);
173         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
174 
175         threadCount = 1;
176         ret = pthread_create(&newPthread[threadCount], &a, ThreadFuncTest0, 0);
177         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
178 
179         threadCount = 2; // 2
180         while (threadCount < NEW_THREAD_COUNT) {
181             ret = pthread_create(&newPthread[threadCount], &a, ThreadFuncTest1, 0);
182             ICUNIT_ASSERT_EQUAL(ret, 0, ret);
183             threadCount++;
184         }
185 
186         usleep(1000 * 10 * 8); // 1000, 10, 8
187 
188         ret = pthread_create(&newPthread1, nullptr, ThreadFuncTest2, 0);
189         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
190 
191         gettimeofday(&timeVal, nullptr);
192         while (1) {
193             gettimeofday(&time, nullptr);
194             if ((time.tv_sec - timeVal.tv_sec) >= 1) {
195                 break;
196             }
197         }
198 
199         ret = pthread_mutex_unlock(&g_muxLock001);
200         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
201 
202         threadCount = 0;
203         while (threadCount < NEW_THREAD_COUNT) {
204             ret = pthread_join(newPthread[threadCount], nullptr);
205             ICUNIT_ASSERT_EQUAL(ret, 0, ret);
206             threadCount++;
207         }
208 
209         ICUNIT_ASSERT_NOT_EQUAL(g_testToCount001, -1, g_testToCount001);
210         ICUNIT_ASSERT_NOT_EQUAL(g_testToCount002, -1, g_testToCount002);
211         ICUNIT_ASSERT_EQUAL(g_testToCount001, NEW_THREAD_COUNT - 2, g_testToCount001); // 2, test value
212 
213         pthread_mutex_destroy(&g_muxLock003);
214         index--;
215     }
216     return 0;
217 }
218 
ItTestPthreadMutex020(void)219 void ItTestPthreadMutex020(void)
220 {
221     TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_020", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
222 }
223