• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mutex038;
34 static sem_t g_sem038;
35 
TaskF01(void * arg)36 static void *TaskF01(void *arg)
37 {
38     int ret;
39 
40     ret = pthread_mutex_trylock(&g_mutex038);
41     ICUNIT_GOTO_NOT_EQUAL(ret, 0, ret, EXIT);
42 
43     ret = sem_post(&g_sem038);
44     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
45 
46     ret = pthread_mutex_lock(&g_mutex038);
47     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
48 
49     ret = sem_post(&g_sem038);
50     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
51 
52     ret = pthread_mutex_unlock(&g_mutex038);
53     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
54 
55 EXIT:
56     return NULL;
57 }
58 
Testcase(VOID)59 static UINT32 Testcase(VOID)
60 {
61     int ret;
62     int i;
63     pthread_mutexattr_t ma;
64     pthread_t child;
65 
66     ret = sem_init(&g_sem038, 0, 0);
67     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
68 
69     ret = pthread_mutexattr_init(&ma);
70     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
71 
72     ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE);
73     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
74 
75     ret = pthread_mutex_init(&g_mutex038, &ma);
76     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
77 
78     ret = pthread_mutexattr_destroy(&ma);
79     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
80 
81     ret = pthread_mutex_lock(&g_mutex038);
82     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
83 
84     ret = pthread_mutex_lock(&g_mutex038);
85     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
86 
87     ret = pthread_mutex_unlock(&g_mutex038);
88     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
89 
90     ret = pthread_create(&child, NULL, TaskF01, NULL);
91     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
92 
93     ret = sem_wait(&g_sem038);
94     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
95 
96     ret = pthread_mutex_unlock(&g_mutex038);
97     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
98 
99     ret = sem_wait(&g_sem038);
100     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
101 
102     ret = pthread_mutex_unlock(&g_mutex038);
103     ICUNIT_ASSERT_NOT_EQUAL(1, 0, ret);
104 
105     ret = pthread_join(child, NULL);
106     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
107 
108     for (i = 0; i < 50; i++) { // 50, The loop frequency.
109         ret = pthread_mutex_lock(&g_mutex038);
110         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
111     }
112 
113     for (i = 0; i < 50; i++) { // 50, The loop frequency.
114         ret = pthread_mutex_unlock(&g_mutex038);
115         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
116     }
117 
118     ret = pthread_mutex_unlock(&g_mutex038);
119     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
120 
121     ret = pthread_mutex_destroy(&g_mutex038);
122     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
123 
124     ret = sem_destroy(&g_sem038);
125     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
126 
127     return LOS_OK;
128 }
129 
130 /**
131  * @tc.name: ItPosixMux037
132  * @tc.desc: Test interface sem_wait
133  * @tc.type: FUNC
134  * @tc.require: issueI5YAEX
135  */
136 
ItPosixMux037(void)137 VOID ItPosixMux037(void)
138 {
139     TEST_ADD_CASE("ItPosixMux037", Testcase, TEST_POSIX, TEST_MUX, TEST_LEVEL2, TEST_FUNCTION);
140 }
141