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 <threads.h>
18 #include "functionalext.h"
19
20 static cnd_t cond;
21 static mtx_t mutex;
22
23 extern int __cnd_timedwait_time64(cnd_t *__restrict, mtx_t *__restrict, const struct timespec *__restrict);
24
signal_parent(void * arg)25 static int signal_parent(void *arg)
26 {
27 if (mtx_lock(&mutex) != thrd_success) {
28 t_error("%s mtx_lock failed\n", __func__);
29 }
30 if (cnd_signal(&cond) != thrd_success) {
31 t_error("%s cnd_signal failed\n", __func__);
32 }
33 if (mtx_unlock(&mutex) != thrd_success) {
34 t_error("%s mtx_unlock failed\n", __func__);
35 }
36
37 thrd_exit(thrd_success);
38 }
39
40 /**
41 * @tc.name : cnd_timedwait_0100
42 * @tc.desc : The parameter is valid and can block the calling thread and release the specified mutex.
43 * @tc.level : Level 0
44 */
cnd_timedwait_0100(void)45 void cnd_timedwait_0100(void)
46 {
47 thrd_t id;
48 struct timespec w_time;
49
50 if (cnd_init(&cond) != thrd_success) {
51 t_error("%s cnd_init failed\n", __func__);
52 }
53 if (mtx_init(&mutex, mtx_plain) != thrd_success) {
54 t_error("%s mtx_init failed\n", __func__);
55 }
56 if (mtx_lock(&mutex) != thrd_success) {
57 t_error("%s mtx_lock failed\n", __func__);
58 }
59 if (clock_gettime(CLOCK_REALTIME, &w_time) != 0) {
60 t_error("%s clock_gettime failed\n", __func__);
61 }
62
63 w_time.tv_sec += 3600;
64
65 if (thrd_create(&id, signal_parent, NULL) != thrd_success) {
66 t_error("%s thrd_create failed\n", __func__);
67 }
68
69 if (cnd_timedwait(&cond, &mutex, &w_time) != thrd_success) {
70 t_error("%s cnd_timedwait failed\n", __func__);
71 }
72
73 if (thrd_join(id, NULL) != thrd_success) {
74 t_error("%s thrd_join failed\n", __func__);
75 }
76
77 if (mtx_unlock(&mutex) != thrd_success) {
78 t_error("%s mtx_unlock failed\n", __func__);
79 }
80 mtx_destroy(&mutex);
81 cnd_destroy(&cond);
82 }
83
84 /**
85 * @tc.name : cnd_timedwait_time64_0100
86 * @tc.desc : The parameter is valid and can block the calling thread and release the specified mutex.
87 * @tc.level : Level 0
88 */
cnd_timedwait_time64_0100(void)89 void cnd_timedwait_time64_0100(void)
90 {
91 thrd_t id;
92 struct timespec w_time;
93
94 if (cnd_init(&cond) != thrd_success) {
95 t_error("%s cnd_init failed\n", __func__);
96 }
97 if (mtx_init(&mutex, mtx_plain) != thrd_success) {
98 t_error("%s mtx_init failed\n", __func__);
99 }
100 if (mtx_lock(&mutex) != thrd_success) {
101 t_error("%s mtx_lock failed\n", __func__);
102 }
103 if (clock_gettime(CLOCK_REALTIME, &w_time) != 0) {
104 t_error("%s clock_gettime failed\n", __func__);
105 }
106
107 w_time.tv_sec += 3600;
108
109 if (thrd_create(&id, signal_parent, NULL) != thrd_success) {
110 t_error("%s thrd_create failed\n", __func__);
111 }
112
113 if (__cnd_timedwait_time64(&cond, &mutex, &w_time) != thrd_success) {
114 t_error("%s cnd_timedwait failed\n", __func__);
115 }
116
117 if (thrd_join(id, NULL) != thrd_success) {
118 t_error("%s thrd_join failed\n", __func__);
119 }
120
121 if (mtx_unlock(&mutex) != thrd_success) {
122 t_error("%s mtx_unlock failed\n", __func__);
123 }
124 mtx_destroy(&mutex);
125 cnd_destroy(&cond);
126 }
127
main(int argc,char * argv[])128 int main(int argc, char *argv[])
129 {
130 cnd_timedwait_0100();
131 cnd_timedwait_time64_0100();
132 return t_status;
133 }