/*
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 实现的更多相关文章

  1. 装逼名词-ABA CAS SpinLock

    今天看wiki,看到一个提到什么什么会陷入 race condition & ABA problem.丫的我没听过ABA呀,那么我去搜了一下,如下: http://www.bubuko.com ...

  2. 【C#】【Thread】SpinLock

    SpinLock结构是一个低级别的互斥同步基元,它在等待获取锁时进行旋转. 在多核计算机上,当等待时间预计较短且极少出现争用情况时,SpinLock 的性能将高于其他类型的锁. 不过,我们建议您仅在通 ...

  3. 锁相关知识 & mutex怎么实现的 & spinlock怎么用的 & 怎样避免死锁 & 内核同步机制 & 读写锁

    spinlock在上一篇文章有提到:http://www.cnblogs.com/charlesblc/p/6254437.html  通过锁数据总线来实现. 而看了这篇文章说明:mutex内部也用到 ...

  4. Linux内核原子(1) - spinlock的实现

    spinlock的数据结构spinlock_t定义在头文件linux/spinlock_types.h里面: typedef struct { raw_spinlock_t raw_lock; #if ...

  5. [20140829]spinlock导致cpu居高不下

    背景: 出现cpu高于常规的告警 排查: 1.开跟踪,没有发现cup特别高的查询 2.查看内核cpu使用量,看是否是sql server 端引起 3.查看负荷,是否负荷特别高这里使用 batch re ...

  6. spinlock原理

    [参考] http://www.searchtb.com/2011/06/spinlock%E5%89%96%E6%9E%90%E4%B8%8E%E6%94%B9%E8%BF%9B.html

  7. 自旋锁-SpinLock(.NET 4.0+)

    短时间锁定的情况下,自旋锁(spinlock)更快.(因为自旋锁本质上不会让线程休眠,而是一直循环尝试对资源访问,直到可用.所以自旋锁线程被阻塞时,不进行线程上下文切换,而是空转等待.对于多核CPU而 ...

  8. 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute

    [源码下载] 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationCont ...

  9. 【linux】spinlock 的实现

    一.什么是spinlock spinlock又称自旋锁,是实现保护共享资源而提出一种锁机制.自旋锁与互斥锁比较类似,都是为了解决对某项资源的互斥使用 无论是互斥锁,还是自旋锁,在任何时刻,最多只能有一 ...

  10. atomic, spinlock and mutex性能比较

    我非常好奇于不同同步原理的性能,于是对atomic, spinlock和mutex做了如下实验来比较: 1. 无同步的情况 #include <future> #include <i ...

随机推荐

  1. wordpress页面前端添加编辑按钮

    <?php edit_post_link(__('Edit This')); ?> 在single.php或者page.php模板页面加入以上代码片段.当管理员登录后,可以直接点击编辑文章 ...

  2. nginx gzip 模块配置

    #gzip模块设置 gzip on; #开启gzip压缩输出 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_ve ...

  3. C# .Net实现URL绝对路径和相对路径之间互相转换

    网站制作开发中,URL的绝对路径和相对路径之间互相转换,是经常需要用到的.以下是在C#.Net下一种实现二者互相转化的方法: [DllImport("shlwapi.dll", C ...

  4. 删除win7中的库/收藏夹/家庭组/网络

    通过修改注册表删除库/收藏夹/家庭组/网络(还是不习惯库的这种管理方式, 导航里面又太占地方) 库:[HKEY_CLASSES_ROOT\CLSID\{031E4825-7B94-4dc3-B131- ...

  5. node.js学习路线图

    http://www.admin10000.com/document/4624.html

  6. Naming Service 与 Zookeeper

      命名服务是指通过指定的名字来获取资源或者服务的地址,提供者的信息.利用Zookeeper很容易创建一个全局的路径,而这个路径就可以作为 一个名字,它可以指向集群中的集群,提供的服务的地址,远程对象 ...

  7. 【Android开发学习笔记】【第九课】重力感应

    概念 使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下. 在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下.注意: 下面提供的demo程 ...

  8. php--如何解决网站分页导致的SEO问题

    如何解决网站分页导致的SEO问题 分页(pagination)是一种自动分页机制,可以将移动Web窗体中的内容分割成一组组较小的页进行呈现,以适合于特定的设备,该机制还呈现可用于浏览到其他页的用户界面 ...

  9. Python排列组合实验

    import itertools 排列: 4个数内选2个 >>> print list(itertools.permutations([1,2,3,4],2)) [(1, 2), ( ...

  10. css“变形”效果

    <html <head> <title></title> <style> .test { margin-left:300px; margin-to ...