1 // Copyright 2013 The Flutter Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef FLUTTER_FML_MESSAGE_LOOP_IMPL_H_ 6 #define FLUTTER_FML_MESSAGE_LOOP_IMPL_H_ 7 8 #include <atomic> 9 #include <deque> 10 #include <map> 11 #include <mutex> 12 #include <queue> 13 #include <utility> 14 15 #include "flutter/fml/closure.h" 16 #include "flutter/fml/delayed_task.h" 17 #include "flutter/fml/macros.h" 18 #include "flutter/fml/memory/ref_counted.h" 19 #include "flutter/fml/message_loop.h" 20 #include "flutter/fml/message_loop_task_queues.h" 21 #include "flutter/fml/synchronization/thread_annotations.h" 22 #include "flutter/fml/time/time_point.h" 23 #include "flutter/fml/wakeable.h" 24 25 namespace fml { 26 27 class MessageLoopImpl : public Wakeable, 28 public fml::RefCountedThreadSafe<MessageLoopImpl> { 29 public: 30 static fml::RefPtr<MessageLoopImpl> Create(); 31 32 virtual ~MessageLoopImpl(); 33 34 virtual void Run() = 0; 35 36 virtual void Terminate() = 0; 37 38 void PostTask(fml::closure task, fml::TimePoint target_time); 39 40 void AddTaskObserver(intptr_t key, fml::closure callback); 41 42 void RemoveTaskObserver(intptr_t key); 43 44 void DoRun(); 45 46 void DoTerminate(); 47 48 virtual TaskQueueId GetTaskQueueId() const; 49 50 protected: 51 // Exposed for the embedder shell which allows clients to poll for events 52 // instead of dedicating a thread to the message loop. 53 friend class MessageLoop; 54 55 void RunExpiredTasksNow(); 56 57 void RunSingleExpiredTaskNow(); 58 59 protected: 60 MessageLoopImpl(); 61 62 private: 63 fml::RefPtr<MessageLoopTaskQueues> task_queue_; 64 TaskQueueId queue_id_; 65 66 std::atomic_bool terminated_; 67 68 void FlushTasks(FlushType type); 69 70 FML_DISALLOW_COPY_AND_ASSIGN(MessageLoopImpl); 71 }; 72 73 } // namespace fml 74 75 #endif // FLUTTER_FML_MESSAGE_LOOP_IMPL_H_ 76