1 /* 2 * Copyright (c) 2023-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 #ifndef OHOS_DEFERRED_PROCESSING_SERVICE_TIME_BROKER_H 17 #define OHOS_DEFERRED_PROCESSING_SERVICE_TIME_BROKER_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <queue> 25 #include <string> 26 #include <vector> 27 #include "timer.h" 28 29 namespace OHOS { 30 namespace CameraStandard { 31 namespace DeferredProcessing { 32 class TimeBroker : public std::enable_shared_from_this<TimeBroker> { 33 public: 34 static std::shared_ptr<TimeBroker> Create(std::string name); 35 explicit TimeBroker(std::string name); 36 ~TimeBroker(); 37 void Initialize(); 38 bool RegisterCallback(uint32_t delayTimeMs, std::function<void(uint32_t handle)> timerCallback, uint32_t& handle); 39 void DeregisterCallback(uint32_t handle); 40 std::function<void(uint32_t handle)> GetExpiredFunc(uint32_t handle); 41 private: 42 struct TimerInfo { TimerInfoTimerInfo43 TimerInfo(uint32_t handle, uint64_t timestamp, std::function<void(uint32_t handle)> callback) 44 : handle(handle), timestamp(timestamp), timerCallback(std::move(callback)) 45 { 46 } 47 uint32_t handle; 48 uint64_t timestamp; 49 std::function<void(uint32_t handle)> timerCallback; 50 }; 51 bool GetNextHandle(uint32_t& handle); 52 uint32_t GenerateHandle(); 53 void TimerExpired(); 54 bool RestartTimer(bool force = false); 55 56 const std::string name_; 57 std::mutex mutex_; 58 std::shared_ptr<Timer> timer_; 59 std::priority_queue<uint64_t, std::vector<uint64_t>, std::greater<uint64_t>> timeline_; 60 std::map<uint32_t, std::shared_ptr<TimerInfo>> timerInfos_; 61 std::map<uint64_t, std::vector<std::weak_ptr<TimerInfo>>> expiringTimers_; 62 uint32_t preHandle_{0}; 63 }; 64 } //namespace DeferredProcessing 65 } // namespace CameraStandard 66 } // namespace OHOS 67 #endif // OHOS_DEFERRED_PROCESSING_SERVICE_TIME_BROKER_H 68