• 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 <stdlib.h>
18 #include <sys/time.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21 int flag = 0;
22 static pthread_cond_t gcond;
23 static pthread_mutex_t gmutex;
24 struct timespec gtm;
25 
threadfuncA(void * arg)26 void *threadfuncA(void *arg)
27 {
28     struct timeval now;
29     struct timespec outtime;
30     gtm.tv_nsec = 1;
31     pthread_mutex_lock(&gmutex);
32     gettimeofday(&now, NULL);
33     outtime.tv_sec = now.tv_sec + 5;
34     outtime.tv_nsec = now.tv_usec * 1000;
35     pthread_cond_timedwait(&gcond, &gmutex, &outtime);
36     flag++;
37     pthread_mutex_unlock(&gmutex);
38     return arg;
39 }
40 
threadfuncB(void * arg)41 void *threadfuncB(void *arg)
42 {
43     pthread_mutex_lock(&gmutex);
44     pthread_cond_signal(&gcond);
45     pthread_mutex_unlock(&gmutex);
46     return arg;
47 }
48 
49 /**
50  * @tc.name:      pthread_cond_timedwait_0100
51  * @tc.desc:      Verify pthread create process success
52  * @tc.level:     level 0.
53  */
pthread_cond_timedwait_0100(void)54 void pthread_cond_timedwait_0100(void)
55 {
56     pthread_t pid1, pid2;
57     pthread_create(&pid1, NULL, threadfuncA, NULL);
58     pthread_create(&pid2, NULL, threadfuncB, NULL);
59     pthread_join(pid1, NULL);
60     pthread_join(pid2, NULL);
61     EXPECT_EQ("pthread_cond_timedwait_0200", flag, 1);
62     pthread_mutex_destroy(&gmutex);
63     pthread_cond_destroy(&gcond);
64 }
65 
main(void)66 int main(void)
67 {
68     pthread_cond_timedwait_0100();
69     return t_status;
70 }
71