1 /*
2 * Copyright (c) 2021 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 "xcollie_checker_test.h"
17
18 #include <future>
19 #include <thread>
20
21 namespace OHOS {
22 namespace HiviewDFX {
XCollieCheckerTest(const std::string name)23 XCollieCheckerTest::XCollieCheckerTest(const std::string name)
24 : XCollieChecker(name),
25 timeout_(0),
26 testLockNumber_(0),
27 testThreadNumber_(0),
28 threadEnd_(false),
29 thread_(nullptr)
30 {
31 }
32
~XCollieCheckerTest()33 XCollieCheckerTest::~XCollieCheckerTest()
34 {
35 if (thread_ != nullptr && thread_->joinable()) {
36 thread_->join();
37 }
38 thread_ = nullptr;
39 }
40
CheckLock()41 void XCollieCheckerTest::CheckLock()
42 {
43 testLockNumber_++;
44 if (timeout_ == 0) {
45 return;
46 }
47 std::this_thread::sleep_for(std::chrono::seconds(timeout_));
48 return;
49 }
50
CheckThreadBlock()51 void XCollieCheckerTest::CheckThreadBlock()
52 {
53 testThreadNumber_++;
54 if (timeout_ == 0) {
55 SetThreadBlockResult(true);
56 return;
57 }
58 if (threadEnd_) {
59 return;
60 }
61 thread_ = std::make_unique<std::thread>(&XCollieCheckerTest::SetThreadBlockComplete, this);
62 threadEnd_ = true;
63 }
64
SetThreadBlockComplete()65 void XCollieCheckerTest::SetThreadBlockComplete()
66 {
67 std::this_thread::sleep_for(std::chrono::seconds(timeout_));
68 SetThreadBlockResult(true);
69 threadEnd_ = false;
70 return;
71 }
72 } // end of namespace HiviewDFX
73 } // end of namespace OHOS
74