1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <time.h>
17 #include <threads.h>
18 #include "test.h"
19
20 #define ms (1000.0)
21 #define us (1000000)
22 #define ns (1000000000L)
23
ustimer(struct timespec tss,struct timespec tse)24 double ustimer(struct timespec tss, struct timespec tse)
25 {
26 double sd = difftime(tse.tv_sec, tss.tv_sec);
27 long nsd = tse.tv_nsec - tss.tv_nsec;
28 if (nsd < 0) {
29 return us * (sd - 1) + (ns + nsd) / ms;
30 } else {
31 return us * (sd) + nsd / ms;
32 }
33 }
34
yieldfunc(void)35 void yieldfunc(void)
36 {
37 struct timespec tss, tse;
38 timespec_get(&tss, TIME_UTC);
39 do {
40 thrd_yield();
41 timespec_get(&tse, TIME_UTC);
42 } while (ustimer(tss, tse) < 100.0);
43 }
44
45 /**
46 * @tc.name : thrd_yield_0100
47 * @tc.desc : Busy wait while yielding
48 * @tc.level : Level 0
49 */
thrd_yield_0100(void)50 void thrd_yield_0100(void)
51 {
52 int result;
53 struct timespec tss, tse;
54
55 result = timespec_get(&tss, TIME_UTC);
56 if (result != TIME_UTC) {
57 t_error("%s timespec_get start failed", __func__);
58 return;
59 }
60
61 yieldfunc();
62
63 result = timespec_get(&tse, TIME_UTC);
64 if (result != TIME_UTC) {
65 t_error("%s timespec_get end failed", __func__);
66 return;
67 }
68
69 if ((ustimer(tss, tse) - 100.0) < 0) {
70 t_error("%s sleep time is not 100us", __func__);
71 return;
72 }
73 }
74
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 thrd_yield_0100();
78 return t_status;
79 }