1 /* 2 * Copyright (c) 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 #ifndef FFRT_SERIAL_LOOPER_H 16 #define FFRT_SERIAL_LOOPER_H 17 18 #include <atomic> 19 #include <memory> 20 #include <string> 21 #include "cpp/task.h" 22 #include "internal_inc/non_copyable.h" 23 #include "serial_queue.h" 24 #include "serial_task.h" 25 26 namespace ffrt { GetSerialTaskByFuncStorageOffset(ffrt_function_header_t * f)27inline SerialTask* GetSerialTaskByFuncStorageOffset(ffrt_function_header_t* f) 28 { 29 return reinterpret_cast<SerialTask*>(static_cast<uintptr_t>(static_cast<size_t>(reinterpret_cast<uintptr_t>(f)) - 30 (reinterpret_cast<size_t>(&((reinterpret_cast<SerialTask*>(0))->func_storage))))); 31 } 32 33 class SerialLooper : public NonCopyable { 34 public: 35 SerialLooper(const char* name, int qos, uint64_t timeout = 0, ffrt_function_header_t* timeoutCb = nullptr); 36 ~SerialLooper(); 37 38 void Quit(); GetQueueId()39 inline uint32_t GetQueueId() const 40 { 41 return qid_; 42 } GetQueueIns()43 inline std::shared_ptr<SerialQueue> GetQueueIns() const 44 { 45 return queue_; 46 } 47 48 private: 49 void Run(); 50 void SetTimeoutMonitor(ITask* task); 51 void RunTimeOutCallback(ITask* task); 52 53 std::string name_; 54 std::atomic_bool isExit_ = {false}; 55 task_handle handle; 56 std::shared_ptr<SerialQueue> queue_; 57 // for timeout watchdog 58 const uint32_t qid_; 59 const uint64_t timeout_; 60 ffrt_function_header_t* timeoutCb_; 61 std::atomic_int delayedCbCnt_ = 0; 62 }; 63 } // namespace ffrt 64 65 #endif // FFRT_SERIAL_LOOPER_H 66