mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-09 07:06:47 +00:00
doc: mutex文档
This commit is contained in:
parent
946bbef392
commit
311a6181b5
@ -16,6 +16,7 @@
|
|||||||
  在DragonOS之中,实现了以下的休眠锁:
|
  在DragonOS之中,实现了以下的休眠锁:
|
||||||
|
|
||||||
- semaphore
|
- semaphore
|
||||||
|
- mutex_t
|
||||||
|
|
||||||
### 自旋锁
|
### 自旋锁
|
||||||
|
|
||||||
@ -29,8 +30,69 @@
|
|||||||
| _irqsave()/_irqrestore() | 在加锁时保存中断状态,并关中断/在放锁时恢复中断状态 |
|
| _irqsave()/_irqrestore() | 在加锁时保存中断状态,并关中断/在放锁时恢复中断状态 |
|
||||||
|
|
||||||
|
|
||||||
|
## 详细介绍
|
||||||
### semaphore信号量
|
### semaphore信号量
|
||||||
|
|
||||||
  semaphore信号量是基于计数实现的。
|
  semaphore信号量是基于计数实现的。
|
||||||
|
|
||||||
  当可用资源不足时,尝试对semaphore执行down操作的进程将会被休眠,直到资源可用。
|
  当可用资源不足时,尝试对semaphore执行down操作的进程将会被休眠,直到资源可用。
|
||||||
|
|
||||||
|
### mutex互斥量
|
||||||
|
|
||||||
|
  mutex是一种轻量级的同步原语,只有0和1两种状态。
|
||||||
|
|
||||||
|
  当mutex被占用时,尝试对mutex进行加锁操作的进程将会被休眠,直到资源可用。
|
||||||
|
|
||||||
|
#### 特性
|
||||||
|
|
||||||
|
- 同一时间只有1个任务可以持有mutex
|
||||||
|
- 不允许递归地加锁、解锁
|
||||||
|
- 只允许通过mutex的api来操作mutex
|
||||||
|
- 在硬中断、软中断中不能使用mutex
|
||||||
|
|
||||||
|
#### 数据结构
|
||||||
|
|
||||||
|
  mutex定义在`common/mutex.h`中。其数据类型如下所示:
|
||||||
|
|
||||||
|
```c
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
|
||||||
|
atomic_t count; // 锁计数。1->已解锁。 0->已上锁,且有可能存在等待者
|
||||||
|
spinlock_t wait_lock; // mutex操作锁,用于对mutex的list的操作进行加锁
|
||||||
|
struct List wait_list; // Mutex的等待队列
|
||||||
|
} mutex_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### API
|
||||||
|
|
||||||
|
##### mutex_init
|
||||||
|
|
||||||
|
**`void mutex_init(mutex_t *lock)`**
|
||||||
|
|
||||||
|
  初始化一个mutex对象。
|
||||||
|
|
||||||
|
##### mutex_lock
|
||||||
|
|
||||||
|
**`void mutex_lock(mutex_t *lock)`**
|
||||||
|
|
||||||
|
  对一个mutex对象加锁。若mutex当前被其他进程持有,则当前进程进入休眠状态。
|
||||||
|
|
||||||
|
##### mutex_unlock
|
||||||
|
|
||||||
|
**`void mutex_unlock(mutex_t *lock)`**
|
||||||
|
|
||||||
|
  对一个mutex对象解锁。若mutex的等待队列中有其他的进程,则唤醒下一个进程。
|
||||||
|
|
||||||
|
##### mutex_trylock
|
||||||
|
|
||||||
|
**`void mutex_trylock(mutex_t *lock)`**
|
||||||
|
|
||||||
|
  尝试对一个mutex对象加锁。若mutex当前被其他进程持有,则返回0.否则,加锁成功,返回1.
|
||||||
|
|
||||||
|
##### mutex_is_locked
|
||||||
|
|
||||||
|
**`void mutex_is_locked(mutex_t *lock)`**
|
||||||
|
|
||||||
|
  判断mutex是否已被加锁。若给定的mutex已处于上锁状态,则返回1,否则返回0。
|
||||||
|
|
||||||
|
@ -17,8 +17,8 @@ typedef struct
|
|||||||
{
|
{
|
||||||
|
|
||||||
atomic_t count; // 锁计数。1->已解锁。 0->已上锁,且有可能存在等待者
|
atomic_t count; // 锁计数。1->已解锁。 0->已上锁,且有可能存在等待者
|
||||||
spinlock_t wait_lock;
|
spinlock_t wait_lock; // mutex操作锁,用于对mutex的list的操作进行加锁
|
||||||
struct List wait_list;
|
struct List wait_list; // Mutex的等待队列
|
||||||
} mutex_t;
|
} mutex_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user