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 <signal.h>
17 #include <stdlib.h>
18 #include <threads.h>
19 #include "test.h"
20
21 static thrd_t thr;
22 static int count = 0;
23
threadfuncA(void * arg)24 int threadfuncA(void *arg)
25 {
26 count++;
27 thrd_t id = thrd_current();
28
29 if (!(thrd_equal(id, thr))) {
30 t_error("%s thrd_current failed", __func__);
31 }
32
33 thrd_exit(thrd_success);
34 }
35
threadfuncB(void * arg)36 int threadfuncB(void *arg)
37 {
38 count++;
39
40 thrd_exit(thrd_success);
41 }
42
43 /**
44 * @tc.name : thrd_equal_0100
45 * @tc.desc : Test that two threads have the same ID
46 * @tc.level : Level 0
47 */
thrd_equal_0100(void)48 void thrd_equal_0100(void)
49 {
50 int result;
51
52 result = thrd_create(&thr, threadfuncA, NULL);
53 if (result != thrd_success) {
54 t_error("%s thrd_create failed", __func__);
55 }
56
57 result = thrd_sleep(&(struct timespec){.tv_sec = 1}, NULL);
58 if (result != 0) {
59 t_error("%s thrd_sleep failed", __func__);
60 }
61
62 result = thrd_join(thr, NULL);
63 if (result != thrd_success) {
64 t_error("%s thrd_join failed", __func__);
65 }
66
67 if (count != 1) {
68 t_error("%s failed, count is %d", __func__, count);
69 }
70 count = 0;
71 }
72
73 /**
74 * @tc.name : thrd_equal_0200
75 * @tc.desc : Test that two threads have different ID
76 * @tc.level : Level 1
77 */
thrd_equal_0200(void)78 void thrd_equal_0200(void)
79 {
80 thrd_t thr1, thr2;
81 int result;
82
83 result = thrd_create(&thr1, threadfuncB, NULL);
84 if (result != thrd_success) {
85 t_error("%s thrd_create failed", __func__);
86 }
87
88 result = thrd_create(&thr2, threadfuncB, NULL);
89 if (result != thrd_success) {
90 t_error("%s thrd_create failed", __func__);
91 }
92
93 result = thrd_sleep(&(struct timespec){.tv_sec = 1}, NULL);
94 if (result != 0) {
95 t_error("%s thrd_sleep failed", __func__);
96 }
97
98 if (thrd_equal(thr1, thr2)) {
99 t_error("%s failed, thr1 and thr2 equal", __func__);
100 }
101
102 if (thrd_equal(thr1, thrd_current())) {
103 t_error("%s failed, thr1 and current thread equal", __func__);
104 }
105
106 if (thrd_equal(thr2, thrd_current())) {
107 t_error("%s failed, thr2 and current thread equal", __func__);
108 }
109
110 result = thrd_join(thr1, NULL);
111 if (result != thrd_success) {
112 t_error("%s thrd_join failed", __func__);
113 }
114
115 result = thrd_join(thr2, NULL);
116 if (result != thrd_success) {
117 t_error("%s thrd_join failed", __func__);
118 }
119
120 if (count != 2) {
121 t_error("%s failed, count is %d", __func__, count);
122 }
123 count = 0;
124 }
125
main(int argc,char * argv[])126 int main(int argc, char *argv[])
127 {
128 thrd_equal_0100();
129 thrd_equal_0200();
130 return t_status;
131 }