/*
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. 运维工程师必会的109个Linux命令

    运维工程师必会的109个Linux命令 版本1.0 崔存新 更新于2009-12-26 目录 1 文件管理 6 1.1 basename 6 1.2 cat 6 1.3 cd 7 1.4 chgrp ...

  2. WordPress工作原理之程序文件执行顺序

    在了解WordPress挂载机制时,一直有一个疑惑,到底是WordPress的内核源文件先执行还是主题文件里functions.php文件先执行.为了解决这个问题,想了解WordPress的工作原理, ...

  3. nginx服务器安装与启动

    nginx服务器介绍 1.nginx服务器功能 nginx服务器可以扮演:轻量级的web服务器.应用服务器.代理服务器.反向代理服务器.后台服务器.CDN缓存服务器 nginx的基本模块: 内核模块C ...

  4. add active class

    根据URI添加菜单的active css class Active item item in menu: <?php function aim($page) { if(stristr($_SER ...

  5. Delphi 如何清除动态数组的内存?

    SetLength(glb_IndexConfig,); FreeAndNil(glb_IndexConfig);

  6. js 程序出发事件

    <html> <head><title>Test</title></head> <body> <div id='divTe ...

  7. 关于Hive的调优(本身,sql,mapreduce)

    1.关于hive的优化 ->大表拆分小表 ->过滤字段 ->按字段分类存放 ->外部表与分区表 ->外部表:删除时只删除元数据信息,不删除数据文件 多人使用多个外部表操作 ...

  8. 介绍UDF,以及完成大小写的转换

    一:概述 1.UDF 用户自定义函数,用java实现自定义的需求 2.UDF的类型 udf:一进一出 udaf:多进一出 udtf:一进多出 3.udf的实现步骤 继承UDF类 实现evaluate的 ...

  9. ubuntu下hadoop环境配置

    软件环境: 虚拟机:VMware Workstation 10 操作系统:ubuntu-12.04-desktop-amd64 JAVA版本:jdk-7u55-linux-x64 Hadoop版本:h ...

  10. undefined和void

    1.undefined undefined在js中并不是关键字/保留字,因此在IE5.5~8中可以对undefined赋值,但是在IE9以上,对其赋值是无效的 <script> var a ...