Files
engine/fml/message_loop.h
T
Kaushik Iska 632a37b5d5 Make message loop task entry containers thread safe (#11367)
The core underlying issue is that vector push_back could re-allocate and cause us to segfault. I have switched the backing queues to a map per @jason-simmons suggestion in flutter/flutter#38778.

I've also added a test to capture the aforementioned bug. I've run internal tests several times to validate that this is fixed.

General threading note for this class is that only the following operations take a write lock on the meta mutex:

1. Create
2. Dispose

The rest of the operations take read lock on the meta mutex and acquire finer grained locks for the duration of the operation. We can not grab read lock for the entire duration of NotifyObservers for example because observer can in-turn create other queues -- Which we should not block.

Additional changes:

1. Make as many methods as possible const. Unlocked methods are all const.
2. Migrate all the queue members to a struct, and have a map.
3. Get rid of the un-used Swap functionality.
2019-08-22 23:27:25 -07:00

62 lines
1.3 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_H_
#define FLUTTER_FML_MESSAGE_LOOP_H_
#include "flutter/fml/macros.h"
#include "flutter/fml/task_runner.h"
namespace fml {
class TaskRunner;
class MessageLoopImpl;
class MessageLoop {
public:
FML_EMBEDDER_ONLY
static MessageLoop& GetCurrent();
bool IsValid() const;
void Run();
void Terminate();
void AddTaskObserver(intptr_t key, fml::closure callback);
void RemoveTaskObserver(intptr_t key);
fml::RefPtr<fml::TaskRunner> GetTaskRunner() const;
// Exposed for the embedder shell which allows clients to poll for events
// instead of dedicating a thread to the message loop.
void RunExpiredTasksNow();
static void EnsureInitializedForCurrentThread();
static bool IsInitializedForCurrentThread();
~MessageLoop();
static TaskQueueId GetCurrentTaskQueueId();
private:
friend class TaskRunner;
friend class MessageLoopImpl;
fml::RefPtr<MessageLoopImpl> loop_;
fml::RefPtr<fml::TaskRunner> task_runner_;
MessageLoop();
fml::RefPtr<MessageLoopImpl> GetLoopImpl() const;
FML_DISALLOW_COPY_AND_ASSIGN(MessageLoop);
};
} // namespace fml
#endif // FLUTTER_FML_MESSAGE_LOOP_H_