| 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/time/time_point.h" |
| 22 | #include "flutter/fml/wakeable.h" |
| 23 | |
| 24 | namespace fml { |
| 25 | |
| 26 | /// An abstract class that represents the differences in implementation of a \p |
| 27 | /// fml::MessageLoop depending on the platform. |
| 28 | /// \see fml::MessageLoop |
| 29 | /// \see fml::MessageLoopAndroid |
| 30 | /// \see fml::MessageLoopDarwin |
| 31 | class MessageLoopImpl : public Wakeable, |
| 32 | public fml::RefCountedThreadSafe<MessageLoopImpl> { |
| 33 | public: |
| 34 | static fml::RefPtr<MessageLoopImpl> Create(); |
| 35 | |
| 36 | virtual ~MessageLoopImpl(); |
| 37 | |
| 38 | virtual void Run() = 0; |
| 39 | |
| 40 | virtual void Terminate() = 0; |
| 41 | |
| 42 | void PostTask(const fml::closure& task, fml::TimePoint target_time); |
| 43 | |
| 44 | void AddTaskObserver(intptr_t key, const fml::closure& callback); |
| 45 | |
| 46 | void RemoveTaskObserver(intptr_t key); |
| 47 | |
| 48 | void DoRun(); |
| 49 | |
| 50 | void DoTerminate(); |
| 51 | |
| 52 | virtual TaskQueueId GetTaskQueueId() const; |
| 53 | |
| 54 | protected: |
| 55 | // Exposed for the embedder shell which allows clients to poll for events |
| 56 | // instead of dedicating a thread to the message loop. |
| 57 | friend class MessageLoop; |
| 58 | |
| 59 | void RunExpiredTasksNow(); |
| 60 | |
| 61 | void RunSingleExpiredTaskNow(); |
| 62 | |
| 63 | protected: |
| 64 | MessageLoopImpl(); |
| 65 | |
| 66 | private: |
| 67 | fml::MessageLoopTaskQueues* task_queue_; |
| 68 | TaskQueueId queue_id_; |
| 69 | |
| 70 | std::atomic_bool terminated_; |
| 71 | |
| 72 | void FlushTasks(FlushType type); |
| 73 | |
| 74 | FML_DISALLOW_COPY_AND_ASSIGN(MessageLoopImpl); |
| 75 | }; |
| 76 | |
| 77 | } // namespace fml |
| 78 | |
| 79 | #endif // FLUTTER_FML_MESSAGE_LOOP_IMPL_H_ |
| 80 |
