• 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 
Testcase(void)69 static int Testcase(void)
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     pthread_mutex_init(&g_mutexLock, &attr);
88 
89     pthread_mutexattr_destroy(&attr);
90 
91     g_testToCount001 = 0;
92     g_testToCount002 = 0;
93 
94     ret = pthread_create(&tid1, nullptr, ThreadFuncTest1, nullptr);
95     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
96 
97     ret = pthread_create(&tid2, nullptr, ThreadFuncTest2, nullptr);
98     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
99 
100     pthread_join(tid1, nullptr);
101     pthread_join(tid2, nullptr);
102 
103     while (g_testToCount001 == 0 || g_testToCount002 == 0) {
104     }
105     ICUNIT_ASSERT_NOT_EQUAL(g_testToCount001, -1, g_testToCount001);
106     ICUNIT_ASSERT_NOT_EQUAL(g_testToCount002, -1, g_testToCount002);
107 
108     pthread_mutex_destroy(&g_mutexLock);
109 
110     return 0;
111 }
112 
ItTestPthreadMutex023(void)113 void ItTestPthreadMutex023(void)
114 {
115     TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_023", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
116 }
117