- 浏览: 358380 次
- 性别:
- 来自: 阿里巴巴
文章分类
- 全部博客 (207)
- Maven (5)
- Cassandra (2)
- Hadoop (3)
- LDAP (2)
- SOA (7)
- 认证、加密、安全 (6)
- 搜索引擎相关技术 (3)
- REST (7)
- 数据库 (11)
- Java 基础相关 (38)
- UML (1)
- Java NIO 框架 (3)
- javassist (1)
- Bean容器 (4)
- 网络编程 (1)
- NoSQL (4)
- XML、Json (1)
- JS (2)
- Google (6)
- Warp-MVC (2)
- 持久层 (2)
- sitebricks (1)
- MVC (6)
- CSS (2)
- JPA (2)
- RDBMS (5)
- cache (4)
- tomcat (1)
- 其它 (3)
- eclipse (1)
- bigpipe (1)
- RDBMS MySQL (1)
- MySQL (2)
- ant (1)
- 前端 (2)
- Groovy (1)
- linux (3)
- Scala (1)
- zookeeper (1)
- redis (2)
- 测试 (1)
- 监控 (1)
- mac (3)
- 区块链 (3)
- 工具 (1)
最新评论
-
masuweng:
好好好,辛苦了!!
Spring Data JPA 简单介绍 -
masuweng:
Spring Data JPA 简单介绍 -
zhangjianxinjava:
您好,大神本人小白一个最近在研究不知道可否 通过邮箱进行交流, ...
JAVA Metrics度量工具 - Metrics Core -
xzs603:
http://zhengdl126.iteye.com/blo ...
数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器 -
JavaStart:
运行mysql2redis 的install.sh 文件为何提 ...
mysql到redis的复制
1 Overview
Linux内核中常见的同步机制有Atomic Operation,Spin Locks,Semaphore,Mutex等。其中Spin Locks和Semaphore都支持读/写锁。此外,Linux内核还支持一种更轻量级的读/写锁定机制:Sequential Lock。跟其它读/写锁定机制相比,Sequential Lock有以下特点:
- 在获取锁时偏向写锁。只要他人不持有写锁,那么获得写锁的操作总是能够成功。
- 读操作时不需要获得锁(即读操作永远不会被阻塞),然而在有些情况下读操作需要重试。
- Sequential Lock更轻量级,可伸缩性相对更好。
2 Linux Kernel's Implementation
以下是2.6版本的内核中,Sequential Lock的实现:
typedef struct { unsigned sequence; spinlock_t lock; } seqlock_t; static inline void write_seqlock(seqlock_t *sl) { spin_lock(&sl->lock); ++sl->sequence; smp_wmb(); } static inline void write_sequnlock(seqlock_t *sl) { smp_wmb(); sl->sequence++; spin_unlock(&sl->lock); } static __always_inline unsigned read_seqbegin(const seqlock_t *sl) { unsigned ret = sl->sequence; smp_rmb(); return ret; } static __always_inline int read_seqretry(const seqlock_t *sl, unsigned iv) { smp_rmb(); return (iv & 1) | (sl->sequence ^ iv); }
以上代码中,通过使用spin lock保证了write操作的原子性。需要注意的是,Linux spin lock是非可重入锁,这也是read_seqretry方法中可以通过(iv & 1)判断一个写操作是否正在进行中的原因。此外,Linux spin lock只能保证原子性,为了避免由于编译器或者处理器对指令的重排序(例如x86平台不会对写操作重排序,但是可能会对读操作进行重排序)所导致的潜在问题,seqlock使用memory barrier(smp_wmb和smp_rmb)保证了内存可见性。
seqlock的用法如下:
To define a seqlock:
seqlock_t mr_seq_lock = DEFINE_SEQLOCK(mr_seq_lock);
The write path:
write_seqlock(&mr_seq_lock); /* write lock is obtained... */ write_sequnlock(&mr_seq_lock);
The read path:
unsigned long seq; do { seq = read_seqbegin(&mr_seq_lock); /* read data here ... */ } while (read_seqretry(&mr_seq_lock, seq));
seqlock的典型使用场景如下:
- 跟读操作的数量相比,写操作的数量很少。
- 在锁获取时,希望写操作优先。
使用seqlock的注意事项:
- 读操作耗时应比较短,否则容易导致过多次的读重试。
- 由于在读的过程中写操作可能正在修改数据,因此读操作可能会读取到某种不一致的状态。所以进行读取时,需要保护性的验证,以避免因读取不一致的错误数据导致错读。
3 Sequential Lock in Java
本想自己实现一个Java版本的Sequential lock,但是在Google了之后发现Makoto YUI已经提供了一比较好的实现,在其基础之上稍加改动之后的实现如下:
public final class SequentialLock { // private volatile int counter; /** * */ public int readBegin() { return this.counter; } public boolean readRetry(int v) { return (v & 1) == 1 || this.counter != v; } public synchronized void writeLock() { // if((this.counter & 1) != 0) { throw new IllegalStateException("sequential lock is NOT reentrantable"); } // ++this.counter; } public synchronized void writeUnlock() { ++this.counter; } }
首先,SequentialLock不是可重入的。其次由于读操作没有获得锁,因此读写之间需要额外的机制以保证内存可见性:对其volitle counter成员变量的读写保证了Sequential Lock保护的数据之间的happens bofore语义。
4 Reference
Linux Kernel Development 3rd Edition, Robert Love
发表评论
-
浮点数计算
2020-08-25 16:15 290@Test public void test2() ... -
AQS、ReentrantLock、CLH锁 、MCS锁 分析
2018-12-25 23:45 6451. ReentrantLock的介绍 Reentran ... -
Java并发编程:CountDownLatch、CyclicBarrier、Semaphore、Phaser
2015-02-26 16:41 1284在java 1.5中,提供了 ... -
技术网站资料
2014-07-17 11:53 0http://javakaiyuan.com ht ... -
Java之死-前言
2014-07-15 10:14 720用了好多年的java之后越来越感觉其并不像 ... -
通过JVM获取相关的服务器信息 .
2013-01-07 20:18 2141http://blog.csdn.net/zgmzyr/art ... -
Java中使用OpenSSL生成的RSA公私钥进行数据加解密
2012-10-25 11:34 4343openssl genrsa -out rsa_pr ... -
java开源
2012-05-29 14:34 1421开源不是开放编译器的源代码,而是写了一个软 ... -
用Ant打Jar包--在Manifest中引用Classpath
2012-03-15 13:20 1566用Ant打Jar包--在Manifest ... -
分布式锁服务器
2011-11-07 22:14 1915在分布式系统中如何 ... -
Java 7 Fork/Join 并行计算框架概览
2011-11-05 16:26 1043http://www.iteye.com/topic/ ... -
Apache Jakarta Commons 工具集简介
2011-11-03 11:22 1525org.apache.commons.collectio ... -
JDBC batch批处理Statement executeBatch 详解
2011-09-28 13:25 2703http://blog.csdn.net/basene ... -
代码动态生成利器ASM
2011-09-22 20:02 1029前言 代码生成器(code generato ... -
Fastjson技术内幕
2011-09-13 23:51 1313文章来源:http://code.alibabatech.co ... -
基于Spring可扩展Schema提供自定义配置支持
2011-08-07 16:01 1198在很多情况下,我们需要为系统提供可配置化支持,简单的做法可以直 ... -
Mule 与 Spring2.0's extensible XML configuration mechanism
2011-08-07 15:50 1333mule2.2.1已经采用从sprin ... -
alibaba fastjson(json序列化器)序列化部分源码解析
2011-08-03 21:11 1968本文copy自http://www.flydmeng.co ... -
JVM 远程调试 参数
2011-07-28 09:49 1850远程调试 参数 -server -Xdebug -Xnoag ... -
Hessian远程调用及序列化协议
2011-06-28 18:22 2971demo中客户端和服务端的hessian版本都是4.0.7,下 ...
相关推荐
Non sequential model in zemax
simply more things that can go wrong in a concurrent program than in a sequential one. So, why do we bother with concurrency? Threads are an inescapable feature of the Java language, and they can ...
in Java), linked lists, stacks, queues, recursion, random numbers, files (text, binary, random access, indexed), binary trees, advanced sorting methods (heapsort, quicksort, mergesort, Shell sort), ...
Detection of anomalous trajectories is an important problem in the surveillance domain. Various algorithms based on learning of normal trajectory patterns have been proposed for this problem. Yet, ...
Sequential Monte Carlo in C++ 包括一片文章介绍SMC,然后是C++代码实现。
role in modeling user behaviors, various sequential recom- mendation methods have been proposed. Methods based on Markov assumption are widely-used, but independently com- bine several most recent ...
SPMF is an open-source data mining mining library written in Java, specialized in pattern mining. It is distributed under the GPL v3 license. It offers implementations of 82 data mining algorithms ...
Processor Arch-Sequential
时间序列分析——The main focus of this book is on a systematic development of the theory of sequential hypothesis testing (Part I) and changepoint detection (Part II). In Part III, we briefly describe...
Sequential Logic and Verilog HDL Fundamentals discusses the analysis and synthesis of synchronous and asynchronous sequential machines. These machines are implemented using Verilog Hardware ...
Mining Sequential Patterns
What You Will Learn, Design concurrent applications by converting a sequential algorithm into a concurrent oneDiscover how to avoid all the possible problems you can get in concurrent algorithmsUse ...
Sequential events in a single computation are not concurrent. For example, if a particular computation performs the operations identified by events 1, 2 and 3 in that order, then clearly and 1 ! 2 and...
对SC(Sequential Consistency)和TSO(Total Store Order)内存模型进行了讲解, 浅显易懂, 用最简明的图示讲明了二者的区别, 适合初步学习相关知识的同学阅读.
level language, this book applies the Deitel signature live-code approach to teaching programming and explores the Java® 9 language and APIs in depth. The book presents concepts in fully tested ...
自己总结的关于datastage sequential stage属性的说明。希望对各位同志有用,如果有不对的地方请多多指教。
Communicating Sequential Processes
基于STM32F103ZET,移植LWIP协议栈,sequential编程接口实现TCP客户端