linux 内核的几种锁介绍 http://wenku.baidu.com/link?url=RdvuOpN3RPiC5aY0fKi2Xqw2MyTnpZwZbE07JriN7raJ_L6Ss8Ru1f6C3Gaxl1klYrX8sWGjWV0FJigMFo96Umisnf8cdnccboyczsikpye

一、 以2.6.38以前的内核为例, 讲spinlock、 mutex 以及 semaphore
1. spinlock更原始,效率高,但讲究更多,不能随便用。
2. 个人觉得初级阶段不要去深挖mutex 以及 semaphore的不同,用法类似。在内核代码里面搜索,感觉 DEFINE_MUTEX + mutex_lock_xx + mutex_unlock 用的更多。
3. 在内核里面这三个符号发挥的作用就是: 自旋锁与互斥体。
semaphore:内核中的信号量通常用作mutex互斥体(信号量初值初始化为1,即binary semaphore的方式,就达到了互斥的效果)。
mutex:顾名思义, 互斥体。
所以在内核里面,mutex_lock()和down()的使用情景基本上相同。

//spinlock.h
/******
*API
*spin_lock
*spin_lock_bh
*spin_lock_irq
*spin_trylock
*spin_trylock_bh
*spin_trylock_irq
*spin_unlock
*spin_unlock_bh
*spin_unlock_irq
*spin_unlock_irqrestore
*spin_unlock_wait
******/

//semaphore.h
用 DECLARE_MUTEX 定义了一个count==1 的信号量(binary semaphore)。

#define DECLARE_MUTEX(name)    \
    struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
    
struct semaphore {
    spinlock_t        lock;
    unsigned int        count;
    struct list_head    wait_list;
};

#define __SEMAPHORE_INITIALIZER(name, n)                \
{                                    \
    .lock        = __SPIN_LOCK_UNLOCKED((name).lock),        \
    .count        = n,                        \
    .wait_list    = LIST_HEAD_INIT((name).wait_list),        \
}
    
#define init_MUTEX(sem)        sema_init(sem, 1)
#define init_MUTEX_LOCKED(sem)    sema_init(sem, 0)

/*****
*API:
*#define init_MUTEX(sem)        sema_init(sem, 1)
*#define init_MUTEX_LOCKED(sem)    sema_init(sem, 0)
*extern void down(struct semaphore *sem);
*extern int __must_check down_interruptible(struct semaphore *sem);
*extern int __must_check down_killable(struct semaphore *sem);
*extern int __must_check down_trylock(struct semaphore *sem);
*extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
*extern void up(struct semaphore *sem);
****/

//mutex.h
#define DEFINE_MUTEX(mutexname) \
    struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
    
    #define __MUTEX_INITIALIZER(lockname) \
        { .count = ATOMIC_INIT(1) \
        , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
        , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
        __DEBUG_MUTEX_INITIALIZER(lockname) \
        __DEP_MAP_MUTEX_INITIALIZER(lockname) }

/*
 * Simple, straightforward mutexes with strict semantics:
 *
 * - only one task can hold the mutex at a time
 * - only the owner can unlock the mutex
 * - multiple unlocks are not permitted
 * - recursive locking is not permitted
 * - a mutex object must be initialized via the API
 * - a mutex object must not be initialized via memset or copying
 * - task may not exit with mutex held
 * - memory areas where held locks reside must not be freed
 * - held mutexes must not be reinitialized
 * - mutexes may not be used in hardware or software interrupt
 *   contexts such as tasklets and timers
 *
 * These semantics are fully enforced when DEBUG_MUTEXES is
 * enabled. Furthermore, besides enforcing the above rules, the mutex
 * debugging code also implements a number of additional features
 * that make lock debugging easier and faster:
 *
 * - uses symbolic names of mutexes, whenever they are printed in debug output
 * - point-of-acquire tracking, symbolic lookup of function names
 * - list of all locks held in the system, printout of them
 * - owner tracking
 * - detects self-recursing locks and prints out all relevant info
 * - detects multi-task circular deadlocks and prints out all affected
 *   locks and tasks (and only those tasks)
 */
struct mutex {
    /* 1: unlocked, 0: locked, negative: locked, possible waiters */
    atomic_t        count;
    spinlock_t        wait_lock;
    struct list_head    wait_list;
#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
    struct thread_info    *owner;
#endif
#ifdef CONFIG_DEBUG_MUTEXES
    const char         *name;
    void            *magic;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
    struct lockdep_map    dep_map;
#endif
};

