-
-
Notifications
You must be signed in to change notification settings - Fork 822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translation Chapter 4 #171
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 教程 04 - 全局安全 | ||
|
||
## tl;dr | ||
|
||
- 引入了假的锁。 | ||
- 这是第一次展示原始操作系统同步,并支持安全访问全局数据结构。 | ||
|
||
## Rust中的全局可变 | ||
|
||
当我们引入全局可用的`print!`宏在 [教程03],我门有一点作弊。 调用 | ||
`core::fmt`的`write_fmt()`函数,接受`&mut self`的方法之所以有效, | ||
是因为在每次调用时都会创建一个新的`QEMUOutput`实例。 | ||
|
||
如果我们想保留一些状态,例如关于写入字符数的统计数据, | ||
我们需要创建`QEMUOutput`的一个全局实例 (在Rust中,使用`static`关键字). | ||
|
||
然而`static QEMU_OUTPUT`不允许调用具有`&mut self`的函数。 | ||
为此,我们需要`static mut`,但是调用改变`static mut`状态的函数是不安全的。 | ||
这个是Rust编译器对此的推理,它无法再阻止核心/线程同时改变数据(它是全局的,所以每个人都可以从任何地方引用它,检查程序借用在这里帮不上忙)。 | ||
|
||
|
||
这个问题的解决方案是将全局封装到原始同步中。在我们的例子中,是一个*MUTual EXclusion*原语的变体。 | ||
`Mutex`是`synchronization.rs`中引入的一个特性,并由同一文件中的`NullLock`实现。 | ||
为了使代码更易于教学,它省略了用于防止并发访问的实际体系结构特定逻辑,因为只要内核仅在单个内核上执行并禁用中断,我们就不需要它。 | ||
|
||
`NullLock`侧重于展示Rust内部可变性的核心概念。请务必阅读它。 | ||
我们还建议您阅读这篇关于[Rust的引用类型的精确心智模型]文章 | ||
|
||
如果要将`NullLock`与一些真实的互斥实现进行比较,可以查看 | ||
[spin crate]或者[parking lot crate]。 | ||
|
||
[教程03]: ../03_hacky_hello_world | ||
[内部可变性]: https://doc.rust-lang.org/std/cell/index.html | ||
[Rust的引用类型的精确心智模型]: https://docs.rs/dtolnay/0.0.6/dtolnay/macro._02__reference_types.html | ||
[spin crate]: https://github.com/mvdnes/spin-rs | ||
[parking lot crate]: https://github.com/Amanieu/parking_lot | ||
|
||
## 测试 | ||
|
||
```console | ||
$ make qemu | ||
[...] | ||
|
||
[0] Hello from Rust! | ||
[1] Chars written: 22 | ||
[2] Stopping here. | ||
``` | ||
|
||
## 相比之前的变化(diff) | ||
请检查[英文版本](README.md#diff-to-previous),这是最新的。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原文用的是并发地改变数据而非同时,应该还是有区别的。另外borrow checker 似乎直译成借用检查器比较常见?