`
xussen
  • 浏览: 31482 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

How to build a exclusive Lock

阅读更多

阻塞lock的基本流程

if(cas(0,acquire))
setExclusiveOwnerThread
else if(current==getExclusiveOnwerThread) {
     setState(c+acquires)
}
addWaiter
park
........................
for(;;) {
p = node.predecessor
if(p==head&&tryAcquire(arg)){
setHead(node)
return
}
park
}
 
阻塞release的基本流程
if(current!=getExclusiveOnwerThread){
IllegalMonitorStateException
}
if(c == 0){
setExclusive(null)
free = true
}
setState(c)
return free
unparkSuccessor(LockSupport.unpark(thread))
 
非阻塞lock的基本流程
if(cas(0,acquire)) 
     setExclusiveOwnerThread   true
else if(current==getExclusiveOwnerThread)
     setState(c+acquire)    true
false
 
非阻塞的release的基本流程
与阻塞的release流程相同
 
 
阻塞共享lock的基本流程
tryAcquireShared(acquires){
for(;;) {
remaining = state-acquires
if(remaining<0 || CAS(state,remaining))
return remaining
}
}
 
 
if(tryAcquireShared(arg)<0){
     node = addWaiter(Node.SHARED)
     for(;;) {
p = node.predecessor
propagate = tryAcquireShared(arg)
if(p==head&&propagate>=0){
setHead(node)
if(propagate>0||next==null||next.isShared())//如果还有剩余,继续唤醒接下来一个节点
     doReleaseShared() 
return
}
park
     }
}
 
阻塞共享release的基本流程
tryReleaseShared(acquires){
for(;;){
     if(cas(state,state+acquires)) 
          return true
}
 
}
if(tryReleaseShared(acquires)) {
     unparkSuccessor(h)
}
 
 
 
 
 

共享lock的基本模式
覆盖tryAcquireShared方法
for(;;) {
     if(state-acquire<0) {
          false
     }
     if(cas(state,state-acquires)) {
          true
     }
}
 
共享lock的release的基本模式
覆盖tryReleaseShared方法
for(;;) {
     if(cas(state,state+acquires)) {
          true
     }
}
 排它lock的基本模式
覆盖tryAcquire方法
if(state==0) {
     if(cas(state, state+acquire)) {
          setExclusiveOwnerThread(current)
              return true
     }
} else if(current==getExclusiveOwnerThread) {
     if(cas(state, state+acquire)) {
          return true
     }
}
return flase
 排它lock的release基本模式
覆盖tryRelease方法
if(current != getExclusiveThread) {
     throw new IllegalMonitorStateException()
}
nextc = state-release
if(nextc == 0) {
     setState(nextc)
     setExclusiveOwnerThread(current)
     return true
} else {
     setState(nextc)
     return false
}
 
分享到:
评论

相关推荐

    微软内部资料-SQL性能优化3

    An intent lock indicates that SQL Server wants to acquire a shared (S) lock or exclusive (X) lock on some of the resources lower down in the hierarchy. For example, a shared intent lock placed at the ...

    The Executive’s How-To Guide to Automation

    An algorithm is merely a set of rules, and anyone with the ability to envision how different components of a business can interact with other components already has the ability to work in algorithms....

    彻底搞清楚library cache lock的成因和解决方法

    lock_mode =&gt; DBMS_LOCK.EXCLUSIVE, wait_time =&gt; 300); -- 修改 SQL 或 PL/SQL 对象 -- ... -- 释放锁 DBMS_LOCK.RELEASE(lock_id =&gt; l_lock_id); EXCEPTION WHEN OTHERS THEN -- 处理异常 DBMS_LOCK....

    92 Applied Predictive Modeling Techniques in R-LEWIS-2015.pdf

    Lewis will show you how to build predictive analytic models in less time than you ever imagined possible! Even if you’re a busy professional or a student with little time. By spending as little as ...

    [iPhone开发书籍大全].Professional.iPhone.and.iPod.Touch.Programming(Wrox.2008).pdf

    this book shows you how to integrate these various elements with key design concepts and principles in order to develop a highly usable interface for the touch screen. You'll learn to use existing ...

    Getting Started with React Native 无水印pdf 0分

    Learn to design and build a fully-featured application using the newest cutting-edge framework from Facebook Leverage your JavaScript skills to become a native app developer Develop custom UI ...

    Unity.5.From.Zero.to.Proficiency.B01EJCE85M

    Chapter 4 explains how to create a simple weapon management system. You will create weapons (e.g., a gun and a grenade launcher), manage the collection of ammunition, and also implement a user ...

    数据库系统原理英文课件:ch16 Concurrency Control.ppt

    A lock is a tool that enforces restrictions on concurrent access to a data item. There are two main lock modes: 1. **Exclusive (X) mode**: Allows both reading and writing of the data item. A ...

    oracle lock

    - **Row Exclusive Table Lock (RX)**:也称为Sub-Exclusive Table Lock (SX),通常表示持有该锁的事务正准备更新整个表,但还没有实际开始更新。这种模式允许其他用户读取表中的数据,但不允许修改。 #### 结论 ...

    Node.js Design Patterns_Second Edition

    It then shows you how to master the asynchronous control flow patterns, and the stream component and it culminates into a detailed list of Node.js implementations of the most common design patterns, ...

    linux下实现高性能读写锁(read/write lock)

    在给定的文件"rwlock.cpp"和"rwlock.h"中,我们可以看到一个实现了Windows的slim read/write lock算法的Linux版本。下面将详细讲解读写锁的工作原理、特性以及如何在Linux下实现。 首先,了解读写锁的基本概念: 1...

    Learning.Node.js.for..NET.Developers.epub

    You will learn how to build web applications and APIs in Node, discover packages in the Node.js ecosystem, test and deploy your Node.js code, and more. Finally, you will discover how to integrate ...

    微软内部资料-SQL性能优化5

    If you build a clustered index without specifying the unique keyword, SQL Server forces uniqueness by adding a uniqueifier to the rows when necessary. This uniqueifier is a 4-byte value added as an ...

    fanucstate.zip

    To build and report mutually exclusive machine states you have to be able to distinguish between different states of your manufacturing resources. For machines equipped with FOCAS-enabled Fanuc ...

    Lock详解.pdf

    - **Exclusive(独占)**:只有一个线程能执行,如ReentrantLock。 - **Shared(共享)**:多个线程可以同时执行,如Semaphore和CountDownLatch。 AQS还定义了两种等待队列: - **同步等待队列**:当线程尝试获取...

    EasyNSE 0.2

    Uses an EasyNSE exclusive menu editor to build the menu instead of the built in menu editor and TMenuItem. Why you ask? TMenuItem was not designed with a shell extension in mind. Delphi menu items are...

Global site tag (gtag.js) - Google Analytics