1 /** 2 * Copyright (c) 2022-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 PANDA_TOOLING_INSPECTOR_CONNECTION_EVENT_LOOP_H 17 #define PANDA_TOOLING_INSPECTOR_CONNECTION_EVENT_LOOP_H 18 19 #include <atomic> 20 21 #include "os/mutex.h" 22 23 namespace ark::tooling::inspector { 24 class EventLoop { 25 public: 26 // Notify the running event loop to stop. 27 bool Kill(); 28 29 // Run the event loop until paused. 30 void Run(const std::string& msg); 31 32 // Run at most one event loop handler, may block. 33 virtual bool ParseMessage(const std::string& msg) = 0; 34 35 // Pause the event loop. Wait for the current task to finish. Pause()36 void Pause() ACQUIRE_SHARED(taskExecution_) 37 { 38 taskExecution_.ReadLock(); 39 } 40 41 // Notify the event loop to continue. Continue()42 void Continue() RELEASE_GENERIC(taskExecution_) 43 { 44 taskExecution_.Unlock(); 45 } 46 47 private: 48 std::atomic<bool> running_ {false}; 49 os::memory::RWLock taskExecution_; 50 }; 51 } // namespace ark::tooling::inspector 52 53 #endif // PANDA_TOOLING_INSPECTOR_CONNECTION_EVENT_LOOP_H 54