• 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 
34 static pthread_mutex_t g_muxLock;
35 
TaskF01(void * arg)36 static void *TaskF01(void *arg)
37 {
38     unsigned int ret;
39     ret = pthread_mutex_trylock(&g_muxLock);
40     ICUNIT_TRACK_EQUAL(ret, EBUSY, ret);
41 
42     g_testCount++;
43 
44     ret = pthread_mutex_lock(&g_muxLock);
45     ICUNIT_TRACK_EQUAL(ret, 0, ret);
46 
47     ret = pthread_mutex_unlock(&g_muxLock);
48     ICUNIT_TRACK_EQUAL(ret, 0, ret);
49     g_testCount++;
50 
51     return nullptr;
52 }
53 
TestCase(void)54 static unsigned int TestCase(void)
55 {
56     unsigned int ret;
57     pthread_t newThread;
58     pthread_attr_t attr;
59     pthread_mutexattr_t mutexAttr;
60     struct sched_param sp;
61 
62     g_testCount = 0;
63 
64     pthread_mutexattr_init(&mutexAttr);
65     pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_ERRORCHECK);
66 
67     ret = pthread_mutex_init(&g_muxLock, &mutexAttr);
68     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
69 
70     ret = pthread_mutex_lock(&g_muxLock);
71     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
72 
73     ret = pthread_attr_init(&attr);
74     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
75     sp.sched_priority = 4; // 4, Set the priority according to the task purpose,a smaller number means a higher priority.
76     ret = pthread_attr_setschedparam(&attr, &sp);
77     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
78 
79     pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
80     ret = pthread_create(&newThread, &attr, TaskF01, nullptr);
81     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
82 
83     LosTaskDelay(5); // 5, delay enouge time
84     ICUNIT_ASSERT_EQUAL(g_testCount, 1, g_testCount);
85 
86     ret = pthread_mutex_unlock(&g_muxLock);
87     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
88 
89 #ifdef LOSCFG_USER_TEST_SMP
90     LosTaskDelay(5); // 5, delay enouge time
91 #endif
92     ICUNIT_ASSERT_EQUAL(g_testCount, 2, g_testCount); // 2,The expected value
93 
94     ret = pthread_mutex_lock(&g_muxLock);
95     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
96 
97     ret = pthread_mutex_lock(&g_muxLock);
98     ICUNIT_ASSERT_EQUAL(ret, EDEADLK, ret);
99 
100     ret = pthread_mutex_lock(&g_muxLock);
101     ICUNIT_ASSERT_EQUAL(ret, EDEADLK, ret);
102 
103     ret = pthread_mutex_unlock(&g_muxLock);
104     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
105 
106     ret = pthread_mutex_unlock(&g_muxLock);
107     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
108 
109     ret = pthread_mutex_unlock(&g_muxLock);
110     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
111 
112     ret = pthread_mutex_destroy(&g_muxLock);
113     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
114 
115     ret = pthread_join(newThread, nullptr);
116     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
117     return 0;
118 }
119 
ItTestPthreadMutex009(void)120 void ItTestPthreadMutex009(void)
121 {
122     TEST_ADD_CASE("IT_POSIX_PTHREAD_MUTEX_009", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
123 }
124