flutter_flutter/fml/message_loop_task_queue.h
Kaushik Iska 6f5347c5d6
MessageLoopTaskQueue schedules Wakes (#9316)
* Refactor to move Task Queue to its own class

- This is to help with sharing task queue among
  multiple message loops going forward.

- currently there is 1:1 mapping between task queue
  and message loop, we are still maintaining the semantics
  for this change.

* Add mutex include

* Most of the waking up changes minus test failures

* Refactor MessageLoopImpl to be Wakeable

- Makes testing easier by letting us putting a TestWakeable

- Also move the waking up logic to the task queue

* add tests

* Fix formatting and license
2019-06-13 17:44:44 -07:00

82 lines
1.8 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_FML_MESSAGE_LOOP_TASK_QUEUE_H_
#define FLUTTER_FML_MESSAGE_LOOP_TASK_QUEUE_H_
#include <map>
#include <mutex>
#include <vector>
#include "flutter/fml/closure.h"
#include "flutter/fml/delayed_task.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/fml/synchronization/thread_annotations.h"
#include "flutter/fml/wakeable.h"
namespace fml {
enum class FlushType {
kSingle,
kAll,
};
// This class keeps track of all the tasks and observers that
// need to be run on it's MessageLoopImpl. This also wakes up the
// loop at the required times.
class MessageLoopTaskQueue {
public:
// Lifecycle.
MessageLoopTaskQueue();
~MessageLoopTaskQueue();
void Dispose();
// Tasks methods.
void RegisterTask(fml::closure task, fml::TimePoint target_time);
bool HasPendingTasks();
void GetTasksToRunNow(FlushType type, std::vector<fml::closure>& invocations);
size_t GetNumPendingTasks();
// Observers methods.
void AddTaskObserver(intptr_t key, fml::closure callback);
void RemoveTaskObserver(intptr_t key);
void NotifyObservers();
// Misc.
void Swap(MessageLoopTaskQueue& other);
void SetWakeable(fml::Wakeable* wakeable);
private:
void WakeUp(fml::TimePoint time);
Wakeable* wakeable_ = NULL;
std::mutex observers_mutex_;
std::map<intptr_t, fml::closure> task_observers_
FML_GUARDED_BY(observers_mutex_);
std::mutex delayed_tasks_mutex_;
DelayedTaskQueue delayed_tasks_ FML_GUARDED_BY(delayed_tasks_mutex_);
size_t order_ FML_GUARDED_BY(delayed_tasks_mutex_);
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(MessageLoopTaskQueue);
};
} // namespace fml
#endif // FLUTTER_FML_MESSAGE_LOOP_TASK_QUEUE_H_