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 ...
随机推荐
- unity中Debug输出控制
1 需求: (1)选择在界面.console中输出,并且能够设置保存到文档 (2)控制debug是否输出,可以在debug模式下输出,release模式下不输出 2 参考: 谢谢雨松同学的博客:htt ...
- java动态联编
JAVA中联编有两种,一种是动态联编,一种是静态联编. 动态联编:也叫多态联编或者是迟后联编,因为到底要调用哪一个函数,在编译时不能确定,而要推迟到运行中确定.也就是说,要等到程序运行时,确定了指针所 ...
- mysql 存储过程 php版本
<?php /** * PHP操作Mysql存储过程示例 * * @author flyer0126 * @date 2011-12-23 * */ //配置数据库连接信息 $hostname ...
- java NIO ;mvn
http://ifeve.com/java-nio-scattergather/ mvn introduction(install,conf, proxy,plugin,samples) http:/ ...
- Linux上free命令的输出
一.明确概念 A buffer is something that has yet to be "written" to disk. A cache is something t ...
- HTML文件基本结构
固定结构: <html> <head>...</head> <body>...</body> </html>1,<html ...
- spring3 mvc:方法返回值的学习
新建后台代码用以测试返回类型,在这里我新建的如下: /** * 项目名称:Spring3mvc demo * Copyright ? 2010-2012 spartacus.org.cn All Ri ...
- RTSP 协议分析
RTSP 协议分析1.概述: RTSP(Real Time Streaming Protocol),实时流传输协议,是TCP/IP协议体系中的一个应用层协议,由哥伦比亚大学.网景和RealNetwor ...
- 【C++】字符串的大小写转换
#include<string> #include<algorithm> #include<iostream> using namespace std; int m ...
- <转>RowState 介绍
1. RowState 介绍 RowState 是 DataRow 很重要的一个属性, 表示 DataRow 当前的状态. RowState 有 Added, Modified, Unchanged, ...