• 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 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, &param);
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     pthread_attr_t a = { 0 };
78     pthread_t newPthread;
79     pthread_mutexattr_t mutex;
80     int currThreadPri, currThreadPolicy;
81 
82     g_preTaskPri = 0xffffffff;
83     g_testCnt = 0;
84 
85     pthread_mutexattr_settype(&mutex, PTHREAD_MUTEX_NORMAL);
86     pthread_mutex_init(&g_muxLock, &mutex);
87 
88     ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, &param);
89     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
90 
91     currThreadPri = param.sched_priority;
92 
93     ret = pthread_mutex_lock(&g_muxLock);
94     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
95 
96     pthread_attr_init(&a);
97     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
98     for (int count = 0; count < THREAD_COUNT; count++) {
99         // 2, Set the priority according to the task purpose,a smaller number means a higher priority.
100         param.sched_priority = currThreadPri - 2 * count;
101         pthread_attr_setschedparam(&a, &param);
102         pthread_attr_setschedpolicy(&a, currThreadPolicy);
103         ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
104         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
105     }
106 
107     param.sched_priority = currThreadPri - 1;
108     pthread_attr_setschedparam(&a, &param);
109     pthread_attr_setschedpolicy(&a, currThreadPolicy);
110     ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
111     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
112 
113     // 6, Set the priority according to the task purpose,a smaller number means a higher priority.
114     param.sched_priority = currThreadPri - 6;
115     pthread_attr_setschedparam(&a, &param);
116     pthread_attr_setschedpolicy(&a, currThreadPolicy);
117     ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
118     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
119 
120     param.sched_priority = 0;
121     pthread_attr_setschedparam(&a, &param);
122     pthread_attr_setschedpolicy(&a, currThreadPolicy);
123     ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
124     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
125 
126     param.sched_priority = currThreadPri - 1;
127     pthread_attr_setschedparam(&a, &param);
128     pthread_attr_setschedpolicy(&a, currThreadPolicy);
129     ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
130     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
131 
132     param.sched_priority = currThreadPri;
133     pthread_attr_setschedparam(&a, &param);
134     pthread_attr_setschedpolicy(&a, currThreadPolicy);
135     ret = pthread_create(&newPthread, &a, ThreadFuncTest3, 0);
136     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
137 
138     sleep(1);
139 
140     ret = pthread_mutex_unlock(&g_muxLock);
141     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
142 
143     SLEEP_AND_YIELD(1);
144 
145     ICUNIT_ASSERT_EQUAL(g_testCnt, 0, g_testCnt);
146 
147     pthread_mutex_destroy(&g_muxLock);
148     return 0;
149 }
150 
ItTestPthreadMutex003(void)151 void ItTestPthreadMutex003(void)
152 {
153     TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_003", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
154 }
155