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