| 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_TASK_RUNNER_H_ |
| 6 | #define FLUTTER_FML_TASK_RUNNER_H_ |
| 7 | |
| 8 | #include "flutter/fml/closure.h" |
| 9 | #include "flutter/fml/macros.h" |
| 10 | #include "flutter/fml/memory/ref_counted.h" |
| 11 | #include "flutter/fml/memory/ref_ptr.h" |
| 12 | #include "flutter/fml/message_loop_task_queues.h" |
| 13 | #include "flutter/fml/time/time_point.h" |
| 14 | |
| 15 | namespace fml { |
| 16 | |
| 17 | class MessageLoopImpl; |
| 18 | |
| 19 | /// An interface over the ability to schedule tasks on a \p TaskRunner. |
| 20 | class BasicTaskRunner { |
| 21 | public: |
| 22 | /// Schedules \p task to be executed on the TaskRunner's associated event |
| 23 | /// loop. |
| 24 | virtual void PostTask(const fml::closure& task) = 0; |
| 25 | }; |
| 26 | |
| 27 | /// The object for scheduling tasks on a \p fml::MessageLoop. |
| 28 | /// |
| 29 | /// Typically there is one \p TaskRunner associated with each thread. When one |
| 30 | /// wants to execute an operation on that thread they post a task to the |
| 31 | /// TaskRunner. |
| 32 | /// |
| 33 | /// \see fml::MessageLoop |
| 34 | class TaskRunner : public fml::RefCountedThreadSafe<TaskRunner>, |
| 35 | public BasicTaskRunner { |
| 36 | public: |
| 37 | virtual ~TaskRunner(); |
| 38 | |
| 39 | virtual void PostTask(const fml::closure& task) override; |
| 40 | |
| 41 | virtual void PostTaskForTime(const fml::closure& task, |
| 42 | fml::TimePoint target_time); |
| 43 | |
| 44 | /// Schedules a task to be run on the MessageLoop after the time \p delay has |
| 45 | /// passed. |
| 46 | /// \note There is latency between when the task is schedule and actually |
| 47 | /// executed so that the actual execution time is: now + delay + |
| 48 | /// message_loop_latency, where message_loop_latency is undefined and could be |
| 49 | /// tens of milliseconds. |
| 50 | virtual void PostDelayedTask(const fml::closure& task, fml::TimeDelta delay); |
| 51 | |
| 52 | /// Returns \p true when the current executing thread's TaskRunner matches |
| 53 | /// this instance. |
| 54 | virtual bool RunsTasksOnCurrentThread(); |
| 55 | |
| 56 | /// Returns the unique identifier associated with the TaskRunner. |
| 57 | /// \see fml::MessageLoopTaskQueues |
| 58 | virtual TaskQueueId GetTaskQueueId(); |
| 59 | |
| 60 | /// Executes the \p task directly if the TaskRunner \p runner is the |
| 61 | /// TaskRunner associated with the current executing thread. |
| 62 | static void RunNowOrPostTask(const fml::RefPtr<fml::TaskRunner>& runner, |
| 63 | const fml::closure& task); |
| 64 | |
| 65 | protected: |
| 66 | explicit TaskRunner(fml::RefPtr<MessageLoopImpl> loop); |
| 67 | |
| 68 | private: |
| 69 | fml::RefPtr<MessageLoopImpl> loop_; |
| 70 | |
| 71 | FML_FRIEND_MAKE_REF_COUNTED(TaskRunner); |
| 72 | FML_FRIEND_REF_COUNTED_THREAD_SAFE(TaskRunner); |
| 73 | FML_DISALLOW_COPY_AND_ASSIGN(TaskRunner); |
| 74 | }; |
| 75 | |
| 76 | } // namespace fml |
| 77 | |
| 78 | #endif // FLUTTER_FML_TASK_RUNNER_H_ |
| 79 | |