1 /* 2 * Copyright (c) 2025 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_ROSEN_WINDOW_SCENE_TIMEOUT_FUTURE_H 17 #define OHOS_ROSEN_WINDOW_SCENE_TIMEOUT_FUTURE_H 18 19 #include <condition_variable> 20 21 namespace OHOS::Rosen { 22 template<class T> 23 class TimeoutFuture { 24 public: GetResult(long timeout,bool & isTimeout)25 T GetResult(long timeout, bool &isTimeout) 26 { 27 std::unique_lock<std::mutex> lock(mutex_); 28 isTimeout = !conditionVariable_.wait_for(lock, 29 std::chrono::milliseconds(timeout), [this] { return IsReady(); }); 30 return result_; 31 } 32 FutureCall(T t)33 void FutureCall(T t) 34 { 35 std::unique_lock<std::mutex> lock(mutex_); 36 result_ = t; 37 isReady_ = true; 38 conditionVariable_.notify_one(); 39 } 40 41 private: IsReady()42 inline bool IsReady() { return isReady_; } 43 44 std::condition_variable conditionVariable_; 45 std::mutex mutex_; 46 bool isReady_ = false; 47 T result_; 48 }; 49 } // namespace OHOS::Rosen 50 51 #endif // OHOS_ROSEN_WINDOW_SCENE_TIMEOUT_FUTURE_H 52