# Types of Locks and Their Rules — DragonOS dev 文档

备注

AI Translation Notice

This document was automatically translated by `Qwen/Qwen3-8B` model, for reference only.

- Source document: kernel/locking/locks.md

- Translation time: 2025-05-19 01:41:26

- Translation model: `Qwen/Qwen3-8B`

Please report issues via Community Channel

# Types of Locks and Their Rules

## Introduction

The DragonOS kernel implements several types of locks, which can be broadly categorized into two types:

- Sleepable locks

- Spin locks

## Types of Locks

### Sleepable Locks

Sleepable locks can only be acquired in a context that is preemptible.

In DragonOS, the following sleepable locks are implemented:

- semaphore

- mutex_t

### Spin Locks

- spinlock_t

- RawSpinLock (Rust version of spinlock_t, but incompatible with spinlock_t)

- SpinLock —— Built on top of RawSpinLock, it wraps a guard, binding the lock and the data it protects into a single structure. This allows for compile-time checks to prevent accessing data without holding the lock.

When a process acquires a spin lock, it changes the lock count in the PCB, thereby implicitly disabling preemption. To provide more flexible operations, spinlock also provides the following methods:

Suffix | Description
--- | ---
_irq() | Disable interrupts when acquiring the lock, enable them when releasing
_irqsave()/_irqrestore() | Save the interrupt state when acquiring the lock, and restore it when releasing

## Detailed Introduction

### Detailed Introduction to Spin Locks

For a detailed introduction to spin locks, please refer to the document: 自旋锁

### Semaphore

A semaphore is implemented based on a counter.

When the available resources are insufficient, a process attempting to perform a down operation on the semaphore will be put to sleep until the resources become available.

### Mutex

Please refer to Mutex文档

Other Versions v: V0.4.0

Tags
  V0.1.0
  V0.1.1
  V0.1.10
  V0.1.2
  V0.1.3
  V0.1.4
  V0.1.5
  V0.1.6
  V0.1.7
  V0.1.8
  V0.1.9
  V0.2.0
  V0.3.0
  V0.4.0

Branches
  master