• 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 <pthread.h>
17 #include "test.h"
18 
19 static pthread_t g_pthd_1, g_pthd_2;
20 
threadfuncA(void * arg)21 void *threadfuncA(void *arg)
22 {
23     g_pthd_1 = pthread_self();
24     return arg;
25 }
26 
threadfuncB(void * arg)27 void *threadfuncB(void *arg)
28 {
29     pthread_t pth = pthread_self();
30     int ret = pthread_equal(g_pthd_1, pth);
31     if (ret != 0) {
32         t_error("%s two should not equal, ret is %d\n", __func__, ret);
33     }
34     return arg;
35 }
36 
threadfuncC(void * arg)37 void *threadfuncC(void *arg)
38 {
39     int ret = pthread_equal(g_pthd_2, pthread_self());
40     if (ret == 0) {
41         t_error("%s the same thread, the result should be greater than zero, ret = %d\n", __func__, ret);
42     }
43     return arg;
44 }
45 
46 /**
47  * @tc.name      : pthread_equal_0100
48  * @tc.desc      : Compare two different threads
49  * @tc.level     : Level 0
50  */
pthread_equal_0100(void)51 void pthread_equal_0100(void)
52 {
53     pthread_t pthread1, pthread2;
54     int result;
55 
56     result = pthread_create(&pthread1, NULL, threadfuncA, NULL);
57     if (result != 0) {
58         t_error("%s pthread_create failed\n", __func__);
59         return;
60     }
61     result = pthread_create(&pthread2, NULL, threadfuncB, NULL);
62     if (result != 0) {
63         t_error("%s pthread_create failed\n", __func__);
64         return;
65     }
66 
67     result = pthread_join(pthread1, NULL);
68     if (result != 0) {
69         t_error("%s pthread_join failed, result is %d\n", __func__, result);
70     }
71     pthread_join(pthread2, NULL);
72     if (result != 0) {
73         t_error("%s pthread_join failed, result is %d\n", __func__, result);
74     }
75 }
76 
77 /**
78  * @tc.name      : pthread_equal_0200
79  * @tc.desc      : Compare the same thread
80  * @tc.level     : Level 0
81  */
pthread_equal_0200(void)82 void pthread_equal_0200(void)
83 {
84     int result;
85 
86     result = pthread_create(&g_pthd_2, NULL, threadfuncC, NULL);
87     if (result != 0) {
88         t_error("%s pthread_create failed\n", __func__);
89         return;
90     }
91 
92     result = pthread_join(g_pthd_2, NULL);
93     if (result != 0) {
94         t_error("%s pthread_join failed, result is %d\n", __func__, result);
95     }
96 }
97 
main(int argc,char * argv[])98 int main(int argc, char *argv[])
99 {
100     pthread_equal_0100();
101     pthread_equal_0200();
102     return t_status;
103 }