• 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 <threads.h>
18 #include <time.h>
19 #include "functionalext.h"
20 
21 const int TWO = 2;
22 
23 extern int __mtx_timedlock_time64(mtx_t *__restrict, const struct timespec *__restrict);
24 
25 /**
26  * @tc.name      : mtx_timedlock_0100
27  * @tc.desc      : Provide correct parameters, test timeout and lock
28  * @tc.level     : Level 0
29  */
mtx_timedlock_0100(void)30 void mtx_timedlock_0100(void)
31 {
32     struct timespec timeout;
33     timeout.tv_sec = TWO;
34     timeout.tv_nsec = 0;
35     mtx_t mutex;
36     int ret = 0;
37     ret = mtx_init(&mutex, mtx_timed);
38     if (ret != thrd_success) {
39         t_error("%s mtx_init failed", __func__);
40         return;
41     }
42     ret = mtx_timedlock(&mutex, &timeout);
43     EXPECT_EQ("mtx_timedlock_0100", ret, thrd_success);
44     ret = mtx_unlock(&mutex);
45     if (ret != thrd_success) {
46         t_error("%s mtx_unlock failed", __func__);
47         return;
48     }
49     mtx_destroy(&mutex);
50 }
51 
52 /**
53  * @tc.name      : mtx_timedlock_0200
54  * @tc.desc      : In the locked state, the test times out and locks
55  * @tc.level     : Level 2
56  */
mtx_timedlock_0200(void)57 void mtx_timedlock_0200(void)
58 {
59     mtx_t mutex;
60     struct timespec timeout;
61     timeout.tv_sec = TWO;
62     timeout.tv_nsec = 0;
63     int ret = 0;
64     ret = mtx_init(&mutex, mtx_timed);
65     if (ret != thrd_success) {
66         t_error("%s mtx_init failed", __func__);
67         return;
68     }
69     if (ret != thrd_success) {
70         t_error("%s mtx_init failed", __func__);
71         return;
72     }
73     ret = mtx_lock(&mutex);
74     ret = mtx_timedlock(&mutex, &timeout);
75     EXPECT_EQ("mtx_timedlock_0200", ret, thrd_timedout);
76     ret = mtx_unlock(&mutex);
77     if (ret != thrd_success) {
78         t_error("%s mtx_unlock failed", __func__);
79         return;
80     }
81     mtx_destroy(&mutex);
82 }
83 
84 /**
85  * @tc.name      : mtx_timedlock_time64_0100
86  * @tc.desc      : Provide correct parameters, test timeout and lock
87  * @tc.level     : Level 0
88  */
mtx_timedlock_time64_0100(void)89 void mtx_timedlock_time64_0100(void)
90 {
91     struct timespec timeout;
92     timeout.tv_sec = TWO;
93     timeout.tv_nsec = 0;
94     mtx_t mutex;
95     int ret = 0;
96     ret = mtx_init(&mutex, mtx_timed);
97     if (ret != thrd_success) {
98         t_error("%s mtx_init failed", __func__);
99         return;
100     }
101     ret = __mtx_timedlock_time64(&mutex, &timeout);
102     EXPECT_EQ("mtx_timedlock_time64_0100", ret, thrd_success);
103     ret = mtx_unlock(&mutex);
104     if (ret != thrd_success) {
105         t_error("%s mtx_unlock failed", __func__);
106         return;
107     }
108     mtx_destroy(&mutex);
109 }
110 
main(void)111 int main(void)
112 {
113     mtx_timedlock_0100();
114     mtx_timedlock_0200();
115     mtx_timedlock_time64_0100();
116     return t_status;
117 }