• 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 <semaphore.h>
18 #include <limits.h>
19 
20 #include "functionalext.h"
21 
22 #define THREADNUM 4
23 
24 pthread_barrier_t mybarrier;
25 
threadfunc(void)26 static void threadfunc(void)
27 {
28     sleep(1);
29     int ret = pthread_barrier_wait(&mybarrier);
30     EXPECT_NE("pthread_barrier_wait_0100", ret, ERREXPECT);
31 
32     return;
33 }
34 
35 /**
36  * @tc.name      : pthread_barrier_wait_0100
37  * @tc.desc      : Blocking and waiting for 4 sub-threads, the return value of the calling function is successful
38  * @tc.level     : Level 1
39  */
pthread_barrier_wait_0100(void)40 void pthread_barrier_wait_0100(void)
41 {
42     int i = 0;
43     pthread_t ids[THREADNUM];
44 
45     int ret = pthread_barrier_init(&mybarrier, NULL, THREADNUM + 1);
46     EXPECT_EQ("pthread_barrier_wait_0100", ret, CMPFLAG);
47 
48     for (i = 0; i < THREADNUM; i++) {
49         if (pthread_create(&ids[i], NULL, (void *)threadfunc, NULL) != 0) {
50             t_error("create thread failed\n");
51             return;
52         }
53     }
54 
55     ret = pthread_barrier_wait(&mybarrier);
56 
57     for (i = 0; i < THREADNUM; i++) {
58         ret = pthread_join(ids[i], NULL);
59         if (ret != 0) {
60             t_error("thread wait failed\n");
61             return;
62         }
63     }
64 
65     ret = pthread_barrier_destroy(&mybarrier);
66     EXPECT_EQ("pthread_barrier_wait_0100", ret, CMPFLAG);
67 }
68 
69 /**
70  * @tc.name      : pthread_barrier_wait_0200
71  * @tc.desc      : The number of blocked threads is 1, and the calling function returns ERREXPECT
72  * @tc.level     : Level 2
73  */
pthread_barrier_wait_0200(void)74 void pthread_barrier_wait_0200(void)
75 {
76     int threadnum = 1;
77 
78     int ret = pthread_barrier_init(&mybarrier, NULL, threadnum);
79     EXPECT_EQ("pthread_barrier_wait_0200", ret, CMPFLAG);
80 
81     ret = pthread_barrier_wait(&mybarrier);
82     EXPECT_EQ("pthread_barrier_wait_0200", ret, ERREXPECT);
83 
84     ret = pthread_barrier_destroy(&mybarrier);
85     EXPECT_EQ("pthread_barrier_wait_0200", ret, CMPFLAG);
86 }
87 
main(void)88 int main(void)
89 {
90     pthread_barrier_wait_0100();
91     pthread_barrier_wait_0200();
92 
93     return t_status;
94 }