• 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_testToCount003 = 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     gettimeofday(&timeVal, nullptr);
77 
78     if(timeVal.tv_usec + 100000 >= 1000000) { // 100000us, delay time; 1000000us, compare
79         time.tv_sec = timeVal.tv_sec + 1;
80         time.tv_nsec = (timeVal.tv_usec + 100000 - 1000000) * 1000; // 100000, delaytime; 1000, us to ns; 1000000us=1s;
81     } else {
82         time.tv_sec = timeVal.tv_sec + 0;
83         time.tv_nsec = (timeVal.tv_usec + 100000) * 1000; // 1000, 100000us to ns
84     }
85 
86     ret = pthread_mutex_timedlock(&g_muxLock001, &time);
87     ICUNIT_GOTO_EQUAL(ret, ETIMEDOUT, ret, EXIT);
88 
89     ICUNIT_GOTO_EQUAL(g_testToCount002, 1, g_testToCount002, EXIT);
90 
91     g_testToCount001++;
92     return nullptr;
93 
94 EXIT:
95     g_testToCount001 = -1;
96     return nullptr;
97 }
98 
99 /* futexList :
100  * 30(20) -> -1(-1) -> -1(-1) -> -1(-1) -> -1(-1) -> -1(-1) -> -1(-1) ->
101  * -1(-1) -> -1(-1) -> -1(-1) -> -1(-1) -> -1(-1) -> -1(-1) ->
102  *
103  */
Testcase(void)104 static int Testcase(void)
105 {
106     struct sched_param param = { 0 };
107     int ret;
108     int threadCount;
109     void *res = nullptr;
110     pthread_attr_t a = { 0 };
111     pthread_t thread = pthread_self();
112     pthread_t newPthread[10], newPthread1;
113     pthread_mutexattr_t mutex;
114     int index = TEST_COUNT;
115     int gCurrThreadPri, gCurrThreadPolicy;
116     struct timeval time = { 0 };
117     struct timeval timeVal = { 0 };
118 
119     ret = pthread_getschedparam(pthread_self(), &gCurrThreadPolicy, &param);
120     ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
121 
122     gCurrThreadPri = param.sched_priority;
123 
124     ret = pthread_attr_init(&a);
125     // 2, Set the priority according to the task purpose,a smaller number means a higher priority.
126     param.sched_priority = gCurrThreadPri + 2;
127     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
128     pthread_attr_setschedparam(&a, &param);
129 
130     while (index) {
131         pthread_mutexattr_settype(&mutex, PTHREAD_MUTEX_NORMAL);
132         pthread_mutex_init(&g_muxLock001, &mutex);
133 
134         g_testToCount001 = 0;
135         g_testToCount002 = 0;
136         threadCount = 0;
137 
138         ret = pthread_mutex_lock(&g_muxLock001);
139         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
140 
141         while (threadCount < NEW_THREAD_COUNT) {
142             ret = pthread_create(&newPthread[threadCount], &a, ThreadFuncTest1, 0);
143             ICUNIT_ASSERT_EQUAL(ret, 0, ret);
144             threadCount++;
145         }
146 
147         usleep(1000 * 10 * 7); // 1000, 10, 7
148 
149         ret = pthread_create(&newPthread1, nullptr, ThreadFuncTest2, 0);
150         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
151 
152         gettimeofday(&timeVal, nullptr);
153         while (1) {
154             gettimeofday(&time, nullptr);
155             if ((time.tv_sec - timeVal.tv_sec) >= 1) {
156                 break;
157             }
158         }
159 
160         ret = pthread_mutex_unlock(&g_muxLock001);
161         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
162 
163         threadCount = 0;
164         while (threadCount < NEW_THREAD_COUNT) {
165             ret = pthread_join(newPthread[threadCount], nullptr);
166             ICUNIT_ASSERT_EQUAL(ret, 0, ret);
167             threadCount++;
168         }
169 
170         ICUNIT_ASSERT_NOT_EQUAL(g_testToCount001, -1, g_testToCount001);
171         ICUNIT_ASSERT_NOT_EQUAL(g_testToCount002, -1, g_testToCount002);
172         ICUNIT_ASSERT_EQUAL(g_testToCount001, NEW_THREAD_COUNT, g_testToCount001);
173 
174         pthread_mutex_destroy(&g_muxLock003);
175         index--;
176     }
177     return 0;
178 }
179 
ItTestPthreadMutex019(void)180 void ItTestPthreadMutex019(void)
181 {
182     TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_019", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
183 }
184