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 "time_xcollie.h"
17 
18 #include "time_hilog.h"
19 
20 #ifdef HICOLLIE_ENABLE
21 #include "xcollie/xcollie.h"
22 #endif
23 
24 namespace OHOS {
25 namespace MiscServices {
TimeXCollie(const std::string & name,uint32_t timeoutSeconds,std::function<void (void *)> func,void * arg,uint32_t flag)26 TimeXCollie::TimeXCollie(const std::string &name, uint32_t timeoutSeconds,
27     std::function<void(void *)> func, void *arg, uint32_t flag)
28 {
29     name_ = name;
30 #ifdef HICOLLIE_ENABLE
31     id_ = HiviewDFX::XCollie::GetInstance().SetTimer(name_, timeoutSeconds, func, arg, flag);
32 #else
33     id_ = -1;
34 #endif
35     isCanceled_ = false;
36     TIME_HILOGD(TIME_MODULE_SERVICE, "start TimeXCollie, name:%{public}s,timeout:%{public}u,flag:%{public}u,"
37         "id:%{public}d", name_.c_str(), timeoutSeconds, flag, id_);
38 }
39 
~TimeXCollie()40 TimeXCollie::~TimeXCollie()
41 {
42     CancelTimeXCollie();
43 }
44 
CancelTimeXCollie()45 void TimeXCollie::CancelTimeXCollie()
46 {
47     if (!isCanceled_) {
48 #ifdef HICOLLIE_ENABLE
49         HiviewDFX::XCollie::GetInstance().CancelTimer(id_);
50 #endif
51         isCanceled_ = true;
52         TIME_HILOGD(TIME_MODULE_SERVICE, "cancel TimeXCollie, tag:%{public}s,id:%{public}d", name_.c_str(), id_);
53     }
54 }
55 }
56 }