2022-07-27 11:17:11 +08:00

36 lines
1.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 锁的类型及其规则
## 简介
  DragonOS内核实现了一些锁大致可以分为两类
- 休眠锁
- 自旋锁
## 锁的类型
### 休眠锁
  休眠锁只能在可抢占的上下文之中被获取。
  在DragonOS之中实现了以下的休眠锁
- semaphore
### 自旋锁
- spinlock_t
  进程在获取自旋锁后将改变pcb中的锁变量持有计数从而隐式地禁止了抢占。为了获得更多灵活的操作spinlock还提供了以下的方法
| 后缀 | 说明 |
| ------------------------ | -------------------------- |
| _irq() | 在加锁时关闭中断/在放锁时开启中断 |
| _irqsave()/_irqrestore() | 在加锁时保存中断状态,并关中断/在放锁时恢复中断状态 |
### semaphore信号量
  semaphore信号量是基于计数实现的。
  当可用资源不足时尝试对semaphore执行down操作的进程将会被休眠直到资源可用。