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