• 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_mutexLock;
34 static volatile int g_testToCount001 = 0;
35 static volatile int g_testToCount002 = 0;
36 
ThreadFuncTest1(void * arg)37 static void *ThreadFuncTest1(void *arg)
38 {
39     int ret = pthread_mutex_lock(&g_mutexLock);
40     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
41 
42     g_testToCount001++;
43     return nullptr;
44 
45 EXIT:
46     g_testToCount001 = -1;
47     return nullptr;
48 }
49 
ThreadFuncTest2(void * arg)50 static void *ThreadFuncTest2(void *arg)
51 {
52     int ret = pthread_mutex_lock(&g_mutexLock);
53     ICUNIT_GOTO_EQUAL(ret, EOWNERDEAD, ret, EXIT);
54 
55     ret = pthread_mutex_consistent(&g_mutexLock);
56     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
57 
58     g_testToCount002++;
59     ret = pthread_mutex_unlock(&g_mutexLock);
60     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
61 
62     return nullptr;
63 
64 EXIT:
65     g_testToCount002 = -1;
66     return nullptr;
67 }
68 
Child()69 static int Child()
70 {
71     int ret;
72     pthread_t tid1, tid2;
73     pthread_mutexattr_t attr;
74     int robust;
75 
76     pthread_mutexattr_init(&attr);
77 
78     pthread_mutexattr_getrobust(&attr, &robust);
79     ICUNIT_ASSERT_EQUAL(robust, 0, robust);
80 
81     ret = pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
82     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
83 
84     pthread_mutexattr_getrobust(&attr, &robust);
85     ICUNIT_ASSERT_EQUAL(robust, PTHREAD_MUTEX_ROBUST, robust);
86 
87     ret = pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_STALLED);
88     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
89 
90     pthread_mutexattr_getrobust(&attr, &robust);
91     ICUNIT_ASSERT_EQUAL(robust, PTHREAD_MUTEX_STALLED, robust);
92 
93     pthread_mutex_init(&g_mutexLock, &attr);
94 
95     pthread_mutexattr_destroy(&attr);
96 
97     g_testToCount001 = 0;
98     g_testToCount002 = 0;
99 
100     ret = pthread_create(&tid1, nullptr, ThreadFuncTest1, nullptr);
101     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
102 
103     ret = pthread_create(&tid2, nullptr, ThreadFuncTest2, nullptr);
104     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
105     for (int i = 0; i < 5; i++) { // 5
106         if (g_testToCount002 == 0) {
107             sleep(1);
108         }
109     }
110 
111     pthread_join(tid1, nullptr);
112 
113     ICUNIT_ASSERT_NOT_EQUAL(g_testToCount001, -1, g_testToCount001);
114     ICUNIT_ASSERT_EQUAL(g_testToCount002, 0, g_testToCount002);
115 
116     pthread_mutex_destroy(&g_mutexLock);
117 
118     exit(255); // 255, set exitcode
119 }
120 
Testcase(void)121 static int Testcase(void)
122 {
123     int ret;
124     int status;
125 
126     pid_t pid = fork();
127     ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, The expected value
128 
129     if (pid == 0) {
130         Child();
131         exit(0);
132     }
133 
134     ret = waitpid(pid, &status, 0);
135     ICUNIT_ASSERT_EQUAL(ret, pid, ret);
136     status = WEXITSTATUS(status);
137     ICUNIT_ASSERT_EQUAL(status, 255, status); // 255, exitcode is 255
138 
139     return 0;
140 EXIT:
141     return 1;
142 }
143 
ItTestPthreadMutex025(void)144 void ItTestPthreadMutex025(void)
145 {
146     TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_025", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
147 }
148