JUST DO IT ~

我只想当个程序员

spinlock

待填坑.

自旋锁是一种方法来保护共享资源从通过同时两个或多个进程被修改。试图修改的资源的第一工艺“获取”锁定,并继续在它的途中,在做什么,它需要与资源。任何其他进程随后试图获取锁得到停止;它们被表示为“旋到位”等待锁到被所述第一过程被释放,因而命名自旋锁。
Linux内核使用旋转锁的很多事情,将数据发送到特定外设如。大多数硬件外设不是设计来处理多个并发状态更新。如果两个不同的修改必须发生,就必须严格遵循其它,它们不能重叠。自旋锁提供了必要的保障,确保修改发生一次。
自旋锁是一个问题,因为纺纱阻止做任何其他工作线程的CPU核心。而Linux内核并提供给其下运行的用户空间程序的多任务服务,即通用的多任务处理设施没有延伸到内核代码。
这种情况正在发生变化,并一直为大多数Linux的存在。向上穿过Linux 2.0中,内核几乎纯粹是一个单任务程序:每当CPU正在运行的内核代码中,只有一个CPU内核使用,因为有一个自旋锁保护所有共享资源,被称为大内核锁(BKL )。使用Linux 2.2开始,BKL正在慢慢分解成每保护一个更集中的资源类的许多独立的锁。如今,随着2.6内核,在BKL仍然存在,但它仅用于真正的老代码,不能轻易移动到一些更细化的锁。现在是完全可能的多核框有有用的运行内核代码的每个CPU。
有一个限制分手BKL因为Linux内核一般缺乏多任务的效用。如果CPU内核被阻塞纺内核的自旋锁,它不能被重新定义,去做些别的事情,直到锁被释放。它只是坐在和旋转,直到锁被释放。
自旋锁可以有效地把一个怪物16芯盒成单核中,如果工作负载是这样的,每一个核心总是等待一个单一自旋锁。这是主要的限制Linux内核的可扩展性:CPU核心增加一倍,从2到4可能会在Linux中的近两倍的速度,但它从16倍增至32可能不会,大多数的工作负载。



A spin lock is a way to protect a shared resource from being modified by two or more processes simultaneously. The first process that tries to modify the resource "acquires" the lock and continues on its way, doing what it needed to with the resource. Any other processes that subsequently try to acquire the lock get stopped; they are said to "spin in place" waiting on the lock to be released by the first process, thus the name spin lock.
The Linux kernel uses spin locks for many things, such as when sending data to a particular peripheral. Most hardware peripherals aren't designed to handle multiple simultaneous state updates. If two different modifications have to happen, one has to strictly follow the other, they can't overlap. A spin lock provides the necessary protection, ensuring that the modifications happen one at a time.
Spin locks are a problem because spinning blocks that thread's CPU core from doing any other work. While the Linux kernel does provide multitasking services to user space programs running under it, that general-purpose multitasking facility doesn't extend to kernel code.
This situation is changing, and has been for most of Linux's existence. Up through Linux 2.0, the kernel was almost purely a single-tasking program: whenever the CPU was running kernel code, only one CPU core was used, because there was a single spin lock protecting all shared resources, called the Big Kernel Lock (BKL). Beginning with Linux 2.2, the BKL is slowly being broken up into many independent locks that each protect a more focused class of resource. Today, with kernel 2.6, the BKL still exists, but it's only used by really old code that can't be readily moved to some more granular lock. It is now quite possible for a multicore box to have every CPU running useful kernel code.
There's a limit to the utility of breaking up the BKL because the Linux kernel lacks general multitasking. If a CPU core gets blocked spinning on a kernel spin lock, it can't be retasked, to go do something else until the lock is released. It just sits and spins until the lock is released.
Spin locks can effectively turn a monster 16-core box into a single-core box, if the workload is such that every core is always waiting for a single spin lock. This is the main limit to the scalability of the Linux kernel: doubling CPU cores from 2 to 4 probably will nearly double the speed of a Linux box, but doubling it from 16 to 32 probably won't, with most workloads.

参考: 
taobao 计算文章写的很好. 

spinlock剖析与改进
http://www.searchtb.com/2011/06/spinlock%E5%89%96%E6%9E%90%E4%B8%8E%E6%94%B9%E8%BF%9B.html

http://blog.chinaunix.net/uid-25871104-id-3052138.html
  
http://www.searchtb.com/2011/01/pthreads-mutex-vs-pthread-spinlock.html 
https://en.wikipedia.org/wiki/Spinlock#Example_implementation  

posted on 2015-07-01 13:52 小高 阅读(274) 评论(0)  编辑  收藏 所属分类: Concurrency 并发


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2015年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿(3)

随笔分类(352)

收藏夹(19)

关注的blog

手册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