/*******
*API:
*extern void mutex_lock(struct mutex *lock);
*extern int __must_check mutex_lock_interruptible(struct mutex *lock);
*extern int __must_check mutex_lock_killable(struct mutex *lock);
*extern void mutex_unlock(struct mutex *lock);
********/

EG1-:
spinlock_t rtc_lock;
spin_lock_init(&rtc_lock);//每个驱动都会事先初始化,只需要这一次初始化 spin_lock_irq(&rtc_lock);
//临界区
spin_unlock_irq(&rtc_lock); EG1-:
unsigned long flags;
static spinlock_t i2o_drivers_lock;
spin_lock_init(&i2o_drivers_lock);//每个驱动都会事先初始化,只需要这一次初始化 spin_lock_irqsave(&i2o_drivers_lock, flags);
//临界区
spin_unlock_irqrestore(&i2o_drivers_lock, flags); EG2:
static DECLARE_MUTEX(start_stop_sem);
down(&start_stop_sem);
//临界区
up(&start_stop_sem); EG3:
static DEFINE_MUTEX(adutux_mutex);
mutex_lock_interruptible(&adutux_mutex);
//临界区
mutex_unlock(&adutux_mutex);

二、 2.6.38以后DECLARE_MUTEX替换成DEFINE_SEMAPHORE(命名改变), DEFINE_MUTEX用法不变

    static DEFINE_SEMAPHORE(msm_fb_pan_sem);// DECLARE_MUTEX
down(&adb_probe_mutex);
//临界区
up(&adb_probe_mutex); static DEFINE_SEMAPHORE(bnx2x_prev_sem);
down_interruptible(&bnx2x_prev_sem);
//临界区
up(&bnx2x_prev_sem);

Linux 2.6.36以后file_operations和DECLARE_MUTEX 的变化http://blog.csdn.net/heanyu/article/details/6757917

在include/linux/semaphore.h 中将#define DECLARE_MUTEX(name)   改成了 #define DEFINE_SEMAPHORE(name)   【命名】

     
三、自旋锁与信号量
1. 自旋锁
简单的说,自旋锁在内核中主要用来防止多处理器中并发访问临界区,防止内核抢占造成的竞争。【适用于多处理器】【自旋锁会影响内核调度】
另外自旋锁不允许任务睡眠(持有自旋锁的任务睡眠会造成自死锁——因为睡眠有可能造成持有锁的内核任务被重新调度,而再次申请自己已持有的锁),它能够在中断上下文中使用。【不允许任务睡眠】

锁定一个自旋锁的函数有四个:
void spin_lock(spinlock_t *lock); //最基本得自旋锁函数,它不失效本地中断。
void spin_lock_irqsave(spinlock_t *lock, unsigned long flags);//在获得自旋锁之前禁用硬中断(只在本地处理器上),而先前的中断状态保存在flags中
void spin_lockirq(spinlock_t *lock);//在获得自旋锁之前禁用硬中断(只在本地处理器上),不保存中断状态
void spin_lock_bh(spinlock_t *lock);//在获得锁前禁用软中断,保持硬中断打开状态

2. 信号量
内核中的信号量通常用作mutex互斥体(信号量初值初始化为1就达到了互斥的效果)。

如果代码需要睡眠——这往往是发生在和用户空间同步时——使用信号量是唯一的选择。由于不受睡眠的限制,使用信号量通常来说更加简单一些。【信号量使用简单】
如果需要在自旋锁和信号量中作选择,应该取决于锁被持有的时间长短。理想情况是所有的锁都应该尽可能短的被持有,但是如果锁的持有时间较长的话,使用信号量是更好的选择。【如果锁占用的时间较长,信号量更好】
另外,信号量不同于自旋锁,它不会关闭内核抢占,所以持有信号量的代码可以被抢占。这意味者信号量不会对影响调度反应时间带来负面影响。【信号量不会影响内核调度】

3. 使用情景对比
=============================================
需求                     建议的加锁方法 
低开销加锁               优先使用自旋锁
短期锁定                 优先使用自旋锁
长期加锁                 优先使用信号量
中断上下文中加锁         使用自旋锁
持有锁是需要睡眠、调度    使用信号量
=============================================

