1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "It_posix_mutex.h"
32 
33 static pthread_mutex_t g_mutex068;
34 
TaskF01(void *argv)35 static VOID *TaskF01(void *argv)
36 {
37     UINT32 ret;
38 
39     g_testCount++;
40 
41     ret = pthread_mutex_lock(&g_mutex068);
42     ICUNIT_TRACK_EQUAL(ret, 0, ret);
43 
44     g_testCount++;
45 
46     LOS_TaskDelay(30); // 30, set delay time.
47 
48     ret = pthread_mutex_unlock(&g_mutex068);
49     ICUNIT_TRACK_EQUAL(ret, 0, ret);
50 
51     LOS_TaskDelay(100); // 100, set delay time.
52 
53     return NULL;
54 }
TaskF02(void *argv)55 static VOID *TaskF02(void *argv)
56 {
57     UINT32 ret;
58 
59     g_testCount++;
60 
61     ret = pthread_mutex_lock(&g_mutex068);
62     ICUNIT_TRACK_EQUAL(ret, 0, ret);
63 
64     g_testCount++;
65 
66     LOS_TaskDelay(30); // 30, set delay time.
67 
68     ret = pthread_mutex_unlock(&g_mutex068);
69     ICUNIT_TRACK_EQUAL(ret, 0, ret);
70 
71     LOS_TaskDelay(100); // 100, set delay time.
72 
73     return NULL;
74 }
75 
Testcasenull76 static UINT32 Testcase(VOID)
77 {
78     UINT32 ret;
79     pthread_t newTh;
80     pthread_attr_t attr;
81     pthread_t newTh2;
82     pthread_attr_t attr2;
83 
84     ret = pthread_mutex_init(&g_mutex068, NULL);
85     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
86 
87     g_testCount = 0;
88 
89     LOS_TaskLock();
90 
91     ret = PosixPthreadInit(&attr, MUTEX_TEST_DEFAULT_PRIO);
92     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
93 
94     ret = pthread_create(&newTh, &attr, TaskF01, NULL);
95     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
96 
97     ret = PosixPthreadInit(&attr2, MUTEX_TEST_HIGH_PRIO);
98     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
99 
100     ret = pthread_create(&newTh2, &attr2, TaskF02, NULL);
101     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
102 
103     LOS_TaskUnlock();
104 
105     ICUNIT_ASSERT_EQUAL(g_testCount, 3, g_testCount); // 3, Assert that g_testCount.
106 
107     LOS_TaskDelay(40); // 40, set delay time.
108     ICUNIT_ASSERT_EQUAL(g_testCount, 4, g_testCount); // 4, Assert that g_testCount.
109 
110     LOS_TaskDelay(30); // 30, set delay time.
111 
112     ret = pthread_mutex_destroy(&g_mutex068);
113     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
114 
115     ret = PosixPthreadDestroy(&attr, newTh);
116     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
117 
118     ret = PosixPthreadDestroy(&attr2, newTh2);
119     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
120 
121     return LOS_OK;
122 }
123 
124 /**
125  * @tc.name: ItPosixMux041
126  * @tc.desc: Test interface pthread_mutex_unlock
127  * @tc.type: FUNC
128  * @tc.require: issueI5YAEX
129  */
130 
ItPosixMux041(void)131 VOID ItPosixMux041(void)
132 {
133     TEST_ADD_CASE("ItPosixMux041", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
134 }
135