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