1 /*
2 * Copyright (c) 2023-2023 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 #include "it_pthread_test.h"
31
ChildProcess(void)32 static int ChildProcess(void)
33 {
34 int ret, currPolicy = 0;
35 volatile unsigned int count = 0;
36 struct sched_param currSchedParam = { 0 };
37 struct sched_param param = {
38 .sched_deadline = 1000000, /* 1000000, 1s */
39 .sched_runtime = 20000, /* 20000, 20ms */
40 .sched_period = 1000000, /* 1000000, 1s */
41 };
42
43 ret = sched_getparam(getpid(), &currSchedParam);
44 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
45
46 currPolicy = sched_getscheduler(getpid());
47 ICUNIT_ASSERT_EQUAL(currPolicy, SCHED_RR, LOS_NOK);
48 currSchedParam.sched_runtime = 0;
49 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_deadline, 0, LOS_NOK);
50 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_runtime, 0, LOS_NOK);
51 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_period, 0, LOS_NOK);
52
53 ret = sched_setscheduler(getpid(), SCHED_DEADLINE, ¶m);
54 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
55
56 currPolicy = sched_getscheduler(getpid());
57 ICUNIT_ASSERT_EQUAL(currPolicy, SCHED_DEADLINE, LOS_NOK);
58
59 ret = sched_getparam(getpid(), &currSchedParam);
60 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
61 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_deadline, 1000000, LOS_NOK); /* 1000000, 1s */
62 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_runtime, 20000, LOS_NOK); /* 20000, 20ms */
63 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_period, 1000000, LOS_NOK); /* 1000000, 1s */
64
65 printf("--- 1 edf thread start --\n\r");
66 do {
67 for (volatile int i = 0; i < 10000; i++) { /* 10000, no special meaning */
68 for (volatile int j = 0; j < 5; j++) { /* 5, no special meaning */
69 int tmp = i - j;
70 }
71 }
72 if (count % 3 == 0) { /* 3, no special meaning */
73 printf("--- 2 edf thread running --\n\r");
74 }
75 count++;
76 } while (count <= 6); /* 6, no special meaning */
77 printf("--- 3 edf thread end --\n\r");
78
79 return 0;
80 }
81
TestCase(void)82 static int TestCase(void)
83 {
84 int ret, pid, status;
85
86 pid = fork();
87 if (pid == 0) {
88 ret = ChildProcess();
89 if (ret != 0) {
90 exit(-1);
91 }
92 exit(0);
93 } else if (pid > 0) {
94 waitpid(pid, &status, 0);
95 } else {
96 exit(__LINE__);
97 }
98
99 return WEXITSTATUS(status) == 0 ? 0 : -1;
100 }
101
ItTestPthread020(void)102 void ItTestPthread020(void)
103 {
104 TEST_ADD_CASE("IT_POSIX_PTHREAD_020", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
105 }
106