Last active
December 23, 2015 00:09
-
-
Save Blaisorblade/6551895 to your computer and use it in GitHub Desktop.
JMM-safe implementation of memory barriers
This file contains 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
object JMMBarriers { | |
@volatile private var volatile: Int = 0 | |
/** Enforce a write barrier. | |
* | |
* Writes before this will not be reordered after this barrier, and will be | |
* visible from any other thread which performs a read barrier. | |
*/ | |
def writeBarrier() { volatile = 1 } | |
/** Enforce a read barrier. | |
* | |
* Reads after this will not be reordered before this barrier, and will see | |
* the effects of any writes from a thread which performed a write barrier | |
* before this one. | |
*/ | |
def readBarrier() { volatile } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment