待填坑.
自旋鎖是一種方法來保護共享資源從通過同時兩個或多個進程被修改。試圖修改的資源的第一工藝“獲取”鎖定,并繼續(xù)在它的途中,在做什么,它需要與資源。任何其他進程隨后試圖獲取鎖得到停止;它們被表示為“旋到位”等待鎖到被所述第一過程被釋放,因而命名自旋鎖。
Linux內(nèi)核使用旋轉(zhuǎn)鎖的很多事情,將數(shù)據(jù)發(fā)送到特定外設(shè)如。大多數(shù)硬件外設(shè)不是設(shè)計來處理多個并發(fā)狀態(tài)更新。如果兩個不同的修改必須發(fā)生,就必須嚴(yán)格遵循其它,它們不能重疊。自旋鎖提供了必要的保障,確保修改發(fā)生一次。
自旋鎖是一個問題,因為紡紗阻止做任何其他工作線程的CPU核心。而Linux內(nèi)核并提供給其下運行的用戶空間程序的多任務(wù)服務(wù),即通用的多任務(wù)處理設(shè)施沒有延伸到內(nèi)核代碼。
這種情況正在發(fā)生變化,并一直為大多數(shù)Linux的存在。向上穿過Linux 2.0中,內(nèi)核幾乎純粹是一個單任務(wù)程序:每當(dāng)CPU正在運行的內(nèi)核代碼中,只有一個CPU內(nèi)核使用,因為有一個自旋鎖保護所有共享資源,被稱為大內(nèi)核鎖(BKL )。使用Linux 2.2開始,BKL正在慢慢分解成每保護一個更集中的資源類的許多獨立的鎖。如今,隨著2.6內(nèi)核,在BKL仍然存在,但它僅用于真正的老代碼,不能輕易移動到一些更細化的鎖。現(xiàn)在是完全可能的多核框有有用的運行內(nèi)核代碼的每個CPU。
有一個限制分手BKL因為Linux內(nèi)核一般缺乏多任務(wù)的效用。如果CPU內(nèi)核被阻塞紡內(nèi)核的自旋鎖,它不能被重新定義,去做些別的事情,直到鎖被釋放。它只是坐在和旋轉(zhuǎn),直到鎖被釋放。
自旋鎖可以有效地把一個怪物16芯盒成單核中,如果工作負載是這樣的,每一個核心總是等待一個單一自旋鎖。這是主要的限制Linux內(nèi)核的可擴展性:CPU核心增加一倍,從2到4可能會在Linux中的近兩倍的速度,但它從16倍增至32可能不會,大多數(shù)的工作負載。
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