• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <sys/wait.h>
21 #include <pthread.h>
22 #include "functionalext.h"
23 
24 static int count = 0;
25 
exception_handler(int sig)26 void exception_handler(int sig)
27 {
28     exit(t_status);
29 }
30 
threadfunc(void * arg)31 int threadfunc(void *arg)
32 {
33     count++;
34     return 0;
35 }
36 
37 /**
38  * @tc.name      : thrd_join_0100
39  * @tc.desc      : Blocks current thread until it identified by thr finishes execution with no return value
40  * @tc.level     : Level 0
41  */
thrd_join_0100(void)42 void thrd_join_0100(void)
43 {
44     thrd_t id;
45     int result;
46     result = thrd_create(&id, threadfunc, NULL);
47     if (result != thrd_success) {
48         t_error("%s thrd_create failed", __func__);
49     }
50 
51     result = thrd_sleep(&(struct timespec){.tv_nsec = 1000000}, NULL);
52     if (result != 0) {
53         t_error("%s thrd_sleep failed", __func__);
54     }
55 
56     result = thrd_join(id, NULL);
57     if (result != thrd_success) {
58         t_error("%s thrd_join failed", __func__);
59     }
60 
61     if (count != 1) {
62         t_error("%s failed, count is %d", __func__, count);
63     }
64     count = 0;
65 }
66 
67 /**
68  * @tc.name      : thrd_join_0200
69  * @tc.desc      : Blocks current thread until it identified by thr finishes execution with return value
70  * @tc.level     : Level 1
71  */
thrd_join_0200(void)72 void thrd_join_0200(void)
73 {
74     thrd_t id;
75     int ret = 1;
76     int result;
77     result = thrd_create(&id, threadfunc, NULL);
78     if (result != thrd_success) {
79         t_error("%s thrd_create failed", __func__);
80     }
81 
82     result = thrd_sleep(&(struct timespec){.tv_nsec = 1000000}, NULL);
83     if (result != 0) {
84         t_error("%s thrd_sleep failed", __func__);
85     }
86 
87     result = thrd_join(id, &ret);
88     if (result != thrd_success) {
89         t_error("%s thrd_join failed", __func__);
90     }
91 
92     if (ret != 0) {
93         t_error("%s return value failed", __func__);
94     }
95 
96     if (count != 1) {
97         t_error("%s failed, count is %d", __func__, count);
98     }
99     count = 0;
100 }
101 
102 /**
103  * @tc.name      : thrd_join_0300
104  * @tc.desc      : Invalid parameter test
105  * @tc.level     : Level 2
106  */
thrd_join_0300(void)107 void thrd_join_0300(void)
108 {
109     pid_t pid = fork();
110     if (pid == -1) {
111         t_error("thrd_join_0300: Error forking process");
112     } else if (pid == 0) {
113         thrd_join(NULL, NULL);
114     } else {
115         int status;
116         waitpid(pid, &status, 0);
117         if (WIFSIGNALED(status)) {
118             int sig = WTERMSIG(status);
119             EXPECT_EQ("thrd_join_0300", SIGABRT, sig);
120         }
121     }
122 }
123 
main(int argc,char * argv[])124 int main(int argc, char *argv[])
125 {
126     thrd_join_0100();
127     thrd_join_0200();
128     thrd_join_0300();
129     return t_status;
130 }