• 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 
35 static const unsigned int TEST_COUNT = 1;
36 static volatile int g_testToCount001 = 0;
37 static volatile int g_testToCount002 = 0;
38 static volatile int g_testToCount003 = 0;
39 
ThreadFuncTest3(void * a)40 static void *ThreadFuncTest3(void *a)
41 {
42     int ret;
43     pthread_t thread = pthread_self();
44     int currThreadPri, currThreadPolicy;
45     struct sched_param param = { 0 };
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     gettimeofday(&timeVal, nullptr);
53 
54     time.tv_sec = timeVal.tv_sec + 15; // 15
55     time.tv_nsec = timeVal.tv_usec + 0;
56 
57     ret = pthread_mutex_timedlock(&g_muxLock001, &time);
58     ICUNIT_GOTO_EQUAL(ret, ETIMEDOUT, ret, EXIT);
59 
60     g_testToCount003++;
61 
62     return nullptr;
63 EXIT:
64     g_testToCount003 = -1;
65     return nullptr;
66 }
67 
ThreadFuncTest2(void * a)68 static void *ThreadFuncTest2(void *a)
69 {
70     int ret;
71     pthread_t thread = pthread_self();
72     struct timespec time;
73     struct timeval timeVal = { 0 };
74 
75     ret = pthread_detach(thread);
76     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
77 
78     gettimeofday(&timeVal, nullptr);
79 
80     time.tv_sec = timeVal.tv_sec + 10; // 10
81     time.tv_nsec = timeVal.tv_usec + 0;
82 
83     ret = pthread_mutex_timedlock(&g_muxLock001, &time);
84     ICUNIT_GOTO_EQUAL(ret, ETIMEDOUT, ret, EXIT);
85 
86     g_testToCount002++;
87 
88     return nullptr;
89 
90 EXIT:
91     g_testToCount002 = -1;
92     return nullptr;
93 }
94 
ThreadFuncTest1(void * a)95 static void *ThreadFuncTest1(void *a)
96 {
97     int ret;
98     pthread_t thread = pthread_self();
99     struct timespec time;
100     struct timeval timeVal = { 0 };
101 
102     ret = pthread_detach(thread);
103     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
104 
105     gettimeofday(&timeVal, nullptr);
106 
107     time.tv_sec = timeVal.tv_sec + 5; // 5
108     time.tv_nsec = timeVal.tv_usec + 0;
109 
110     ret = pthread_mutex_timedlock(&g_muxLock001, &time);
111     ICUNIT_GOTO_EQUAL(ret, ETIMEDOUT, ret, EXIT);
112 
113     g_testToCount001++;
114 
115     return nullptr;
116 EXIT:
117     g_testToCount001 = -1;
118     return nullptr;
119 }
120 
Testcase(void)121 static int Testcase(void)
122 {
123     struct sched_param param = { 0 };
124     int ret;
125     void *res = nullptr;
126     pthread_attr_t a = { 0 };
127     pthread_t thread = pthread_self();
128     pthread_t newPthread, newPthread1;
129     pthread_mutexattr_t mutex;
130     int index = TEST_COUNT;
131 
132     while (index) {
133         pthread_mutexattr_settype(&mutex, PTHREAD_MUTEX_NORMAL);
134         pthread_mutex_init(&g_muxLock001, &mutex);
135 
136         g_testToCount001 = 0;
137         g_testToCount002 = 0;
138         g_testToCount003 = 0;
139 
140         ret = pthread_mutex_lock(&g_muxLock001);
141         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
142 
143         ret = pthread_create(&newPthread, nullptr, ThreadFuncTest1, 0);
144         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
145 
146         ret = pthread_create(&newPthread, nullptr, ThreadFuncTest2, 0);
147         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
148 
149         ret = pthread_create(&newPthread, nullptr, ThreadFuncTest3, 0);
150         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
151 
152         while (g_testToCount001 == 0 || g_testToCount002 == 0 || g_testToCount003 == 0) {
153         }
154 
155         ret = pthread_mutex_unlock(&g_muxLock001);
156         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
157 
158         index--;
159 
160         SLEEP_AND_YIELD(2); // 2, delay enouge time
161 
162         ICUNIT_ASSERT_NOT_EQUAL(g_testToCount001, -1, g_testToCount001);
163         ICUNIT_ASSERT_NOT_EQUAL(g_testToCount002, -1, g_testToCount002);
164         ICUNIT_ASSERT_NOT_EQUAL(g_testToCount003, -1, g_testToCount003);
165     }
166 
167     pthread_mutex_destroy(&g_muxLock001);
168     return 0;
169 }
170 
ItTestPthreadMutex008(void)171 void ItTestPthreadMutex008(void)
172 {
173     TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_008", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
174 }
175