低调小强
驱动牛犊
驱动牛犊
  • 注册日期2005-09-16
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分457分
  • 威望48点
  • 贡献值0点
  • 好评度45点
  • 原创分1分
  • 专家分0分
阅读:2742回复:7

Using Spin Locks

楼主#
更多 发布于:2007-03-09 14:55
  在网上看了很多贴都没有搞清楚!最后还是在书上看到了。。转给需要的看看!应该说的够清楚的了

Using Spin Locks
There are two major kinds of spin locks provided by the kernel. They are distinguished by the IRQL level at which they are used.

-Interrupt spin locks.?/b>

   These synchronize and provide access to driver data structures shared by      multiple-driver routines. Interrupt spin locks are acquired at the DIRQL associated with the device.


-Executive spin locks.?/b>

   These guard various operating system data structures, and their associated IRQL is DISPATCH_LEVEL.


When a driver uses Interrupt spin locks, operation is straightforward. The function KeSynchronizeExecution is described in chapter 8.

Executive spin locks are more complicated to work with. The following steps must be followed when using Executive spin locks:

-Decide what data items must be guarded and how many spin locks should be used. Additional spin locks allow finer granularity of access to the data. However, the possibility of deadlock is present whenever acquisition of more than one spin lock at a time is required.

-Reserve space for a data structure of type KSPIN_LOCK for each lock. Storage for the spin lock must be in nonpaged pool. Usually, the spin lock is declared in the device or controller extension.

-Initialize the spin lock once by calling KeInitializeSpinLock. This function can be called from any IRQL level, though it is most commonly used from the DriverEntry routine.

-Call KeAcquireSpinLock before touching any resource guarded by a spin lock. This function raises IRQL to DISPATCH_LEVEL, acquires the spin lock, and returns the previous IRQL value. This function must be called at or below DISPATCH_LEVEL IRQL. If the code is already at DISPATCH_LEVEL, a more efficient call is KeAcquireSpinLockFromDpcLevel.

-When access to the resource is complete, use the KeReleaseSpinLock function to free the lock. This function is called from DISPATCH_LEVEL IRQL and it restores IRQL to its original value. If the original level was known to be DISPATCH_LEVEL, a more efficient call would be KeReleaseSpinLockFromDpcLevel, which releases the lock without changing IRQL.

Some driver support routines (like the interlock lists and queues described in the next section) use Executive spin locks for protection. In these cases, the only requirement is that the spin lock object be initialized. The driver support routines that manage the interlocked object will acquire and release the spin lock on the driver's behalf.

Rules for Using Spin Locks
Spin locks aren't terribly difficult to use, but there are a few rules that should be followed.

-Be sure to release a spin lock as soon as possible because while holding it, other CPU activity may be blocked. The official DDK recommendation is not to hold a spin lock for more than about 25 microseconds.

-Don't cause any hardware or software exceptions while holding the spin lock. This is a guaranteed system crash.

D-on't access any page code or data while holding the spin lock. This may result in a page fault exception.

-Don't try to acquire a spin lock the CPU already owns. This leads to a deadlock situation since the CPU freezes up, waiting for itself to release the spin lock.

-Avoid driver designs that depend on holding multiple spin locks simultaneously. Without careful design, this can lead to a deadly embrace condition. If multiple spin locks must be used, ensure that all code paths agree to acquire them in a fixed order and release them in the exact reverse order.
killvxk
论坛版主
论坛版主
  • 注册日期2005-10-03
  • 最后登录2014-04-14
  • 粉丝3
  • 关注1
  • 积分1082分
  • 威望2003点
  • 贡献值0点
  • 好评度1693点
  • 原创分2分
  • 专家分0分
沙发#
发布于:2007-03-10 19:23
好文~
好文~
没有战争就没有进步 X3工作组 为您提供最好的军火
ldljlzw
驱动中牛
驱动中牛
  • 注册日期2002-03-16
  • 最后登录2014-01-02
  • 粉丝1
  • 关注0
  • 积分1021分
  • 威望372点
  • 贡献值0点
  • 好评度187点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2007-03-10 20:28
看不懂,英文的!
rayyang2000
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2012-09-13
  • 粉丝3
  • 关注0
  • 积分1036分
  • 威望925点
  • 贡献值3点
  • 好评度823点
  • 原创分0分
  • 专家分0分
地板#
发布于:2007-03-14 08:40
spinlock - 永远的痛~~~~
天天coding-debugging中----超稀饭memory dump file ======================================================== [b]Windows Device Driver Development and Consulting Service[/b] [color=blue][url]http://www.ybwork.com[/url][/color] ========================================================
pilixuanke
驱动中牛
驱动中牛
  • 注册日期2005-10-31
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分1018分
  • 威望626点
  • 贡献值0点
  • 好评度512点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2007-03-14 08:46
引用第3楼rayyang20002007-03-14 08:40发表的“”:
spinlock - 永远的痛~~~~


版主大人何出此言?
向底层开发进军!!!
rayyang2000
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2012-09-13
  • 粉丝3
  • 关注0
  • 积分1036分
  • 威望925点
  • 贡献值3点
  • 好评度823点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2007-03-14 10:11
引用第4楼pilixuanke2007-03-14 10:46发表的“”:


版主大人何出此言?


spinlock确实可以在绝大多数情况下保护shared resource,但是它会改变IRQL,太多的API/memory不可用,得非常小心的组织代码
天天coding-debugging中----超稀饭memory dump file ======================================================== [b]Windows Device Driver Development and Consulting Service[/b] [color=blue][url]http://www.ybwork.com[/url][/color] ========================================================
pilixuanke
驱动中牛
驱动中牛
  • 注册日期2005-10-31
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分1018分
  • 威望626点
  • 贡献值0点
  • 好评度512点
  • 原创分0分
  • 专家分0分
6楼#
发布于:2007-03-14 12:34
引用第5楼rayyang20002007-03-14 10:11发表的“”:


spinlock确实可以在绝大多数情况下保护shared resource,但是它会改变IRQL,太多的API/memory不可用,得非常小心的组织代码


呵呵,记住啦!
一般是提到DISPATCH_LEVEL级别吧,在spinlock中没有用过什么API,memory当然是用Nonpaged pool的,
向底层开发进军!!!
chen1110
驱动牛犊
驱动牛犊
  • 注册日期2012-04-07
  • 最后登录2012-04-07
  • 粉丝0
  • 关注0
  • 积分15分
  • 威望101点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
7楼#
发布于:2012-04-07 12:19
现在我们能够做的,是找一个静静的地方,让自己静静的思考,明白该如何做,才能够不让珍贵的东西,重要的人再次失去,明白该如何做,郑州牛皮癣专科医院    同样的错误不会再次发生。从中吸取经验,吸取力量,继续坚定的前行,寻找喜欢的东西,碰到真爱的人,去做正确的事。——【席慕容】
游客

返回顶部