• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int pt = (int)args;
35     int ret,  currPolicy;
36     struct sched_param currSchedParam;
37     volatile unsigned int count = 0;
38     int currTID = Syscall(SYS_gettid, 0, 0, 0, 0);
39 
40     ret = pthread_getschedparam(pthread_self(), & currPolicy, &currSchedParam);
41     ICUNIT_GOTO_EQUAL(ret, 0, LOS_NOK, EXIT);
42     ICUNIT_GOTO_EQUAL( currPolicy, SCHED_DEADLINE, LOS_NOK, EXIT);
43     ICUNIT_GOTO_EQUAL(currSchedParam.sched_deadline, 1000000, LOS_NOK, EXIT);    /* 1000000, 1s */
44     ICUNIT_GOTO_EQUAL(currSchedParam.sched_runtime, 20000, LOS_NOK, EXIT);       /* 20000, 20ms */
45     ICUNIT_GOTO_EQUAL(currSchedParam.sched_period, 1000000, LOS_NOK, EXIT);      /* 1000000, 1s */
46 
47     printf("--- 1 edf Tid[%d] PTid[%d] thread start ---\n\r", currTID, pt);
48     do {
49         for (volatile int i = 0; i < 100000; i++) { /* 100000, no special meaning */
50             for (volatile int j = 0; j < 5; j++) { /* 5, no special meaning */
51                 volatile int tmp = i - j;
52             }
53         }
54         if (count % 3 == 0) {  /* 3, no special meaning */
55             printf("--- 2 edf Tid[%d] PTid[%d] thread running ---\n\r", currTID, pt);
56         }
57         count++;
58     } while (count <= 6); /* 6, no special meaning */
59     printf("--- 3 edf Tid[%d] PTid[%d] thread end ---\n\r", currTID, pt);
60 
61     ret = LOS_OK;
62     return (void *)(&ret);
63 EXIT:
64     ret = LOS_NOK;
65     return (void *)(&ret);
66 }
67 
ChildProcess(void)68 static int ChildProcess(void)
69 {
70     int *childThreadRetval = nullptr;
71     pthread_t newUserThread;
72     int ret,  currPolicy = 0;
73     volatile unsigned int count = 0;
74     struct sched_param currSchedParam = { 0 };
75     int currTID = Syscall(SYS_gettid, 0, 0, 0, 0);
76     struct sched_param param = {
77         .sched_deadline = 1000000,  /* 1000000, 1s */
78         .sched_runtime = 20000,    /* 20000, 20ms */
79         .sched_period = 1000000,    /* 1000000, 1s */
80     };
81 
82     ret = sched_setscheduler(getpid(), SCHED_DEADLINE, &param);
83     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
84 
85     ret = pthread_getschedparam(pthread_self(), & currPolicy, &currSchedParam);
86     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
87     ICUNIT_ASSERT_EQUAL(currPolicy, SCHED_DEADLINE, LOS_NOK);
88     ICUNIT_ASSERT_EQUAL(currSchedParam.sched_deadline, 1000000, LOS_NOK);    /* 1000000, 1s */
89     ICUNIT_ASSERT_EQUAL(currSchedParam.sched_runtime, 20000, LOS_NOK);       /* 20000, 20ms */
90     ICUNIT_ASSERT_EQUAL(currSchedParam.sched_period, 1000000, LOS_NOK);      /* 1000000, 1s */
91 
92     ret = pthread_create(&newUserThread, NULL, ThreadFuncTest, (void *)currTID);
93     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
94 
95     printf("--- 1 edf Tid[%d] thread start ---\n\r", currTID);
96     do {
97         for (volatile int i = 0; i < 10000; i++) { /* 10000, no special meaning */
98             for (volatile int j = 0; j < 5; j++) { /* 5, no special meaning */
99                 int tmp = i - j;
100             }
101         }
102         if (count % 3 == 0) {  /* 3, no special meaning */
103             printf("--- 2 edf Tid[%d] thread running ---\n\r", currTID);
104         }
105         count++;
106     } while (count <= 6); /* 6, no special meaning */
107     printf("--- 3 edf Tid[%d] thread end ---\n\r", currTID);
108 
109     ret = pthread_join(newUserThread, (void **)&childThreadRetval);
110     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
111     ICUNIT_ASSERT_EQUAL(*childThreadRetval, 0, *childThreadRetval);
112 
113     return 0;
114 }
115 
TestCase(void)116 static int TestCase(void)
117 {
118     int ret, pid, status;
119 
120     pid = fork();
121     if (pid == 0) {
122         ret = ChildProcess();
123         if (ret != 0) {
124             exit(-1);
125         }
126         exit(0);
127     } else if (pid > 0) {
128         waitpid(pid, &status, 0);
129     } else {
130         exit(__LINE__);
131     }
132 
133     return WEXITSTATUS(status) == 0 ? 0 : -1;
134 }
135 
ItTestPthread021(void)136 void ItTestPthread021(void)
137 {
138     TEST_ADD_CASE("IT_POSIX_PTHREAD_021", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
139 }
140