-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make string Injections safer. Fix #199
- Loading branch information
1 parent
cac17f1
commit 40f668a
Showing
3 changed files
with
40 additions
and
5 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
bijection-core/src/main/scala/com/twitter/bijection/AtomicSharedMutable.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.twitter.bijection | ||
|
||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
/** | ||
* This class is for sharing an object between threads | ||
* when allocation of a new one is not that cheap, but | ||
* better than blocking. | ||
*/ | ||
private[bijection] class AtomicSharedState[T <: AnyRef](cons: () => T) { | ||
private[this] val ref = new AtomicReference[T](cons()) | ||
|
||
def get: T = { | ||
val borrow = ref.getAndSet(null.asInstanceOf[T]) | ||
if (null == borrow) cons() | ||
else borrow | ||
} | ||
def release(t: T): Unit = { | ||
ref.lazySet(t) | ||
} | ||
} |
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
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