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 rc;
63
64 ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT);
65 g_testCount++;
66 rc = pthread_mutex_lock(&g_pthreadMutexTest1);
67 ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
68
69 ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // 3, here assert the result.
70 g_testCount++;
71 rc = pthread_cond_signal(&g_pthreadCondTest1);
72 ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
73
74 ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT); // 4, here assert the result.
75 g_testCount++;
76
77 rc = pthread_mutex_unlock(&g_pthreadMutexTest1); /* Increase latency for thread polling mutex */
78 ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT);
79 LOS_TaskDelay(2); // 2, delay for Timing control.
80
81 EXIT:
82 return NULL;
83 }
Testcase(VOID)84 static UINT32 Testcase(VOID)
85 {
86 int i;
87 long t1 = 1;
88 long t2 = 2; // 2, init
89 int rc;
90 pthread_t threads[3]; // 3, need 3 pthread for test.
91 pthread_attr_t attr;
92
93 g_testCount = 0;
94 pthread_mutex_init(&g_pthreadMutexTest1, NULL);
95 /* When creating thread, it is set to connectable state, which is easy to transplant */
96 pthread_cond_init(&g_pthreadCondTest1, NULL);
97 pthread_attr_init(&attr);
98
99 rc = pthread_create(&threads[0], &attr, PthreadF01, (void *)t1);
100 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
101
102 rc = pthread_create(&threads[1], &attr, PthreadF02, (void *)t2);
103 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
104
105 for (i = 0; i < LOOP_NUM; i++) {
106 rc = pthread_join(threads[i], NULL);
107 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
108 }
109
110 rc = pthread_attr_destroy(&attr);
111 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
112
113 rc = pthread_mutex_destroy(&g_pthreadMutexTest1);
114 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
115 rc = pthread_cond_destroy(&g_pthreadCondTest1);
116 ICUNIT_ASSERT_EQUAL(rc, 0, rc);
117
118 return LOS_OK;
119 }
120
121 /**
122 * @tc.name: ItPosixPthread013
123 * @tc.desc: Test interface pthread_mutex_init
124 * @tc.type: FUNC
125 * @tc.require: issueI5TIRQ
126 */
127
ItPosixPthread013(VOID)128 VOID ItPosixPthread013(VOID)
129 {
130 TEST_ADD_CASE("ItPosixPthread013", Testcase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
131 }
132