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
35 static int g_testToCount = 0;
36 static int g_testBackCount = 0;
37
ThreadFuncTest3(void * a)38 static void *ThreadFuncTest3(void *a)
39 {
40 int ret;
41 pthread_t thread = pthread_self();
42
43 ret = pthread_detach(thread);
44 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
45
46 g_testToCount++;
47 ret = pthread_mutex_lock(&g_mutexLock);
48 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
49
50 g_testToCount++;
51 ret = pthread_mutex_unlock(&g_mutexLock);
52 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
53
54 EXIT:
55 return nullptr;
56 }
57
ThreadFuncTest2(void * a)58 static void *ThreadFuncTest2(void *a)
59 {
60 int ret;
61
62 g_testBackCount++;
63 ret = pthread_mutex_lock(&g_mutexLock);
64 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
65
66 g_testBackCount++;
67 ret = pthread_mutex_unlock(&g_mutexLock);
68 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
69
70 EXIT:
71 return nullptr;
72 }
73
TestCase(void)74 static int TestCase(void)
75 {
76 struct sched_param param = { 0 };
77 int ret;
78 int currThreadPri, currThreadPolicy;
79 pthread_attr_t a = { 0 };
80 pthread_t newPthread;
81 pthread_mutexattr_t mutex;
82 pthread_mutexattr_settype(&mutex, PTHREAD_MUTEX_NORMAL);
83 pthread_mutex_init(&g_mutexLock, &mutex);
84
85 g_testToCount = 0;
86 g_testBackCount = 0;
87
88 SLEEP_AND_YIELD(1);
89
90 ret = pthread_mutex_lock(&g_mutexLock);
91 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
92
93 ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, ¶m);
94 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
95
96 currThreadPri = param.sched_priority;
97
98 // 2, Set the priority according to the task purpose,a smaller number means a higher priority.
99 param.sched_priority = currThreadPri - 2;
100 pthread_attr_init(&a);
101 pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
102 pthread_attr_setschedparam(&a, ¶m);
103 pthread_attr_setschedpolicy(&a, currThreadPolicy);
104 ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
105 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
106
107 SLEEP_AND_YIELD(5); // 5, delay enouge time
108
109 ICUNIT_ASSERT_EQUAL(g_testToCount, 1, g_testToCount);
110
111 ret = pthread_create(&newPthread, nullptr, ThreadFuncTest2, 0);
112 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
113
114 SLEEP_AND_YIELD(5); // 5, delay enouge time
115
116 ICUNIT_ASSERT_EQUAL(g_testBackCount, 1, g_testBackCount);
117
118 ret = pthread_mutex_unlock(&g_mutexLock);
119 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
120
121 ret = pthread_join(newPthread, nullptr);
122 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
123
124 ICUNIT_ASSERT_EQUAL(g_testBackCount, 2, g_testBackCount); // 2,The expected value
125 ICUNIT_ASSERT_EQUAL(g_testToCount, 2, g_testToCount); // 2,The expected value
126
127 pthread_mutex_destroy(&g_mutexLock);
128 return 0;
129 }
130
ItTestPthreadMutex017(void)131 void ItTestPthreadMutex017(void)
132 {
133 TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_017", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
134 }
135