JOS中 spinlock 的实现

2024-06-06 09:32
文章标签 实现 spinlock jos

本文主要是介绍JOS中 spinlock 的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JOS中  "spinlock" 的实现


  1. 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.










          春节, 快乐


这篇关于JOS中 spinlock 的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035722

相关文章

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到

Go语言并发之通知退出机制的实现

《Go语言并发之通知退出机制的实现》本文主要介绍了Go语言并发之通知退出机制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、通知退出机制1.1 进程/main函数退出1.2 通过channel退出1.3 通过cont

Python实现PDF按页分割的技术指南

《Python实现PDF按页分割的技术指南》PDF文件处理是日常工作中的常见需求,特别是当我们需要将大型PDF文档拆分为多个部分时,下面我们就来看看如何使用Python创建一个灵活的PDF分割工具吧... 目录需求分析技术方案工具选择安装依赖完整代码实现使用说明基本用法示例命令输出示例技术亮点实际应用场景扩

java如何实现高并发场景下三级缓存的数据一致性

《java如何实现高并发场景下三级缓存的数据一致性》这篇文章主要为大家详细介绍了java如何实现高并发场景下三级缓存的数据一致性,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 下面代码是一个使用Java和Redisson实现的三级缓存服务,主要功能包括:1.缓存结构:本地缓存:使

如何在Java Spring实现异步执行(详细篇)

《如何在JavaSpring实现异步执行(详细篇)》Spring框架通过@Async、Executor等实现异步执行,提升系统性能与响应速度,支持自定义线程池管理并发,本文给大家介绍如何在Sprin... 目录前言1. 使用 @Async 实现异步执行1.1 启用异步执行支持1.2 创建异步方法1.3 调用

Spring Boot配置和使用两个数据源的实现步骤

《SpringBoot配置和使用两个数据源的实现步骤》本文详解SpringBoot配置双数据源方法,包含配置文件设置、Bean创建、事务管理器配置及@Qualifier注解使用,强调主数据源标记、代... 目录Spring Boot配置和使用两个数据源技术背景实现步骤1. 配置数据源信息2. 创建数据源Be

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st