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