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

相关文章

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义