Created
March 27, 2013 12:58
-
-
Save kjellwinblad/5253979 to your computer and use it in GitHub Desktop.
Interface for delayed writes readers-writer lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A trait for implementations of DelayedWritesLocks. The guarantees | |
| * mentioned in the description of the methods in the trait are of | |
| * course only given if all threads accessing the data protected by | |
| * the lock are calling appropriate methods on the lock. | |
| */ | |
| trait DelayedWritesLock { | |
| trait WriteInfo { | |
| val areaId:Int | |
| def doWrite(): Unit | |
| } | |
| /** | |
| * This method performs the write specified by the WriteInfo | |
| * object. The write will not be reordered with other writes with | |
| * the same areaId that has happened before or will happen after | |
| * this write. All the writes that this write can be reordered with | |
| * including this write will appear to happen atomically to all | |
| * readers. The write will have exclusive access to the data | |
| * protected by the lock when it is performed. | |
| */ | |
| def write(writeInfo:WriteInfo): Unit | |
| /** | |
| * A thread T calling this method is given exclusive access to the | |
| * data protected by the lock until the thread T calls | |
| * writeReadUnlock(). | |
| */ | |
| def writeReadLock(): Unit | |
| /** | |
| * See writeReadLock() | |
| */ | |
| def writeReadUnlock(): Unit | |
| /** | |
| * Helper method that wraps the execution of the function op given | |
| * as parameter between a call to writeReadLock() and | |
| * writeReadUnlock(). Returns the result of the execution of op. | |
| */ | |
| def doWriteReadProtected[T](op:Unit => T): T = { | |
| writeReadLock() | |
| try{ | |
| return op() | |
| }finally{ | |
| writeReadUnlock() | |
| } | |
| } | |
| /** | |
| * A thread T calling this method is guaranteed that no writes to | |
| * the data protected by this lock will be performed before the | |
| * thread T has called readUnlock(). | |
| */ | |
| def readLock(): Unit | |
| /** | |
| * See readLock() | |
| */ | |
| def readUnlock(): Unit | |
| /** | |
| * Helper method that wraps the execution of the function op | |
| * given as parameter between a call to readLock() and | |
| * readUnlock(). Returns the result of the execution of op. | |
| */ | |
| def doReadProtected[T](op:Unit => T): T = { | |
| readLock() | |
| try{ | |
| return op() | |
| }finally{ | |
| readUnlock() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment