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 #ifndef RENDER_SERVICE_BASE_ADAPTER_GENERIC_RS_THREAD_LOOPER_IMPL_H 17 #define RENDER_SERVICE_BASE_ADAPTER_GENERIC_RS_THREAD_LOOPER_IMPL_H 18 19 #include <condition_variable> 20 #include <deque> 21 #include <mutex> 22 #include <set> 23 24 namespace OHOS { 25 namespace Rosen { 26 class ThreadLooperMessage { 27 public: 28 ThreadLooperMessage() = default; 29 virtual ~ThreadLooperMessage() = default; 30 virtual void Process(int param) const = 0; 31 }; 32 33 class ThreadLooperImpl final { 34 public: 35 #ifdef ROSEN_USE_SYSTEM_CLOCK 36 using clock_t = std::chrono::system_clock; 37 #else 38 using clock_t = std::chrono::steady_clock; 39 #endif 40 ThreadLooperImpl() = default; 41 ~ThreadLooperImpl() = default; 42 43 static ThreadLooperImpl* CreateThreadInstance(); 44 static ThreadLooperImpl* GetThreadInstance(); 45 void ProcessOneMessage(int timeoutMillis); 46 void ProcessAllMessages(int timeoutMillis); 47 void PostMessage(const std::shared_ptr<ThreadLooperMessage>& message, int param); 48 void PostMessage(int64_t delay, const std::shared_ptr<ThreadLooperMessage>& message, int param); 49 void RemoveMessages(const std::shared_ptr<ThreadLooperMessage>& message); 50 void WakeUp(); 51 52 private: 53 using time_t = std::chrono::time_point<clock_t>; 54 using message_t = std::shared_ptr<ThreadLooperMessage>; 55 using queue_element_t = std::tuple<message_t, int>; 56 using delayed_queue_element_t = std::tuple<time_t, message_t, int>; 57 58 class Comp final { 59 public: operator()60 bool operator()(const delayed_queue_element_t& a, const delayed_queue_element_t& b) const 61 { 62 if (std::get<time_t>(a) != std::get<time_t>(b)) { 63 return std::get<time_t>(a) < std::get<time_t>(b); 64 } 65 // If we actually have same timestamp, that's unlikely, but still, then just comapre pointers of handlers 66 return std::get<message_t>(a).get() < std::get<message_t>(b).get(); 67 } 68 }; 69 70 bool HaveDelayedMessageToProcess(); 71 void WaitForMessage(int timeoutMillis); 72 bool ProcessOneMessageInternal(); 73 74 std::mutex mutex_; 75 std::condition_variable cv_; 76 // Set is used as ordered queue, since we can't have 2 exactly same elements 77 std::set<delayed_queue_element_t, Comp> delayedQueue_; 78 std::deque<queue_element_t> queue_; 79 bool wakeup_ = false; 80 }; 81 } // namespace Rosen 82 } // namespace OHOS 83 84 #endif // RENDER_SERVICE_BASE_ADAPTER_GENERIC_RS_THREAD_LOOPER_IMPL_H 85