• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "It_posix_pthread.h"
32 
33 #define LOOP_NUM 2
34 static pthread_mutex_t g_pthreadMutexTest1 = PTHREAD_MUTEX_INITIALIZER;
35 static pthread_cond_t g_pthreadCondTest1 = PTHREAD_COND_INITIALIZER;
36 
PthreadF01(void * t)37 static VOID *PthreadF01(void *t)
38 {
39     int rc;
40 
41     rc = pthread_mutex_lock(&g_pthreadMutexTest1);
42     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
43 
44     g_testCount++;
45     LOS_TaskDelay(1);
46     ICUNIT_GOTO_EQUAL(g_testCount, 2, g_testCount, EXIT); // 2, here assert the result.
47     g_testCount++;
48 
49     rc = pthread_cond_wait(&g_pthreadCondTest1, &g_pthreadMutexTest1);
50     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
51 
52     ICUNIT_GOTO_EQUAL(g_testCount, 5, g_testCount, EXIT); // 5, here assert the result.
53     rc = pthread_mutex_unlock(&g_pthreadMutexTest1);
54     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
55 
56 EXIT:
57     return NULL;
58 }
59 
PthreadF02(void * t)60 static VOID *PthreadF02(void *t)
61 {
62     int i;
63     int rc;
64 
65     ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT);
66     g_testCount++;
67     rc = pthread_mutex_lock(&g_pthreadMutexTest1);
68     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
69 
70     ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // 3, here assert the result.
71     g_testCount++;
72     rc = pthread_cond_signal(&g_pthreadCondTest1);
73     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
74 
75     ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT); // 4, here assert the result.
76     g_testCount++;
77 
78     rc = pthread_mutex_unlock(&g_pthreadMutexTest1); /* Increase latency for thread polling mutex */
79     ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
80     LOS_TaskDelay(2); // 2, delay for Timing control.
81 
82 EXIT:
83     return NULL;
84 }
Testcase(VOID)85 static UINT32 Testcase(VOID)
86 {
87     int i;
88     long t1 = 1;
89     long t2 = 2; // 2, init
90     int rc;
91     pthread_t threads[3]; // 3, need 3 pthread for test.
92     pthread_attr_t attr;
93 
94     g_testCount = 0;
95     pthread_mutex_init(&g_pthreadMutexTest1, NULL);
96     /* When creating thread, it is set to connectable state, which is easy to transplant */
97     pthread_cond_init(&g_pthreadCondTest1, NULL);
98     pthread_attr_init(&attr);
99 
100     rc = pthread_create(&threads[0], &attr, PthreadF01, (void *)t1);
101     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
102 
103     rc = pthread_create(&threads[1], &attr, PthreadF02, (void *)t2);
104     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
105 
106     for (i = 0; i < LOOP_NUM; i++) {
107         rc = pthread_join(threads[i], NULL);
108         ICUNIT_ASSERT_EQUAL(rc, 0, rc);
109     }
110 
111     rc = pthread_attr_destroy(&attr);
112     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
113 
114     rc = pthread_mutex_destroy(&g_pthreadMutexTest1);
115     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
116     rc = pthread_cond_destroy(&g_pthreadCondTest1);
117     ICUNIT_ASSERT_EQUAL(rc, 0, rc);
118 
119     return LOS_OK;
120 }
121 
122 /**
123  * @tc.name: ItPosixPthread013
124  * @tc.desc: Test interface pthread_mutex_init
125  * @tc.type: FUNC
126  * @tc.require: issueI5TIRQ
127  */
128 
ItPosixPthread013(VOID)129 VOID ItPosixPthread013(VOID)
130 {
131     TEST_ADD_CASE("ItPosixPthread013", Testcase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
132 }
133