Skip to content

Instantly share code, notes, and snippets.

@DRMacIver
Created March 14, 2009 14:45
Show Gist options
  • Select an option

  • Save DRMacIver/79086 to your computer and use it in GitHub Desktop.

Select an option

Save DRMacIver/79086 to your computer and use it in GitHub Desktop.
object timeout{
private val _doomsday = new scala.util.DynamicVariable[Long](Long.MaxValue);
def doomsday = _doomsday.value;
def timeRemaining = doomsday - System.currentTimeMillis;
def checkpoint = if (System.currentTimeMillis > _doomsday.value) throw DieNow;
def timeout[T](time : Long)(t : =>T) : Option[T] = _doomsday.withValue(System.currentTimeMillis + time){
try { Some(t) } catch { case DieNow => None }
}
private[timeout] object DieNow extends Error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment