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
ThreadFuncTest(void * args)32 static void *ThreadFuncTest(void *args)
33 {
34 return (void *)NULL;
35 }
36
ChildProcess(void)37 static int ChildProcess(void)
38 {
39 int ret;
40 int currPolicy = 0;
41 int oldPolicy;
42 pthread_t newUserThread;
43 pthread_attr_t a = { 0 };
44 struct sched_param hpfparam = { 0 };
45 volatile unsigned int count = 0;
46 struct sched_param currSchedParam = { 0 };
47 int currTID = Syscall(SYS_gettid, 0, 0, 0, 0);
48 struct sched_param param = {
49 .sched_deadline = 1000000, /* 1000000, 1s */
50 .sched_runtime = 20000, /* 20000, 20ms */
51 .sched_period = 1000000, /* 1000000, 1s */
52 };
53
54 ret = pthread_getschedparam(pthread_self(), &oldPolicy, &hpfparam);
55 ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
56
57 ret = sched_setscheduler(getpid(), SCHED_DEADLINE, ¶m);
58 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
59
60 ret = pthread_getschedparam(pthread_self(), &currPolicy, &currSchedParam);
61 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
62 ICUNIT_ASSERT_EQUAL(currPolicy, SCHED_DEADLINE, LOS_NOK);
63 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_deadline, 1000000, LOS_NOK); /* 1000000, 1s */
64 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_runtime, 20000, LOS_NOK); /* 20000, 20ms */
65 ICUNIT_ASSERT_EQUAL(currSchedParam.sched_period, 1000000, LOS_NOK); /* 1000000, 1s */
66
67 ret = pthread_attr_init(&a);
68 pthread_attr_setschedpolicy(&a, SCHED_RR);
69 pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
70 ret = pthread_create(&newUserThread, &a, ThreadFuncTest, NULL);
71 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
72
73 printf("--- edf2 -- 1 -- Tid[%d] thread end ---\n\r", currTID);
74
75 return 0;
76 }
77
TestCase(void)78 static int TestCase(void)
79 {
80 int ret, pid, status;
81
82 pid = fork();
83 if (pid == 0) {
84 ret = ChildProcess();
85 if (ret != 0) {
86 exit(-1);
87 }
88 exit(0);
89 } else if (pid > 0) {
90 waitpid(pid, &status, 0);
91 } else {
92 exit(__LINE__);
93 }
94
95 return WEXITSTATUS(status) == 0 ? 0 : -1;
96 }
97
ItTestPthread023(void)98 void ItTestPthread023(void)
99 {
100 TEST_ADD_CASE("IT_POSIX_PTHREAD_023", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
101 }
102