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_pthread_test.h"
32
33
34 static pthread_t g_th;
PthreadTest115(VOID * arg)35 static VOID *PthreadTest115(VOID *arg)
36 {
37 g_testCount++;
38 sleep(1);
39 g_testCount++;
40
41 return NULL;
42 }
43
PthreadFunc1115(VOID * arg)44 static VOID *PthreadFunc1115(VOID *arg)
45 {
46 int ret = 0;
47 g_testCount++;
48 usleep(1000 * 10 * 10); // 1000 * 10 * 10, delay for timimg control.
49
50 ret = pthread_join(g_th, NULL);
51 ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
52 g_testCount++;
53 EXIT:
54 return NULL;
55 }
56
PthreadFunc2115(VOID * arg)57 static VOID *PthreadFunc2115(VOID *arg)
58 {
59 int ret = 0;
60 g_testCount++;
61 usleep(1000 * 10 * 15); // 1000 * 10 * 15, delay for timimg control.
62
63 ret = pthread_join(g_th, NULL);
64 ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
65 g_testCount++;
66 EXIT:
67 return NULL;
68 }
69
PthreadFunc3115(VOID * arg)70 static VOID *PthreadFunc3115(VOID *arg)
71 {
72 int ret = 0;
73 g_testCount++;
74 usleep(1000 * 10 * 6); // 1000 * 10 * 6, delay for timimg control.
75
76 ret = pthread_detach(g_th);
77 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
78 g_testCount++;
79 ;
80
81 EXIT:
82 return NULL;
83 }
84
Testcase(VOID)85 static UINT32 Testcase(VOID)
86 {
87 int rc;
88
89 int ret = 0;
90 pthread_t th1;
91 pthread_t th2;
92 pthread_t th3;
93 pthread_attr_t attr;
94 g_testCount = 0;
95 pthread_attr_init(&attr);
96 pthread_create(&g_th, &attr, PthreadTest115, NULL);
97
98 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
99 pthread_create(&th1, &attr, PthreadFunc1115, NULL);
100 pthread_create(&th2, &attr, PthreadFunc2115, NULL);
101 pthread_create(&th3, &attr, PthreadFunc3115, NULL);
102 ret = pthread_join(g_th, NULL);
103 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
104 usleep(1000 * 10 * 50); // 1000 * 10 * 50, delay for timimg control.
105 ICUNIT_ASSERT_EQUAL(g_testCount, 8, g_testCount); // 8, assert the exit code.
106 return 0;
107 }
108
ItTestPthread008(void)109 void ItTestPthread008(void)
110 {
111 TEST_ADD_CASE("IT_POSIX_PTHREAD_008", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
112 }
113