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 static pthread_cond_t g_pthread_cond;
33 static pthread_mutex_t g_pthread_mutex;
34 #define TEST_THREAD_COUNT 5
35
pthread_cond_func002(void * arg)36 static void *pthread_cond_func002(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 += 2; /* 2: wait 2 seconds */
48
49 ret = pthread_cond_timedwait(&g_pthread_cond, &g_pthread_mutex, &ts);
50 ICUNIT_GOTO_EQUAL(ret, ETIMEDOUT, 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_f07(void * argument)60 static VOID *pthread_f07(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_func002, 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 for (i = 0; i < TEST_THREAD_COUNT; i++) {
94 ret = pthread_join(thread[i], NULL);
95 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
96 }
97
98 ICUNIT_GOTO_EQUAL(g_testCount, 10, g_testCount, EXIT); /* 10: Twice per thread */
99
100 EXIT:
101 return NULL;
102 }
103
TestCase(void)104 static int TestCase(void)
105 {
106 int policy;
107 pthread_attr_t attr;
108 pthread_t newTh;
109 struct sched_param schedParam = { 0 };
110 int ret;
111
112 ret = pthread_mutex_init(&g_pthread_mutex, NULL);
113 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
114
115 ret = pthread_cond_init(&g_pthread_cond, NULL);
116 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
117
118 ret = pthread_attr_init(&attr);
119 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
120
121 ret = pthread_getschedparam(pthread_self(), &policy, &schedParam);
122 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
123
124 schedParam.sched_priority -= 1;
125 ret = pthread_attr_setschedparam(&attr, &schedParam);
126 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
127
128 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
129 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
130
131 ret = pthread_create(&newTh, &attr, pthread_f07, NULL);
132 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
133
134 ret = pthread_join(newTh, NULL);
135 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
136
137 return 0;
138 }
139
ItTestPthreadCond004(void)140 void ItTestPthreadCond004(void)
141 {
142 TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_004", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
143 }
144