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分析阻组件的确麻烦,也注意看了下.为了方便大家学习 ...
随机推荐
- tp5.1发送邮件
<?php namespace app\admin\controller; use think\Controller; use think\Request; use PHPMailer\PHPM ...
- PHP静态文件缓存
ob_start(); 2 echo 'aaa'; 3 $string = ob_get_contents(); 4 file_put_contents('a.html', $string); 5 o ...
- uncompressing linux .................................................后没反应解决办法
编译kernel是的no machine record defined 错误,网上有一些解法,其实都是错误的,以讹传讹.不打算自己写,找到一篇还算靠谱的,转摘一下. 其根本原因是没有在 __proc_ ...
- 从0到n-1中随机等概率输出m个不同的数
//假设输入的n远大于m void knuth(int n, int m) { for (int i = 0; i < n; i++) { if (rand() % (n - i)<m) ...
- WPF实现QQ群文件列表动画(二)
上篇(WPF实现QQ群文件列表动画(一))介绍了WPF实现QQ群文件列表动画的大致思路,结合我之前讲过的WPF里ItemsControl的分组实现,实现起来问题不大,以下是效果图: 其实就是个List ...
- 300万PV的ASP.NET网站使用阿里云的配置建议
@老牛吃肉在博文“今天的访问高峰,扛过去了”的评论中询问了这样一个问题: 你好,站长,本公司正在考虑用阿里云.用途:互联网网站,主要站点:asp.net开发目前的考虑情况:访问ip 15-20万,pv ...
- 【Best Time to Buy and Sell Stock】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 微信小程序-----校园头条详细开发之列表展示数据
1.分类列表数据展示功能的实现 1.1 结构 1.2 代码实现 1.2.1 列表显示数据,.每次界面显示6条数据,发请求获取数据,动态存放 var app = getApp() Page({ dat ...
- Leetcode 565.数组嵌套
数组嵌套 索引从0开始长度为N的数组A,包含0到N - 1的所有整数.找到并返回最大的集合S,S[i] = {A[i], A[A[i]], A[A[A[i]]], ... }且遵守以下的规则. 假设选 ...
- apple键盘中的Tilde / back-tick key can't work
使用波浪键的时候会输出>或<. 解决办法:http://atodorov.org/blog/2015/04/30/fixing-tilde-and-function-keys-mappin ...