• 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 THREAD_COUNT = 10;
38 
39 static int g_testInfo[3][10] = { 0 };
40 static int g_testToCount001 = 0;
41 static int g_testBackCount001 = 0;
42 static int g_testToCount002 = 0;
43 static int g_testBackCount002 = 0;
44 static int g_testToCount003 = 0;
45 static int g_testBackCount003 = 0;
46 
ThreadFuncTest3(void * a)47 static void *ThreadFuncTest3(void *a)
48 {
49     int ret;
50     int tid = Gettid();
51     pthread_t thread = pthread_self();
52 
53     ret = pthread_detach(thread);
54     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
55 
56     g_testInfo[2][g_testToCount003] = tid; // 2, max indx
57     g_testToCount003++;
58 
59     ret = pthread_mutex_lock(&g_muxLock003);
60     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
61 
62     ICUNIT_GOTO_EQUAL(g_testInfo[2][g_testBackCount003], tid, tid, EXIT); // 2, max indx
63     g_testBackCount003++;
64 
65     ret = pthread_mutex_unlock(&g_muxLock003);
66     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
67 
68 EXIT:
69     return nullptr;
70 }
71 
ThreadFuncTest2(void * a)72 static void *ThreadFuncTest2(void *a)
73 {
74     int ret;
75     int tid = Gettid();
76     pthread_t thread = pthread_self();
77 
78     ret = pthread_detach(thread);
79     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
80 
81     g_testInfo[1][g_testToCount002] = tid;
82     g_testToCount002++;
83 
84     ret = pthread_mutex_lock(&g_muxLock002);
85     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
86 
87     ICUNIT_GOTO_EQUAL(g_testInfo[1][g_testBackCount002], tid, tid, EXIT);
88     g_testBackCount002++;
89 
90     ret = pthread_mutex_unlock(&g_muxLock002);
91     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
92 
93 EXIT:
94     return nullptr;
95 }
96 
ThreadFuncTest1(void * a)97 static void *ThreadFuncTest1(void *a)
98 {
99     int ret;
100     int tid = Gettid();
101     ;
102     pthread_t thread = pthread_self();
103 
104     ret = pthread_detach(thread);
105     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
106 
107     g_testInfo[0][g_testToCount001] = tid;
108     g_testToCount001++;
109 
110     ret = pthread_mutex_lock(&g_muxLock001);
111     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
112 
113     ICUNIT_GOTO_EQUAL(g_testInfo[0][g_testBackCount001], tid, tid, EXIT);
114     g_testBackCount001++;
115 
116     ret = pthread_mutex_unlock(&g_muxLock001);
117     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
118 
119 EXIT:
120     return nullptr;
121 }
122 
Testcase(void)123 static int Testcase(void)
124 {
125     int ret;
126     pthread_t newPthread;
127     pthread_mutexattr_t mutex;
128 
129     pthread_mutexattr_settype(&mutex, PTHREAD_MUTEX_NORMAL);
130     pthread_mutex_init(&g_muxLock001, &mutex);
131     pthread_mutex_init(&g_muxLock002, &mutex);
132     pthread_mutex_init(&g_muxLock003, &mutex);
133 
134     g_testToCount001 = 0;
135     g_testBackCount001 = 0;
136     g_testToCount002 = 0;
137     g_testBackCount002 = 0;
138     g_testToCount003 = 0;
139     g_testBackCount003 = 0;
140 
141     ret = pthread_mutex_lock(&g_muxLock001);
142     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
143 
144     ret = pthread_mutex_lock(&g_muxLock002);
145     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
146 
147     ret = pthread_mutex_lock(&g_muxLock003);
148     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
149 
150     for (int count = 0; count < THREAD_COUNT; count++) {
151         ret = pthread_create(&newPthread, nullptr, ThreadFuncTest1, 0);
152         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
153 
154         SLEEP_AND_YIELD(1);
155     }
156 
157     for (int count = 0; count < THREAD_COUNT; count++) {
158         ret = pthread_create(&newPthread, nullptr, ThreadFuncTest2, 0);
159         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
160 
161         SLEEP_AND_YIELD(1);
162     }
163 
164     for (int count = 0; count < THREAD_COUNT; count++) {
165         ret = pthread_create(&newPthread, nullptr, ThreadFuncTest3, 0);
166         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
167 
168         SLEEP_AND_YIELD(1);
169     }
170 
171     ret = pthread_mutex_unlock(&g_muxLock001);
172     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
173 
174     SLEEP_AND_YIELD(1);
175 
176     ret = pthread_mutex_unlock(&g_muxLock002);
177     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
178 
179     SLEEP_AND_YIELD(1);
180 
181     ret = pthread_mutex_unlock(&g_muxLock003);
182     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
183 
184     SLEEP_AND_YIELD(1);
185 
186     pthread_mutex_destroy(&g_muxLock001);
187     pthread_mutex_destroy(&g_muxLock002);
188     pthread_mutex_destroy(&g_muxLock003);
189     return 0;
190 }
191 
ItTestPthreadMutex004(void)192 void ItTestPthreadMutex004(void)
193 {
194     TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_004", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
195 }
196