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