• 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     int ret = thrd_detach(thrd_current());
32     if (ret != thrd_success) {
33         t_error("%s thrd_detach failed", __func__);
34     }
35 
36     thrd_exit(thrd_success);
37 }
38 
39 /**
40  * @tc.name      : thrd_detach_0100
41  * @tc.desc      : Detaches the thread identified by thr from the current environment
42  * @tc.level     : Level 0
43  */
thrd_detach_0100(void)44 void thrd_detach_0100(void)
45 {
46     thrd_t id;
47     int result;
48 
49     result = thrd_create(&id, threadfunc, NULL);
50     if (result != thrd_success) {
51         t_error("%s thrd_create failed", __func__);
52     }
53 
54     result = thrd_sleep(&(struct timespec){.tv_sec = 1}, NULL);
55     if (result != 0) {
56         t_error("%s thrd_sleep failed", __func__);
57     }
58 
59     if (count != 1) {
60         t_error("%s failed, count is %d", __func__, count);
61     }
62     count = 0;
63 }
64 
65 /**
66  * @tc.name      : thrd_detach_0200
67  * @tc.desc      : Use thrd_join after thrd_detach
68  * @tc.level     : Level 2
69  */
thrd_detach_0200(void)70 void thrd_detach_0200(void)
71 {
72     signal(SIGSEGV, exception_handler);
73 
74     thrd_t id;
75     int result;
76 
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_sec = 1}, NULL);
83     if (result != 0) {
84         t_error("%s thrd_sleep failed", __func__);
85     }
86 
87     result = thrd_join(id, NULL);
88     if (result == thrd_success) {
89         t_error("%s thrd_join should fail", __func__);
90     }
91 
92     if (count != 1) {
93         t_error("%s failed, count is %d", __func__, count);
94     }
95     count = 0;
96 }
97 
main(int argc,char * argv[])98 int main(int argc, char *argv[])
99 {
100     thrd_detach_0100();
101     thrd_detach_0200();
102     return t_status;
103 }