3 个不稳定版本

0.1.0 2024年4月9日
0.0.2 2023年3月27日
0.0.1 2023年3月26日

#296编程语言


smart_ir 中使用

Apache-2.0 OR MIT

58KB
1K SLoC

[WIP] compiler_base_parallel

摘要

compiler_base_parallel 定义了多任务执行引擎的核心逻辑。它的目标是提供可重用的组件,并为编译器开发积累一些通用的并发模型。

compiler_base_parallel 包由三个主要组件组成:Task、Executor 和 Reporter。

Task

Task 是最小的可执行单元,任何东西都可以被视为一个 Task,它可以由 Executor 执行。因此,我们提供了一个特质来定义一个 Task

pub trait Task {
    /// [`run`] will be executed of the [[`Task`](./src/task/mod.rs)],
    /// and the result of the execution is communicated with other threads through the [`ch`] which is a [`Sender<FinishedTask>`],
    /// so [`run`] method does not need to return a value.
    fn run(&self, ch: Sender<FinishedTask>);

    /// Return the [`TaskInfo`]
    fn info(&self) -> TaskInfo;
}

要在 compiler_base_parallel 中开发编译器的并发机制,第一步是创建一个 Task

有关 Task 的更多信息,请参阅源代码中的文档 ./src/task/mod.rs

Executor

Executor 负责执行 Task

我们还提供了一个 trait 来定义 Executor

pub trait Executor {
    /// [`run_all_tasks`] will execute all tasks concurrently.
    /// [`notify_what_happened`] is a notifier that receives [`TaskEvent`] to output the [[`Task`](./src/task/mod.rs)] execution status in to the log.
    fn run_all_tasks<T, F>(self, tasks: Vec<T>, notify_what_happened: F) -> Result<()>
    where
        T: Task + Sync + Send + 'static,
        F: Fn(TaskEvent) -> Result<()>;

    /// The count for threads.
    fn concurrency_capacity(&self) -> usize;
}

有关 Executor 的更多信息,请参阅源代码中的文档 ./src/executor/mod.rs

TimeoutExecutor

TimeoutExecutor 指的是 rustc 在 rust 单元测试中采用的并发机制,主要包含以下功能:

  • 任务根据线程数量并发执行。

  • 使用超时队列来监控 Task 的执行是否超时。如果是,则会报告警告,但 Task 不会停止,将继续运行直到手动中断。

如果您想在编译器中实现与 rustc 测试相同的流程并行执行单元测试、模糊测试、基准测试或其他操作,可以使用 TimeoutExecutor。如果此流程不适合您的编译器,可以选择实现自己的 Executor

[WIP] Reporter

依赖关系

~3–11MB
~102K SLoC