linux内核情景分析之内核中的互斥操作
信号量机制:
struct semaphore {atomic_t count;//资源数目int sleepers;//等待进程数目wait_queue_head_t wait;//等待队列#if WAITQUEUE_DEBUGlong __magic;#endif};
/** This is ugly, but we want the default case to fall through.* "__down_failed" is a special asm handler that calls the C* routine that actually waits. See arch/i386/kernel/semaphore.c*/static inline void down(struct semaphore * sem){#if WAITQUEUE_DEBUGCHECK_MAGIC(sem->__magic);#endif__asm__ __volatile__("# atomic down operation\n\t"LOCK "decl %0\n\t" /* --sem->count lock字段把总线锁住,防止其他cpu干扰*/"js 2f\n" /*如果小于0,那就跳转到2号*/"1:\n" /*成功拿到,从此处进入临界区*/".section .text.lock,\"ax\"\n""2:\tcall __down_failed\n\t" /*count--后为负值,休眠*/"jmp 1b\n"/*失败睡眠,但经过一段时间被唤醒,并且进入临界区,就跳转到1*/".previous":"=m" (sem->count):"c" (sem):"memory");}
asm(".align 4\n"".globl __down_failed\n""__down_failed:\n\t""pushl %eax\n\t""pushl %edx\n\t""pushl %ecx\n\t""call __down\n\t""popl %ecx\n\t""popl %edx\n\t""popl %eax\n\t""ret");
void __down(struct semaphore * sem){struct task_struct *tsk = current;DECLARE_WAITQUEUE(wait, tsk);//wait代表tsktsk->state = TASK_UNINTERRUPTIBLE;//设置为睡眠状态add_wait_queue_exclusive(&sem->wait, &wait);//把当前进程的等待队列元素wait加入到sem->wait等待队列队尾spin_lock_irq(&semaphore_lock);sem->sleepers++;//等待进程数目+1for (;;) {int sleepers = sem->sleepers;//禁止本地中断并获取指定的锁/*返回非0,表示进程需要等待* 假设有2个进程,进程资源已经被占用,当前进程执行down失败,跳转到这里,等待调度结郭前一个进程归还了资源,count变为0(之前down2次为-1),sleeper-1也为0,相加等于0,于是可以进入临界区*/if (!atomic_add_negative(sleepers - 1, &sem->count)) {//返回0表示可以进入临界区sem->sleepers = 0;//睡眠的进程为0,因为要唤醒这进程了break;}sem->sleepers = 1; /* 没法到临界区,那就需要阻塞,执行到这,设置为1,那就只有us - see -1 above */spin_unlock_irq(&semaphore_lock);schedule();//调度tsk->state = TASK_UNINTERRUPTIBLE;//将当前进程设置为睡眠状态spin_lock_irq(&semaphore_lock);}spin_unlock_irq(&semaphore_lock);//解锁remove_wait_queue(&sem->wait, &wait);//当前进程可以进入临界区后,从wait队列移除tsk->state = TASK_RUNNING;//设置为可执行状态wake_up(&sem->wait);//唤醒等待队列(然而等待队列很多进程依旧无法进入临界区)}
/** Note! This is subtle. We jump to wake people up only if* the semaphore was negative (== somebody was waiting on it).* The default case (no contention) will result in NO* jumps for both down() and up().*/static inline void up(struct semaphore * sem){#if WAITQUEUE_DEBUGCHECK_MAGIC(sem->__magic);#endif__asm__ __volatile__("# atomic up operation\n\t"LOCK "incl %0\n\t" /* ++sem->count */"jle 2f\n"//如果资源充足(也就是递增结果为正数,直接从1:跳出临界区,不用唤醒阻塞进程(应该说没有阻塞临界区的进程)"1:\n"".section .text.lock,\"ax\"\n""2:\tcall __up_wakeup\n\t" /*递增结果为负数或者非0值,就唤醒阻塞进程*/"jmp 1b\n"".previous":"=m" (sem->count):"c" (sem):"memory");}
asm(".align 4\n"".globl __up_wakeup\n""__up_wakeup:\n\t""pushl %eax\n\t""pushl %edx\n\t""pushl %ecx\n\t""call __up\n\t""popl %ecx\n\t""popl %edx\n\t""popl %eax\n\t""ret");
/** Semaphores are implemented using a two-way counter:* The "count" variable is decremented for each process* that tries to acquire the semaphore, while the "sleeping"* variable is a count of such acquires.** Notably, the inline "up()" and "down()" functions can* efficiently test if they need to do any extra work (up* needs to do something only if count was negative before* the increment operation.** "sleeping" and the contention routine ordering is* protected by the semaphore spinlock.** Note that these functions are only called when there is* contention on the lock, and as such all this is the* "non-critical" part of the whole semaphore business. The* critical part is the inline stuff in <asm/semaphore.h>* where we want to avoid any extra jumps and calls.*//** Logic:* - only on a boundary condition do we need to care. When we go* from a negative count to a non-negative, we wake people up.* - when we go from a non-negative count to a negative do we* (a) synchronize with the "sleeper" count and (b) make sure* that we're on the wakeup list before we synchronize so that* we cannot lose wakeup events.*/void __up(struct semaphore *sem){wake_up(&sem->wait);//唤醒等待队列中的进程}
#define wake_up(x) __wake_up((x),TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,WQ_FLAG_EXCLUSIVE)
void __wake_up(wait_queue_head_t *q, unsigned int mode, unsigned int wq_mode){__wake_up_common(q, mode, wq_mode, 0);}
linux内核情景分析之内核中的互斥操作的更多相关文章
- 几个常用内核函数(《Windows内核情景分析》)
参考:<Windows内核情景分析> 0x01 ObReferenceObjectByHandle 这个函数从句柄得到对应的内核对象,并递增其引用计数. NTSTATUS ObRefer ...
- [1]windows 内核情景分析---说明
本文说明:这一系列文章(笔记)是在看雪里面下载word文档,现转帖出来,希望更多的人能看到并分享,感谢原作者的分享精神. 说明 本文结合<Windows内核情景分析>(毛德操著).< ...
- windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数
windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数 1.KeRaiseIrql函数 这个 KeRaiseIrql() 只是简单地调用 hal 模块的 KfRa ...
- [2]windows内核情景分析--系统调用
Windows的地址空间分用户模式与内核模式,低2GB的部分叫用户模式,高2G的部分叫内核模式,位于用户空间的代码不能访问内核空间,位于内核空间的代码却可以访问用户空间 一个线程的运行状态分内核态与用 ...
- [7] Windows内核情景分析---线程同步
基于同步对象的等待.唤醒机制: 一个线程可以等待一个对象或多个对象而进入等待状态(也叫睡眠状态),另一个线程可以触发那个等待对象,唤醒在那个对象上等待的所有线程. 一个线程可以等待一个对象或多个对象, ...
- [4]Windows内核情景分析---内核对象
写过Windows应用程序的朋友都常常听说"内核对象"."句柄"等术语却无从得知他们的内核实现到底是怎样的, 本篇文章就揭开这些技术的神秘面纱. 常见的内核对象 ...
- [12]Windows内核情景分析 --- MDI
Mdl意为'内存映射描述符'.'缓冲描述符',一个mdl就代表一个缓冲.(任意一块物理内存,可以同时映射到用户地址空间和系统地址空间的) 设备IO方式分为三种:缓冲方式.直接IO方式.直接方式 缓冲方 ...
- [3]windows内核情景分析--内存管理
32位系统中有4GB的虚拟地址空间 每个进程有一个地址空间,共4GB,(具体分为低2GB的用户地址空间+高2GB的内核地址空间) 各个进程的用户地址空间不同,属于各进程专有,内核地址空间部分则几乎完全 ...
- c# 分析SQL语句中的表操作
最近写了很多方向的总结和demo.基本包含了工作中的很多方面,毕竟c#已经高度封装并且提供了很多类库.前面已经总结了博文.最近2天突然感觉前面的SQL分析阻组件的确麻烦,也注意看了下.为了方便大家学习 ...
随机推荐
- GC回收的对象
垃圾收集(Carbage Collection) java内存在运行时区域,程序计数器.java虚拟机栈.本地方法三个区域都是线程私有的内存区域,随着线程的启动和销毁而分配和回收.栈帧随着方法的调 ...
- STM32位带操作
STM32的位带操作是基于cortex内核自带的,而不是st公司独创.基本的思路就是用一个32位的地址空间访问一个bit,因为stm32只支持32位数据的读取,不像51单片机一样,是可以单独对一位操作 ...
- eclipse 插件,直接打开文件路径
https://github.com/samsonw/OpenExplorer/downloads 22k的小插件,意义却重大.下载之后,放到plugins里面.
- SXCPC2018 nucoj2005 大闹上兰帝国
超 dark van♂全背包 ref1 ref2 #include <iostream> #include <cstring> #include <cstdio> ...
- linux学习(三) -- lnmp环境切换php版本,并安装相应redis扩展
原创文章,转载请注明出处 我想配置的环境是ubuntu+nginx+mysql+php+redis,其中php装两个版本,php7和php56 ubuntu+nginx+mysql+php的环境配 ...
- cegui 编译过程详解(cegui-0.8.2)
cegui配置整了好长时间了,在一位大牛帮助下终于搞定了,网上的教程大多是老版本的,cegui-0.8.2版的配置寥寥无几,现在总结一下,献给正在纠结的小白们. 准备工作 第一步,先下载cegui-0 ...
- Python-S9-Day114——Flask开始实战
01 今日内容概要 02 课前分享 03 内容回顾 04 路飞学城表结构(一) 05 路飞学城表结构(二) 06 路飞学城立即支付思路 07 今日作业 08 初识Flask 09 werkzug 10 ...
- Visual Studio 在 C# 项目添加动态链接库 dll
DLL(Dynamic Link Library)文件为动态链接库文件,又称“应用程序拓展”,是软件文件类型. 使用DLL文件的好处是程序不需要在运行之初加载所有代码,只有在程序需要某个函数的时候才从 ...
- 【LeetCode】移除元素(Remove Element)
这道题是LeetCode里的第27道题. 题目描述: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
- 玲珑杯”ACM比赛 Round #15
手速狗从西安回来一只浑浑噩噩,好不容易迎来一场送饭比赛体验一把河南的优势,结果被高中生狂虐,无缘奖金..我的奖品梦就这样一次次被打破.... A -- Reverse the lights 最后半小时 ...