1 /* 2 * Copyright (c) 2020-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 "hal_tick.h" 17 18 #include <cstdint> 19 20 #ifdef _WIN32 21 #include <windows.h> 22 #elif defined __linux__ || defined __LITEOS__ || defined __APPLE__ 23 #include <ctime> 24 #else 25 #include "los_tick.h" 26 #endif 27 28 namespace OHOS { GetInstance()29HALTick& HALTick::GetInstance() 30 { 31 static HALTick instance; 32 return instance; 33 } 34 GetTime()35uint32_t HALTick::GetTime() 36 { 37 #ifdef _WIN32 38 return GetTickCount(); 39 #elif defined __linux__ || defined __LITEOS__ || defined __APPLE__ 40 struct timespec time; 41 clock_gettime(CLOCK_MONOTONIC, &time); 42 return time.tv_sec * SEC_TO_MILLISEC + time.tv_nsec / MILLISEC_TO_NANOSEC; 43 #else 44 return LOS_TickCountGet(); 45 #endif 46 } 47 GetElapseTime(uint32_t startTime)48uint32_t HALTick::GetElapseTime(uint32_t startTime) 49 { 50 uint32_t currentTime = GetTime(); 51 uint32_t elapseTime; 52 53 if (currentTime > startTime) { 54 elapseTime = currentTime - startTime; 55 } else { 56 elapseTime = (UINT32_MAX - startTime) + currentTime + 1; 57 } 58 59 return elapseTime; 60 } 61 } // namespace OHOS 62