SpinLock 实现
/*
Example: SpinLock
Description: SpinLock is the lock implementation using AtomicInteger as a primitive synchronizer.
The contracts is based on the paper to be submitted to ECOOP14.
These contracts are specified with one abstract predicate for the resource exchange.
Author: Afshin Amighi
Status: Pass.
command: vct --chalice --progress --explicit SpinLock.java
*/ public class SpinLock{ //shared resource
private int data; // int SynchronizerRole (S) = 0, ThreadRole = 1;
// int UNLOCKED = 0 , LOCKED = 1; //@ resource handle(int role,int val);
//@ resource trans(int role,int last,int next)=( role == 1 ==> true );
//@ pure zfrac part(int rs,int s,int rt, int v){ return (rs == 0 && s == 0) ? 100:0; }
// for simplicity we take resource_invariant=Perm(data,p)
//@ resource inv(zfrac p)= Perm(data,p); /* ----------- AtomicInteger -----------------*/
/*@
given int r, l;
requires srh:handle(r,l) ** sra:trans(r,l,v) ** srr:inv(part(0,v,r,v)) ;
ensures seh:handle(r,v) ** ser:inv(part(0,l,r,l));
*/
void set(int v); /*@
given int r, l;
requires crh:handle(r,l) ** cra:trans(r,x,n) ** crr:inv(part(0,n,r,n));
ensures \result ==> cehp:handle(r,n) ** cerp:inv(part(0,x,r,x));
ensures !\result ==> cehn:handle(r,l) ** cern:inv(part(0,n,r,n));
*/
boolean compareAndSet(int x,int n); /*----------- SpinLock ----------------*/ //@ given int last;
//@ requires lrh: handle(1,last);
//@ ensures leh: handle(1,1) ** Perm(data,100);
public void dolock(){
boolean succ = false;
//@ int role = 1, S=0;
//@ SpinLock.trans tcra;
//@ SpinLock.inv tcrr , tces;
//@ fold tcrr:inv(part(S,1,role,1)); //@ loop_invariant !succ ==> invhn:handle(role,last) ** invpn: inv(part(S,1,role,1));
//@ loop_invariant succ ==> invhp:handle(role,1) ** invpp: inv(part(S,0,role,0));
while (!succ) /*@ with{ invhn = lrh; invpn = tcrr; } then { leh = invhp; tces = invpp; } */ {
//@ fold tcra:trans(role,0,1);
succ = compareAndSet(0,1) /*@ with{ r = role; l = last; crh = invhn; cra = tcra; crr = invpn; }
then{ invhn = cehn; invpn = cern; invhp = cehp; invpp = cerp; } */ ;
}
//@ unfold tces: inv(part(S,0,role,0));
return;
} //@ requires urh:handle(1,1) ** Perm(data,100);
//@ ensures ueh: handle(1,0);
public void unlock(){
//@ int role = 1, S=0;
//@ int last = 1;
//@ SpinLock.inv tsrp;
//@ fold tsrp:inv(part(S,0,role,0));
//@ SpinLock.trans tsra;
//@ fold tsra:trans(role,last,0);
set(0) /*@ with{ r = role; l = last; srh = urh; sra = tsra; srr = tsrp; } then { ueh = seh; } @*/;
}
}
SpinLock 实现的更多相关文章
- 装逼名词-ABA CAS SpinLock
今天看wiki,看到一个提到什么什么会陷入 race condition & ABA problem.丫的我没听过ABA呀,那么我去搜了一下,如下: http://www.bubuko.com ...
- 【C#】【Thread】SpinLock
SpinLock结构是一个低级别的互斥同步基元,它在等待获取锁时进行旋转. 在多核计算机上,当等待时间预计较短且极少出现争用情况时,SpinLock 的性能将高于其他类型的锁. 不过,我们建议您仅在通 ...
- 锁相关知识 & mutex怎么实现的 & spinlock怎么用的 & 怎样避免死锁 & 内核同步机制 & 读写锁
spinlock在上一篇文章有提到:http://www.cnblogs.com/charlesblc/p/6254437.html 通过锁数据总线来实现. 而看了这篇文章说明:mutex内部也用到 ...
- Linux内核原子(1) - spinlock的实现
spinlock的数据结构spinlock_t定义在头文件linux/spinlock_types.h里面: typedef struct { raw_spinlock_t raw_lock; #if ...
- [20140829]spinlock导致cpu居高不下
背景: 出现cpu高于常规的告警 排查: 1.开跟踪,没有发现cup特别高的查询 2.查看内核cpu使用量,看是否是sql server 端引起 3.查看负荷,是否负荷特别高这里使用 batch re ...
- spinlock原理
[参考] http://www.searchtb.com/2011/06/spinlock%E5%89%96%E6%9E%90%E4%B8%8E%E6%94%B9%E8%BF%9B.html
- 自旋锁-SpinLock(.NET 4.0+)
短时间锁定的情况下,自旋锁(spinlock)更快.(因为自旋锁本质上不会让线程休眠,而是一直循环尝试对资源访问,直到可用.所以自旋锁线程被阻塞时,不进行线程上下文切换,而是空转等待.对于多核CPU而 ...
- 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute
[源码下载] 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationCont ...
- 【linux】spinlock 的实现
一.什么是spinlock spinlock又称自旋锁,是实现保护共享资源而提出一种锁机制.自旋锁与互斥锁比较类似,都是为了解决对某项资源的互斥使用 无论是互斥锁,还是自旋锁,在任何时刻,最多只能有一 ...
- atomic, spinlock and mutex性能比较
我非常好奇于不同同步原理的性能,于是对atomic, spinlock和mutex做了如下实验来比较: 1. 无同步的情况 #include <future> #include <i ...
随机推荐
- 批量更改int类型的timestamp字段to datetime
批量更改int类型的timestamp字段to datetime 1.创建datetime字段created_at 2.update 字段 UPDATE table set created_at = ...
- w win cmd VS broswer
php -f Fetch data from db , use php without any bugs to analyse the data and read and write db, with ...
- PowerDesigner连接MySQL,建立逆向工程图解
传说中,程序员们喜欢用powerDesign进行数据库建模.通常都是先设计出物理模型图,在转换出数据库需要的SQL语句,从而生成数据库.但,江湖中流传着"powerDesign逆向工程&qu ...
- Super不要在Super构造器中调用覆盖方法
import java.util.Date; public class Super{ public Super(){ System."); overrideMe(); System.&quo ...
- hive中的常用方法(case,cast,unix_timestamp)
1.case的用法 )格式1 case col when value then '' when value then '' else '' end )格式2 case when col='value' ...
- Java进制转换
其他转10进制 System.out.println(Integer.parseInt("10", 2));// bin System.out.println(Integer.pa ...
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Java学习-037-JavaWeb_006 -- JSP 动作标识 - include
这个动作是指在当前的页面中包含一个或多个 JSP 页面或者 HTML 文件,语法:<jsp:include file="../jsp/login.jsp" flush=&qu ...
- 关于action script与js相互调用的Security Error问题
大家都知道,as和js相互调用可以通过ExternalInterface.call和ExternalInterface.addCallback来进行. 比较好的做法是使用之前通过ExternalInt ...
- leetcode算法
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...