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