JOS中 "spinlock" 的实现
2015-02-19 15:42
295 查看
JOS中 "spinlock" 的实现
In software engineering, a spinlock is
a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available. Since the thread remains active but is not performing a useful task,the use of such a lock is a kind of busy waiting.
-- from wikipedia
XCHG instruction Swaps data between two registers or a register and memory location.
LOCK汇编指令用于封锁总线.
于是乎,在JOS中,spinlock的实现如下:
./kern/spinlock.h 这里利用结构体struct spinlock来抽象封装spinlock.
实质上,spinlock是个全局变量.这里具体的就是./kern/spinlock.h 中的全局变量结构体
extern struct spinlock kernel_lock.
原理还是很简单的,只是基于汇编层次的全局变量的busy waiting.
春节, 快乐
In software engineering, a spinlock is
a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available. Since the thread remains active but is not performing a useful task,the use of such a lock is a kind of busy waiting.
-- from wikipedia
XCHG instruction Swaps data between two registers or a register and memory location.
LOCK汇编指令用于封锁总线.
static inline uint32_t xchg(volatile uint32_t *addr, uint32_t newval) { uint32_t result; // The + in "+m" denotes a read-modify-write operand. asm volatile("lock; xchgl %0, %1" : "+m" (*addr), "=a" (result) : "1" (newval) : "cc"); return result; }每次都会返回addr指向地址处原来的值.
于是乎,在JOS中,spinlock的实现如下:
./kern/spinlock.h 这里利用结构体struct spinlock来抽象封装spinlock.
实质上,spinlock是个全局变量.这里具体的就是./kern/spinlock.h 中的全局变量结构体
extern struct spinlock kernel_lock.
// Mutual exclusion lock. struct spinlock { unsigned locked; // Is the lock held? }; void __spin_initlock(struct spinlock *lk, char *name); void spin_lock(struct spinlock *lk); void spin_unlock(struct spinlock *lk); #define spin_initlock(lock) __spin_initlock(lock, #lock) extern struct spinlock kernel_lock; static inline void lock_kernel(void) { spin_lock(&kernel_lock); } static inline void unlock_kernel(void) { spin_unlock(&kernel_lock); // Normally we wouldn't need to do this, but QEMU only runs // one CPU at a time and has a long time-slice. Without the // pause, this CPU is likely to reacquire the lock before // another CPU has even been given a chance to acquire it. asm volatile("pause"); }
// Mutual exclusion spin locks. // The big kernel lock struct spinlock kernel_lock = { #ifdef DEBUG_SPINLOCK .name = "kernel_lock" #endif }; spinlock锁的初始化 void __spin_initlock(struct spinlock *lk, char *name) { lk->locked = 0; } // Acquire the lock. // Loops (spins) until the lock is acquired. // Holding a lock for a long time may cause // other CPUs to waste time spinning to acquire it. void spin_lock(struct spinlock *lk) { // The xchg is atomic. // It also serializes, so that reads after acquire are not // reordered before it. while (xchg(&lk->locked, 1) != 0) asm volatile ("pause"); } // Release the lock. void spin_unlock(struct spinlock *lk) { // The xchg serializes, so that reads before release are // not reordered after it. The 1996 PentiumPro manual (Volume 3, // 7.2) says reads can be carried out speculatively and in // any order, which implies we need to serialize here. // But the 2007 Intel 64 Architecture Memory Ordering White // Paper says that Intel 64 and IA-32 will not move a load // after a store. So lock->locked = 0 would work here. // The xchg being asm volatile ensures gcc emits it after // the above assignments (and after the critical section). xchg(&lk->locked, 0); }
原理还是很简单的,只是基于汇编层次的全局变量的busy waiting.
春节, 快乐
相关文章推荐
- 高性能自旋锁 MCS Spinlock 的设计与实现(来自IBM)
- 高性能自旋锁 MCS Spinlock 的设计与实现
- 高性能自旋锁 MCS Spinlock 的设计与实现
- Linux spin_lock的实现
- Linux Kernel之spin_lock之ARM64实现
- Linux Kernel之spin_lock之ARM实现
- nginx spinlock 自旋锁的实现
- 高性能自旋锁 MCS Spinlock 的设计与实现
- linux中对spin_lock的实现
- 调用spin_lock_irqsave(&chip->lock,flags); 的下层实现是什么?
- 利用mysql "named lock"实现分布式系统的同步
- Linux Kernel之spin_lock之ARM64实现
- 封装 JMail 4.4 的 POP3 为 .Net 组件 (.dll 程序集),实现 "邮件(附件) 到达" 等 "事件"!
- C# 1.x 实现 "强类型元素唯一的 ArrayList"
- 3G"中国标准"明年实现商用[NEWS]
- 如何使 FlashGet "正常合法" 下载 Session 中的自定义文件链接呢? JSP/Servlet 实现!
- 在VB中实现"绿色"软件
- .Net Remoting 实现简易的"命令行控制台"聊天室
- 封装 JMail 4.4 的 POP3 为 .Net 组件 (.dll 程序集),实现 "邮件(附件) 到达" 等 "事件"!
- 根据汉字声母,要在表中找相关记录,怎么实现?比如输入"zgyh",就能找到所有“中国银行”的记录。