• 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_pthread_test.h"
32 
33 static pthread_barrier_t g_barrier;
34 static int g_testToCount001 = 0;
35 static int g_threadTest[10];
36 
ThreadFuncTest0(void * a)37 static void *ThreadFuncTest0(void *a)
38 {
39     int ret;
40     int count = *((int *)a);
41     g_testToCount001++;
42 
43     ret = pthread_barrier_wait(&g_barrier);
44     ICUNIT_GOTO_EQUAL(ret, PTHREAD_BARRIER_SERIAL_THREAD, ret, EXIT);
45 
46     g_threadTest[count] = count;
47 
48 EXIT:
49     return NULL;
50 }
51 
ThreadFuncTest2(void * a)52 static void *ThreadFuncTest2(void *a)
53 {
54     int ret;
55     int count = *((int *)a);
56     g_testToCount001++;
57 
58     ret = pthread_barrier_wait(&g_barrier);
59     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
60 
61     g_threadTest[count] = count;
62 
63 EXIT:
64     return NULL;
65 }
66 
ThreadFuncTest1(void * a)67 static void *ThreadFuncTest1(void *a)
68 {
69     int ret;
70     int count = *((int *)a);
71     g_testToCount001++;
72 
73     ret = pthread_barrier_wait(&g_barrier);
74     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
75 
76     g_threadTest[count] = count;
77 EXIT:
78     return NULL;
79 }
80 
Testcase(void)81 static int Testcase(void)
82 {
83     struct sched_param param = { 0 };
84     int ret;
85     void *res = NULL;
86     pthread_attr_t a = { 0 };
87     pthread_t thread;
88     pthread_t newPthread[10], newPthread1;
89     pthread_mutexattr_t mutex;
90     int index = 0;
91     int currThreadPri, currThreadPolicy;
92     int threadParam[10];
93     ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, &param);
94     ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
95     currThreadPri = param.sched_priority;
96     const int testCount = 10;
97 
98     g_testToCount001 = 0;
99 
100     ret = pthread_attr_init(&a);
101     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
102     param.sched_priority = currThreadPri - 1;
103     pthread_attr_setschedparam(&a, &param);
104 
105     ret = pthread_barrier_init(&g_barrier, NULL, testCount);
106     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
107 
108     threadParam[0] = 0;
109     ret = pthread_create(&newPthread[index], &a, ThreadFuncTest0, &threadParam[0]);
110     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
111     g_threadTest[0] = 0;
112 
113     index = 1;
114     while (index < (testCount - 1)) {
115         threadParam[index] = index;
116         ret = pthread_create(&newPthread[index], &a, ThreadFuncTest1, &threadParam[index]);
117         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
118         g_threadTest[index] = 0;
119         index++;
120     }
121 
122     ICUNIT_ASSERT_EQUAL(g_testToCount001, testCount - 1, g_testToCount001);
123 
124     threadParam[index] = index;
125     ret = pthread_create(&newPthread[index], &a, ThreadFuncTest2, &threadParam[index]);
126     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
127 
128     sleep(1);
129 
130     ICUNIT_ASSERT_EQUAL(g_testToCount001, testCount, g_testToCount001);
131 
132     index = 0;
133     while (index < testCount) {
134         threadParam[index] = index;
135         ret = pthread_join(newPthread[index], NULL);
136         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
137         ICUNIT_ASSERT_EQUAL(g_threadTest[index], index, g_threadTest[index]);
138         index++;
139     }
140 
141     ret = pthread_barrier_destroy(&g_barrier);
142     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
143 
144     return 0;
145 }
146 
ItTestPthread006(void)147 void ItTestPthread006(void)
148 {
149     TEST_ADD_CASE("IT_POSIX_PTHREAD_006", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
150 }
151