Skip to content

Commit e072a60

Browse files
authored
add more docs (#378)
2 parents 051296f + f453dbc commit e072a60

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

core/docs/en/ordered-work-steal.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Ordered Work Steal Overview
3+
date: 2025-01-17 08:34:00
4+
author: loongs-zhang
5+
---
6+
7+
# Ordered Work Steal Overview
8+
9+
## Why ordered work steal?
10+
11+
In the real world, there are always threads that complete their own tasks first, while other threads have tasks to be
12+
processed. Then, a spectacular scene emerged where one core was in difficulty and other cores were watching.
13+
14+
<div style="text-align: center;">
15+
<img src="../../../docs/img/watching.png" width="50%">
16+
</div>
17+
18+
Obviously, we don't want this to happen. For idle threads, instead of letting them watch other threads working, it's
19+
better to let them help other threads work. In addition, we hope to pop tasks based on priority, the higher the
20+
priority, the earlier it will be popped up.
21+
22+
## What is ordered work steal queue?
23+
24+
An ordered work steal queue consists of a global queue and multiple local queues, the global queue is unbounded, while
25+
the local queue has a bounded SkipList with collections. To ensure high performance, the number of local queues is
26+
usually equal to the number of threads.
27+
28+
## How `push` works
29+
30+
```mermaid
31+
flowchart TD
32+
Cond{Is the local queue full?}
33+
PS[Push to the local queue]
34+
PTG[Push half of the low priority tasks from the local queue to the global queue]
35+
PG[Push to the global queue]
36+
push --> Cond
37+
Cond -- No --> PS
38+
Cond -- Yes --> PTG --- PG
39+
40+
```
41+
42+
## How `pop` works
43+
44+
```mermaid
45+
flowchart TD
46+
Cond1{Are there any tasks in the local queue?}
47+
Cond2{Are there any tasks in other local queues?}
48+
Cond3{Are there any tasks in the global queue?}
49+
PS[Pop the task with the highest priority]
50+
ST[Steal tasks with high priority from other local queues]
51+
PF[Task not found]
52+
pop --> Cond1
53+
Cond1 -- Yes --> PS
54+
Cond1 -- No --> Cond2
55+
Cond2 -- Yes --> ST --> PS
56+
Cond2 -- No --> Cond3
57+
Cond3 -- Yes --> PS
58+
Cond3 -- No --> PF
59+
```

core/docs/en/work-steal.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Work Steal Overview
3+
date: 2025-01-17 08:34:00
4+
author: loongs-zhang
5+
---
6+
7+
# Work Steal Overview
8+
9+
## Why work steal?
10+
11+
In the real world, there are always threads that complete their own tasks first, while other threads have tasks to be
12+
processed. Then, a spectacular scene emerged where one core was in difficulty and other cores were watching.
13+
14+
<div style="text-align: center;">
15+
<img src="../../../docs/img/watching.png" width="50%">
16+
</div>
17+
18+
Obviously, we don't want this to happen. For idle threads, instead of letting them watch other threads working, it's
19+
better to let them help other threads work.
20+
21+
## What is work steal queue?
22+
23+
A work steal queue consists of a global queue and multiple local queues, the global queue is unbounded, while the local
24+
queue has a bounded RingBuffer. To ensure high performance, the number of local queues is usually equal to the number of
25+
threads.
26+
27+
## How `push` works
28+
29+
```mermaid
30+
flowchart TD
31+
Cond{Is the local queue full?}
32+
PS[Push to the local queue]
33+
PTG[Push half of the tasks from the local queue to the global queue]
34+
PG[Push to the global queue]
35+
push --> Cond
36+
Cond -- No --> PS
37+
Cond -- Yes --> PTG --- PG
38+
39+
```
40+
41+
## How `pop` works
42+
43+
```mermaid
44+
flowchart TD
45+
Cond1{Are there any tasks in the local queue?}
46+
Cond2{Are there any tasks in other local queues?}
47+
Cond3{Are there any tasks in the global queue?}
48+
PS[Pop a stack]
49+
ST[Steal tasks from other local queues]
50+
PF[Task not found]
51+
pop --> Cond1
52+
Cond1 -- Yes --> PS
53+
Cond1 -- No --> Cond2
54+
Cond2 -- Yes --> ST --> PS
55+
Cond2 -- No --> Cond3
56+
Cond3 -- Yes --> PS
57+
Cond3 -- No --> PF
58+
```

core/src/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub mod beans;
5151
/// assert_eq!(queue.pop(), None);
5252
/// ```
5353
///
54+
#[doc = include_str!("../../docs/en/work-steal.md")]
5455
pub mod work_steal;
5556

5657
/// Suppose a thread in a work-stealing scheduler is idle and looking for the next task to run. To
@@ -87,6 +88,7 @@ pub mod work_steal;
8788
/// assert_eq!(queue.pop(), None);
8889
/// ```
8990
///
91+
#[doc = include_str!("../../docs/en/ordered-work-steal.md")]
9092
pub mod ordered_work_steal;
9193

9294
#[cfg(target_os = "linux")]

docs/img/watching.png

764 KB
Loading

0 commit comments

Comments
 (0)