forked from cdfmlr/sham
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.go
46 lines (39 loc) · 835 Bytes
/
cpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package sham
import (
"context"
"sync"
)
// CPU 处理器:是一个模拟的「CPU」。
// CPU 在某一时刻只能跑一个东西,所以有个锁
// 其中的 Thread 指向正在运行的「线程」
type CPU struct {
Id string
sync.Mutex
Thread *Thread
Done chan int
Blocked chan string
cancel context.CancelFunc
Clock uint
}
// Run 让 CPU 运行任务
func (c *CPU) Run() {
c.Done, c.cancel = c.Thread.Run()
}
// Cancel 取消 CPU 当前的任务
func (c *CPU) Cancel(status int) {
if c.cancel != nil {
if c.Thread.contextual.Process.Status == StatusRunning {
c.Thread.contextual.Process.Status = status
}
c.cancel()
}
c.Thread = nil
c.Done = nil
c.cancel = nil
}
// Switch 切换 CPU 任务
func (c *CPU) Switch(newThread *Thread) {
c.Cancel(StatusReady)
c.Thread = newThread
c.Run()
}