Skip to content

Instantly share code, notes, and snippets.

@rightfold

rightfold/a.mill Secret

Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/1c9f64faef24921f9e94 to your computer and use it in GitHub Desktop.
Save rightfold/1c9f64faef24921f9e94 to your computer and use it in GitHub Desktop.
// <- for send and receive
import mill.time
import mill.conc.chan
import mill.conc.thread
sub withTimeout[R](timeout: time.Duration, s: () => R): R {
let resultChan = chan.make[R]()
thread.spawn(sub(): () { resultChan <- s() })
select {
case let result = <-resultChan => result
case let _ = <-time.after(timeout) => throw(timeoutError)
}
}
// chan.send and chan.recv for send and receive
import mill.time
import mill.conc.chan
import mill.conc.thread
sub withTimeout[R](timeout: time.Duration, s: () => R): R {
let resultChan = chan.make[R]()
thread.spawn(sub(): () { chan.send(resultChan, s()) })
select {
case let result = chan.recv(resultChan) => result
case let _ = chan.recv(time.after(timeout)) => throw(timeoutError)
}
}
// always-in-scope (unless shadowed) send and recv for send and receive
import mill.time
import mill.conc.chan
import mill.conc.thread
sub withTimeout[R](timeout: time.Duration, s: () => R): R {
let resultChan = chan.make[R]()
thread.spawn(sub(): () { send(resultChan, s()) })
select {
case let result = recv(resultChan) => result
case let _ = recv(time.after(timeout)) => throw(timeoutError)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment