• 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_cond_t g_pthread_cond;
34 static pthread_mutex_t g_pthread_mutex;
35 #define TEST_THREAD_COUNT 5
pthread_cond_func001(void * arg)36 static void *pthread_cond_func001(void *arg)
37 {
38     int ret;
39     struct timespec ts;
40 
41     g_testCount++;
42 
43     ret = pthread_mutex_lock(&g_pthread_mutex);
44     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
45 
46     clock_gettime(CLOCK_REALTIME, &ts);
47     ts.tv_sec += 60; /* 60: wait 1 minute */
48 
49     ret = pthread_cond_timedwait(&g_pthread_cond, &g_pthread_mutex, &ts);
50     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
51 
52     ret = pthread_mutex_unlock(&g_pthread_mutex);
53     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
54 
55     g_testCount++;
56 EXIT:
57     return NULL;
58 }
59 
pthread_f06(void * argument)60 static VOID *pthread_f06(void *argument)
61 {
62     int policy;
63     int ret;
64     int i;
65     pthread_attr_t attr;
66     struct sched_param schedParam = { 0 };
67     pthread_t thread[TEST_THREAD_COUNT];
68 
69     g_testCount = 0;
70 
71     ret = pthread_attr_init(&attr);
72     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
73 
74     ret = pthread_getschedparam(pthread_self(), &policy, &schedParam);
75     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
76 
77     schedParam.sched_priority -= 1;
78     ret = pthread_attr_setschedparam(&attr, &schedParam);
79     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
80 
81     ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
82     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
83 
84     for (i = 0; i < TEST_THREAD_COUNT; i++) {
85         ret = pthread_create(&thread[i], &attr, pthread_cond_func001, NULL);
86         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
87     }
88 
89     sleep(1);
90 
91     ICUNIT_GOTO_EQUAL(g_testCount, 5, g_testCount, EXIT); /* 5: Five threads */
92 
93     ret = pthread_mutex_lock(&g_pthread_mutex);
94     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
95 
96     ret = pthread_cond_broadcast(&g_pthread_cond);
97     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
98 
99     ret = pthread_mutex_unlock(&g_pthread_mutex);
100     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
101 
102     for (i = 0; i < TEST_THREAD_COUNT; i++) {
103         ret = pthread_join(thread[i], NULL);
104         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
105     }
106 
107     ICUNIT_GOTO_EQUAL(g_testCount, 10, g_testCount, EXIT); /* 10: Twice per thread */
108 EXIT:
109     return NULL;
110 }
111 
TestCase(void)112 static int TestCase(void)
113 {
114     int policy;
115     pthread_attr_t attr;
116     pthread_t newTh;
117     struct sched_param schedParam = { 0 };
118     int ret;
119 
120     ret = pthread_mutex_init(&g_pthread_mutex, NULL);
121     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
122 
123     ret = pthread_cond_init(&g_pthread_cond, NULL);
124     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
125 
126     ret = pthread_attr_init(&attr);
127     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
128 
129     ret = pthread_getschedparam(pthread_self(), &policy, &schedParam);
130     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
131 
132     schedParam.sched_priority -= 1;
133     ret = pthread_attr_setschedparam(&attr, &schedParam);
134     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
135 
136     ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
137     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
138 
139     ret = pthread_create(&newTh, &attr, pthread_f06, NULL);
140     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
141 
142     ret = pthread_join(newTh, NULL);
143     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
144 
145     return 0;
146 }
147 
ItTestPthreadCond003(void)148 void ItTestPthreadCond003(void)
149 {
150     TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_003", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
151 }
152