1 /* 2 * Copyright (c) 2021-2022 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 BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_INNER_RUNNER_H 17 #define BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_INNER_RUNNER_H 18 19 #include "event_handler_utils.h" 20 #include "event_queue.h" 21 #include "event_runner.h" 22 #include "nocopyable.h" 23 #include "thread_local_data.h" 24 25 #define LOCAL_API __attribute__((visibility ("hidden"))) 26 namespace OHOS { 27 namespace AppExecFwk { 28 class EventInnerRunner { 29 public: 30 explicit EventInnerRunner(const std::shared_ptr<EventRunner> &runner); 31 virtual ~EventInnerRunner() = default; 32 DISALLOW_COPY_AND_MOVE(EventInnerRunner); 33 34 static std::shared_ptr<EventRunner> GetCurrentEventRunner(); 35 36 LOCAL_API virtual void Run() = 0; 37 LOCAL_API virtual void Stop() = 0; 38 GetEventQueue()39 LOCAL_API const std::shared_ptr<EventQueue> &GetEventQueue() const 40 { 41 return queue_; 42 } 43 SetLogger(const std::shared_ptr<Logger> & logger)44 LOCAL_API void SetLogger(const std::shared_ptr<Logger> &logger) 45 { 46 logger_ = logger; 47 } 48 GetThreadName()49 LOCAL_API const std::string &GetThreadName() 50 { 51 return threadName_; 52 } 53 GetThreadId()54 LOCAL_API const std::thread::id &GetThreadId() 55 { 56 return threadId_; 57 } 58 GetKernelThreadId()59 LOCAL_API uint64_t GetKernelThreadId() 60 { 61 return kernelThreadId_; 62 } 63 64 protected: 65 std::shared_ptr<EventQueue> queue_; 66 std::weak_ptr<EventRunner> owner_; 67 std::shared_ptr<Logger> logger_; 68 static ThreadLocalData<std::weak_ptr<EventRunner>> currentEventRunner; 69 std::string threadName_; 70 std::thread::id threadId_; 71 int64_t kernelThreadId_{0}; 72 Mode runningMode_ = Mode::DEFAULT; 73 }; 74 } // namespace AppExecFwk 75 } // namespace OHOS 76 77 #endif // #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EVENT_INNER_RUNNER_H 78