内核必看: spinlock、 mutex 以及 semaphore的更多相关文章

  1. Linux驱动开发必看详解神秘内核(完全转载)

    Linux驱动开发必看详解神秘内核 完全转载-链接:http://blog.chinaunix.net/uid-21356596-id-1827434.html   IT168 技术文档]在开始步入L ...

  2. 线程同步 –Mutex和Semaphore

    上一篇介绍了同步事件EventWaitHandle,以及它的两个子类型AutoResetEvent和ManualResetEvent.下面接着介绍WaitHandle的另外两个子类型Mutex和Sem ...

  3. Mutex vs Semaphore vs Monitor vs SemaphoreSlim

    C#开发者(面试者)都会遇到Mutex,Semaphore,Monitor,SemaphoreSlim这四个与锁相关的C#类型,本文期望以最简洁明了的方式阐述四种对象的区别. 线程安全 教条式理解 如 ...

  4. Linux网络编程必看书籍推荐

    首先要说讲述计算机网络和TCP/IP的书很多. 先要学习网络知识才谈得上编程 讲述计算机网络的最经典的当属Andrew S.Tanenbaum的<计算机网络>第五版,这本书难易适中. &l ...

  5. Linux经常使用的命令(必看)

    http://www.importnew.com/12425.html     http://www.importnew.com/13107.html http://www.importnew.com ...

  6. Swift 3必看:从使用场景了解GCD新API

    https://www.jianshu.com/p/fc78dab5736f 2016.10.06 21:59* 在学习Swift 3的过程中整理了一些笔记,如果想看其他相关文章可前往<Swif ...

  7. Java编程思想重点笔记(Java开发必看)

    Java编程思想重点笔记(Java开发必看)   Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而 ...

  8. 续评《遇到一位ITer,一位出租车司机,必看》

    链接:<遇到一位ITer,一位出租车司机,必看> 续评:       我算不上严格意义上的共产主义者,但是算是坚定的共富主义者,切忌不能理解为平均主义者.就是说你开个大奔,我最起码也得能开 ...

  9. mutex与semaphore的区别

    网摘1:Mutex 的发音是 /mjuteks/ ,其含义为互斥(体),这个词是Mutual Exclude的缩写.Mutex在计算机中是互斥也就是排他持有的一种方式,和信号量-Semaphore有可 ...

随机推荐

  1. php Smarty详细配置

    1.在Smarty官网下载 路径:https://github.com/smarty-php/smarty/releases 2.把下载下来的Smarty解压出来 3.把解压出来的Smarty里面的l ...

  2. 11.PHP 教程_PHP Switch 语句

    switch 语句用于根据多个不同条件执行不同动作. PHP Switch 语句 如果您希望有选择地执行若干代码块之一,请使用 switch 语句. 语法 switch (n) { case labe ...

  3. IOS UIActionSheet的使用方法

    在IOS的用户接口向导中,苹果提供了另外一种显示警告框的手法,叫做UIActionSheet.它和UIAlertView比起来不会显得过于急切和紧张.而是很温和地在继续流程之前给用户提供了诸多选择. ...

  4. 想精度高,可以考虑用c语言中的函数gettimeofday

    大家好: 在 win32 + bcb 时, 有个 GetTickCount() 返回第统启动到现在的 tick, 单位 ms.请问在 Linux + qt5 怎样实现呢? 如果用 QDateTime ...

  5. activemq在windows下启动报错,闪退问题

    查验了网上各种方法,都没搞定,最后楼主决定按照linux的解决套路来,把windows计算机名称改为纯英文字母,原计算机名:lee_pc,修改后为leepc,然后重启电脑,再重新运行activemq. ...

  6. 【翻译自mos文章】oracle支持在RDBMS HOME 下的 符号链接( Symbolic Links)吗?

    oracle支持在RDBMS HOME 下的 符号链接( Symbolic Links)吗? 參考原文: Does Oracle support Symbolic Links in the RDBMS ...

  7. 数据库SQL基础知识

    数据库:       结构化查询语言(Structured Query Language)简称SQL: 数据库管理系统(Database Management System)简称DBMS: 数据库管理 ...

  8. 「OC」 封装

    一.面向对象和封装 面向对象的三大特性:封装.继承和多态 在OC语言中,使用@interface和@implementation来处理类.   @interface就好像暴露在外面的时钟表面,像外界提 ...

  9. linux ln 命令(转载)

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...

  10. oracle 集合变量以及自定义异常的用法

    oracle 集合变量以及自定义异常的用法, 在过程 record_practice 有record变量和自定义异常的用法实例.具体在3284行. CREATE OR REPLACE Package ...