0Pricing
Assembly Language & x86 Low-Level Systems Programming · 课时

内核空间中的同步与并发

使用自旋锁、互斥锁和原子操作安全地管理内核中的共享数据,并理解中断上下文与进程上下文的区别。

内核空间中的同步与并发 是 CoddyKit 上的免费 Assembly Language & x86 Low-Level Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Assembly Language & x86 Low-Level Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Concurrency Problem

Kernel code runs in a brutally concurrent environment: multiple CPUs, preemptible threads, and interrupts that fire anytime. Unprotected shared data leads to race conditions and corruption.

Process vs Interrupt Context

Kernel code runs in two contexts:

  • Process context: on behalf of a syscall; can sleep
  • Interrupt context: handling hardware; must NOT sleep

The context dictates which locking primitive is legal.

Atomic Operations

The simplest protection is an atomic operation that completes in a single uninterruptible step. The kernel offers types like atomic_t with helpers that map to lock-prefixed x86 instructions.

atomic_t counter = ATOMIC_INIT(0);
atomic_inc(&counter);
int v = atomic_read(&counter);

How Atomics Work in Hardware

On x86 the lock prefix makes a read-modify-write instruction atomic across cores by asserting a cache-line lock.

lock inc dword [counter]   ; atomic increment across CPUs

Spinlocks

A spinlock busy-waits until the lock is free. It never sleeps, so it is the only choice in interrupt context. Hold it for the shortest time possible — spinning wastes CPU.

spinlock_t lock;
spin_lock(&lock);
// critical section
spin_unlock(&lock);

Spinlocks and Interrupts

If an interrupt handler tries to take a spinlock already held on the same CPU, you deadlock. Use spin_lock_irqsave to disable local interrupts while holding the lock.

unsigned long flags;
spin_lock_irqsave(&lock, flags);
// safe even against IRQs
spin_unlock_irqrestore(&lock, flags);

Mutexes and Semaphores

A mutex puts the waiting thread to sleep instead of spinning. It is efficient for longer critical sections but is only usable in process context, never in an interrupt handler.

struct mutex m;
mutex_init(&m);
mutex_lock(&m);
// may sleep here
mutex_unlock(&m);

Choosing the Right Primitive

Quick decision guide:

  • Short, may run in IRQ context -> spinlock
  • Long, process context, can sleep -> mutex
  • Single counter or flag -> atomic

Read-Copy-Update (RCU)

RCU allows lock-free reads of shared data while writers create a new copy and swap a pointer. Readers see either the old or new version, never a torn one. It scales superbly for read-mostly structures.

Memory Barriers

Compilers and CPUs reorder memory accesses. A memory barrier (smp_mb(), smp_wmb()) forces ordering so other cores observe writes in the intended sequence — vital for lock-free code.

Deadlock Avoidance

To prevent deadlock: always acquire multiple locks in a fixed global order, keep critical sections tiny, and never call a sleeping function while holding a spinlock or interrupts are disabled.

Quick Check

Test your kernel concurrency knowledge.

Recap

You learned kernel synchronization:

  • Atomics protect single values via lock-prefixed instructions
  • Spinlocks busy-wait and work in IRQ context; use irqsave variants
  • Mutexes sleep and are process-context only
  • RCU and memory barriers enable scalable lock-free reads

常见问题解答

「内核空间中的同步与并发」课时是免费的吗?

是的 — 「内核空间中的同步与并发」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Assembly Language & x86 Low-Level Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 Assembly Language & x86 Low-Level Systems Programming 课程共包含 4 节课。

「内核空间中的同步与并发」这节课中我会学到什么?

使用自旋锁、互斥锁和原子操作安全地管理内核中的共享数据,并理解中断上下文与进程上下文的区别。 你通过在浏览器中直接运行的动手代码来练习 Assembly Language & x86 Low-Level Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Assembly Language & x86 Low-Level Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Assembly Language & x86 Low-Level Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「内核空间中的同步与并发」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Assembly Language & x86 Low-Level Systems Programming 课中编写并运行代码吗?

能。每节 Assembly Language & x86 Low-Level Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 内核空间简介
  2. 编写简单的设备驱动程序
  3. 直接与硬件交互
  4. 内核空间中的同步与并发
← 返回 Assembly Language & x86 Low-Level Systems Programming