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