1 /*
2 * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include <chrono>
18 #include "c/thread.h"
19 #include "ffrt_inner.h"
20 #include "../common.h"
21
22 using namespace ffrt;
23 using namespace std;
24 using namespace testing;
25 #ifdef HWTEST_TESTING_EXT_ENABLE
26 using namespace testing::ext;
27 #endif
28
29 class CVTest : public testing::Test {
30 protected:
SetUpTestCase()31 static void SetUpTestCase()
32 {
33 }
34
TearDownTestCase()35 static void TearDownTestCase()
36 {
37 }
38
SetUp()39 void SetUp() override
40 {
41 }
42
TearDown()43 void TearDown() override
44 {
45 }
46 };
47
48 HWTEST_F(CVTest, conditonV_wait_for_test2, TestSize.Level0)
49 {
50 ffrt::condition_variable cond;
51 ffrt::mutex lock_;
52 ffrt::cv_status status;
53
54 ffrt::submit(
__anon4f7898280102() 55 [&]() {
56 std::unique_lock lck(lock_);
57 status = cond.wait_for(lck, 100ms);
58 EXPECT_EQ(status, ffrt::cv_status::timeout);
59 },
__anon4f7898280202() 60 {}, {});
61
62 ffrt::wait();
63 }
64
65 HWTEST_F(CVTest, conditonV_wait_for_test3, TestSize.Level0)
66 {
67 ffrt::condition_variable cond;
68 ffrt::mutex lock_;
69 ffrt::cv_status status;
70
71 std::unique_lock lck(lock_);
72 status = cond.wait_for(lck, 100ms);
73 EXPECT_EQ(status, ffrt::cv_status::timeout);
74 }
75
76 HWTEST_F(CVTest, conditonV_nullptr_test, TestSize.Level0)
77 {
78 int ret = 0;
79
80 ret = ffrt_cond_init(nullptr, nullptr);
81 EXPECT_NE(ret, 0);
82 ret = ffrt_cond_signal(nullptr);
83 EXPECT_NE(ret, 0);
84 ret = ffrt_cond_broadcast(nullptr);
85 EXPECT_NE(ret, 0);
86 ret = ffrt_cond_wait(nullptr, nullptr);
87 EXPECT_NE(ret, 0);
88 ret = ffrt_cond_timedwait(nullptr, nullptr, nullptr);
89 EXPECT_NE(ret, 0);
90 ffrt_cond_destroy(nullptr);
91 }
92
93 class MutexTest : public testing::Test {
94 protected:
SetUpTestCase()95 static void SetUpTestCase()
96 {
97 }
98
TearDownTestCase()99 static void TearDownTestCase()
100 {
101 }
102
SetUp()103 void SetUp() override
104 {
105 }
106
TearDown()107 void TearDown() override
108 {
109 }
110 };
111
112 HWTEST_F(MutexTest, try_lock_test, TestSize.Level0)
113 {
114 int val = -1;
115 ffrt::mutex lock;
116 lock.lock();
117 val = lock.try_lock();
118 EXPECT_EQ(val, 0);
119 lock.unlock();
120 val = lock.try_lock();
121 EXPECT_EQ(val, 1);
122 lock.unlock();
123 lock.unlock();
124 }
125
126 HWTEST_F(MutexTest, lock_stress_test, TestSize.Level0)
127 {
128 // trigger lazy init
__anon4f7898280402() 129 ffrt::submit([&]() {}, {}, {});
130 ffrt::wait();
131
132 const int N = 10;
133 const int M = 10;
134 const int J = 10;
135 ffrt::mutex lock;
136 // std::mutex lock;
137 int acc = 0;
138 for (int i = 0; i < N; ++i) {
139 ffrt::submit(
__anon4f7898280702() 140 [&]() {
141 for (int j = 0; j < M; ++j) {
142 lock.lock();
143 acc++;
144 lock.unlock();
145 }
146 },
__anon4f7898280802() 147 {}, {});
148 }
149
150 for (int j = 0; j < J; ++j) {
151 lock.lock();
152 acc++;
153 lock.unlock();
154 }
155
156 ffrt::wait();
157 EXPECT_EQ(acc, (M * N + J));
158 }
159
160 class SleepTest : public testing::Test {
161 protected:
SetUpTestCase()162 static void SetUpTestCase()
163 {
164 }
165
TearDownTestCase()166 static void TearDownTestCase()
167 {
168 }
169
SetUp()170 void SetUp() override
171 {
172 }
173
TearDown()174 void TearDown() override
175 {
176 }
177 };
178
thd_func(void * arg)179 void* thd_func(void *arg)
180 {
181 int *counter = (int *)arg;
182 (*counter)++;
183 return arg;
184 }
185
186 int g_data = 0;
187
188 HWTEST_F(SleepTest, thread_test, TestSize.Level0)
189 {
190 ffrt_thread_t detachThread;
191 ffrt_thread_create(&detachThread, nullptr, thd_func, &g_data);
192 ffrt_thread_detach(detachThread);
193
194 int a = 0;
195 ffrt_thread_t thread;
196 ffrt_thread_create(&thread, nullptr, thd_func, &a);
197 void* result = nullptr;
198 ffrt_thread_join(thread, &result);
199 EXPECT_EQ(1, a);
200 EXPECT_EQ(&a, result);
201 }
202
203 HWTEST_F(SleepTest, thread_test2, TestSize.Level0)
204 {
205 int a = 0;
206 ffrt_thread_t thread;
207 ffrt_thread_attr_t attr;
208 ffrt_thread_create(nullptr, nullptr, thd_func, &a);
209 EXPECT_EQ(0, a);
210 EXPECT_EQ(ffrt_thread_create(&thread, nullptr, nullptr, &a), ffrt_error_inval);
211 EXPECT_EQ(ffrt_thread_create(&thread, &attr, thd_func, &a), ffrt_error);
212
213 void* result = nullptr;
214 EXPECT_EQ(ffrt_thread_join(nullptr, &result), ffrt_error_inval);
215 EXPECT_EQ(ffrt_thread_join(thread, nullptr), ffrt_error_inval);
216 EXPECT_EQ(ffrt_thread_detach(nullptr), ffrt_error_inval);
217 }
218