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_mutex079;
34 static sem_t g_sem079;
35 static int g_pshared084;
36 
TaskF01(void *arg)37 static VOID *TaskF01(void *arg)
38 {
39     UINT32 ret;
40     ret = pthread_mutex_trylock(&g_mutex079);
41     ICUNIT_TRACK_EQUAL(ret, 0, ret);
42 
43     ret = sem_wait(&g_sem079);
44     ICUNIT_TRACK_EQUAL(ret, 0, ret);
45 
46     ret = pthread_mutex_unlock(&g_mutex079);
47     ICUNIT_TRACK_EQUAL(ret, 0, ret);
48 
49     return NULL;
50 }
51 
TaskF02(void *arg)52 static VOID *TaskF02(void *arg)
53 {
54     UINT32 ret;
55 
56     ret = sem_post(&g_sem079);
57     ICUNIT_TRACK_EQUAL(ret, 0, ret);
58     return NULL;
59 }
60 
TaskF03(void *arg)61 static VOID *TaskF03(void *arg)
62 {
63     UINT32 ret;
64 
65     ret = pthread_mutex_lock(&g_mutex079);
66     ICUNIT_TRACK_EQUAL(ret, 0, ret);
67 
68     ret = pthread_mutex_unlock(&g_mutex079);
69     ICUNIT_TRACK_EQUAL(ret, 0, ret);
70 
71     return NULL;
72 }
73 
Testcasenull74 static UINT32 Testcase(VOID)
75 {
76     UINT32 ret;
77     pthread_t newTh;
78     pthread_attr_t attr;
79     pthread_t newTh2;
80     pthread_attr_t attr2;
81     pthread_t newTh3;
82     pthread_attr_t attr3;
83 
84     ret = pthread_mutex_init(&g_mutex079, NULL);
85     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
86 
87     ret = sem_init(&g_sem079, g_pshared084, 0);
88     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
89 
90     ret = PosixPthreadInit(&attr, MUTEX_TEST_HIGH_PRIO);
91     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
92 
93     ret = pthread_create(&newTh, &attr, TaskF01, NULL);
94     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
95 
96     ret = PosixPthreadInit(&attr2, MUTEX_TEST_HIGH_PRIO - 1);
97     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
98 
99     ret = pthread_create(&newTh2, &attr2, TaskF02, NULL);
100     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
101 
102     ret = PosixPthreadInit(&attr3, MUTEX_TEST_HIGH_PRIO);
103     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
104 
105     ret = pthread_create(&newTh3, &attr3, TaskF03, NULL);
106     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
107 
108     ret = pthread_mutex_destroy(&g_mutex079);
109     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
110 
111     ret = sem_destroy(&g_sem079);
112     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
113 
114     ret = PosixPthreadDestroy(&attr, newTh);
115     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
116 
117     ret = PosixPthreadDestroy(&attr2, newTh2);
118     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
119 
120     ret = PosixPthreadDestroy(&attr3, newTh3);
121     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
122 
123     return LOS_OK;
124 }
125 
126 /**
127  * @tc.name: ItPosixMux043
128  * @tc.desc: Test interface pthread_mutex_lock
129  * @tc.type: FUNC
130  * @tc.require: issueI5YAEX
131  */
132 
ItPosixMux043(void)133 VOID ItPosixMux043(void)
134 {
135     TEST_ADD_CASE("ItPosixMux043", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
136 }
137