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