Matej Knopp 6818b23a15 Enforce consistent stack size for Flutter threads (flutter/engine#49111)
Fixes https://github.com/flutter/flutter/issues/72156

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## Pre-launch Checklist

- [X] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [X] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [X] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [X] I listed at least one issue that this PR fixes in the description
above.
- [X] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [X] I updated/added relevant documentation (doc comments with `///`).
- [X] I signed the [CLA].
- [X] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
2024-01-08 10:31:02 +01:00

78 lines
1.9 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_THREAD_H_
#define FLUTTER_FML_THREAD_H_
#include <atomic>
#include <functional>
#include <memory>
#include <string>
#include "flutter/fml/macros.h"
#include "flutter/fml/task_runner.h"
namespace fml {
class ThreadHandle;
class Thread {
public:
/// Valid values for priority of Thread.
enum class ThreadPriority : int {
/// Suitable for threads that shouldn't disrupt high priority work.
kBackground,
/// Default priority level.
kNormal,
/// Suitable for threads which generate data for the display.
kDisplay,
/// Suitable for thread which raster data.
kRaster,
};
/// The ThreadConfig is the thread info include thread name, thread priority.
struct ThreadConfig {
ThreadConfig(const std::string& name, ThreadPriority priority)
: name(name), priority(priority) {}
explicit ThreadConfig(const std::string& name)
: ThreadConfig(name, ThreadPriority::kNormal) {}
ThreadConfig() : ThreadConfig("", ThreadPriority::kNormal) {}
std::string name;
ThreadPriority priority;
};
using ThreadConfigSetter = std::function<void(const ThreadConfig&)>;
explicit Thread(const std::string& name = "");
explicit Thread(const ThreadConfigSetter& setter,
const ThreadConfig& config = ThreadConfig());
~Thread();
fml::RefPtr<fml::TaskRunner> GetTaskRunner() const;
void Join();
static void SetCurrentThreadName(const ThreadConfig& config);
static size_t GetDefaultStackSize();
private:
std::unique_ptr<ThreadHandle> thread_;
fml::RefPtr<fml::TaskRunner> task_runner_;
std::atomic_bool joined_;
FML_DISALLOW_COPY_AND_ASSIGN(Thread);
};
} // namespace fml
#endif // FLUTTER_FML_THREAD_H_