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