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 const unsigned int THREAD_COUNT = 5;
34 static pthread_mutex_t g_muxLock;
35 static int g_preTaskPri = 0;
36 static int g_testCnt = 0;
37
ThreadFuncTest3(void * a)38 static void *ThreadFuncTest3(void *a)
39 {
40 int ret;
41 struct sched_param param = { 0 };
42 pthread_t thread = pthread_self();
43 int currThreadPri, currThreadPolicy;
44
45 ret = pthread_detach(thread);
46 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
47
48 g_testCnt++;
49
50 ret = pthread_mutex_lock(&g_muxLock);
51 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
52
53 ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, ¶m);
54 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
55 currThreadPri = param.sched_priority;
56
57 if (g_preTaskPri == 0xffffffff) {
58 ICUNIT_GOTO_EQUAL(currThreadPri, 0, currThreadPri, EXIT);
59 } else {
60 ICUNIT_GOTO_WITHIN_EQUAL(currThreadPri, g_preTaskPri, 31, currThreadPri, EXIT); // 31, Lowest priority
61 }
62
63 g_preTaskPri = currThreadPri;
64 g_testCnt--;
65
66 ret = pthread_mutex_unlock(&g_muxLock);
67 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
68
69 EXIT:
70 return nullptr;
71 }
72
Testcase(void)73 static int Testcase(void)
74 {
75 struct sched_param param = { 0 };
76 int ret;
77 void *res = nullptr;
78 pthread_attr_t a = { 0 };
79 pthread_t thread = pthread_self();
80 pthread_t newPthread, newPthread1;
81 pthread_mutexattr_t mutex;
82 int count = 0;
83 int currThreadPri, currThreadPolicy;
84
85 g_preTaskPri = 0xffffffff;
86 g_testCnt = 0;
87
88 pthread_mutexattr_settype(&mutex, PTHREAD_MUTEX_NORMAL);
89 pthread_mutex_init(&g_muxLock, &mutex);
90
91 ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, ¶m);
92 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
93
94 currThreadPri = param.sched_priority;
95
96 ret = pthread_mutex_lock(&g_muxLock);
97 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
98
99 pthread_attr_init(&a);
100 pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
101 for (int count = 0; count < THREAD_COUNT; count++) {
102 // 2, Set the priority according to the task purpose,a smaller number means a higher priority.
103 param.sched_priority = currThreadPri - 2 * count;
104 pthread_attr_setschedparam(&a, ¶m);
105 pthread_attr_setschedpolicy(&a, currThreadPolicy);
106 ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
107 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
108 }
109
110 param.sched_priority = currThreadPri - 1;
111 pthread_attr_setschedparam(&a, ¶m);
112 pthread_attr_setschedpolicy(&a, currThreadPolicy);
113 ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
114 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
115
116 // 6, Set the priority according to the task purpose,a smaller number means a higher priority.
117 param.sched_priority = currThreadPri - 6;
118 pthread_attr_setschedparam(&a, ¶m);
119 pthread_attr_setschedpolicy(&a, currThreadPolicy);
120 ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
121 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
122
123 param.sched_priority = 0;
124 pthread_attr_setschedparam(&a, ¶m);
125 pthread_attr_setschedpolicy(&a, currThreadPolicy);
126 ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
127 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
128
129 param.sched_priority = currThreadPri - 1;
130 pthread_attr_setschedparam(&a, ¶m);
131 pthread_attr_setschedpolicy(&a, currThreadPolicy);
132 ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
133 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
134
135 param.sched_priority = currThreadPri;
136 pthread_attr_setschedparam(&a, ¶m);
137 pthread_attr_setschedpolicy(&a, currThreadPolicy);
138 ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
139 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
140
141 sleep(1);
142
143 ret = pthread_mutex_unlock(&g_muxLock);
144 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
145
146 SLEEP_AND_YIELD(1);
147
148 ICUNIT_ASSERT_EQUAL(g_testCnt, 0, g_testCnt);
149
150 pthread_mutex_destroy(&g_muxLock);
151 return 0;
152 }
153
ItTestPthreadMutex003(void)154 void ItTestPthreadMutex003(void)
155 {
156 TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_003", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
157 }
158