 		comm = syncedComm
 	}
 
-	private func recv() -> (V?, Bool) {
+	private func recv() -> HandoffReceiveResult<V> {
 		return comm.receiverEnter()
 	}
 
-	private func waitForRecv(v: V) -> Bool {
+	private func waitForRecv(v: V) -> HandoffResult {
 		return comm.senderEnter(v)
 	}
 }

		}

		switch action {
		case let .Block(thread)
			where thread.waitForRecv(v): return
		case let .ToReceiver(thread)
			where thread.send(v): return
		default:
			// When thread.waitForRecv(v) or thread.send(v) return false
			// the communication was canceled so the thread recurses into
			// another attempt to send the value.
			send(v)
		}
	}

	public func recv() -> V {

	static var newURI: String  { return "org.eksdyne.\(self.dynamicType)/\(newUUID)" }
}

// The result of a handoff without access to the value.
public enum HandoffResult {
	case Canceled
	case Completed
}

// The result of a handoff with access to the value if
// the handoff was completed succesfully.
public enum HandoffReceiveResult<V> {
	case Canceled
	case Completed(V)

	var withoutValue: HandoffResult {
		switch self {
		case .Canceled:  return .Canceled
		case .Completed: return .Completed
		}
	}
}

public enum Handoff<V> {
	case Empty

	case Reader
	case Value(V)

	case Ready(V)

	case Done(HandoffReceiveResult<V>)

	func setValue(v: V) -> Handoff {
		switch self {
		case .Reader:
			return .Ready(v)
		default:
			return .Value(v)
		}
	}

	func hasReader() -> Handoff {
		switch self {
		case let .Value(v):
			return Ready(v)
		default:
			return .Reader
		}
	}

	func cancel() -> Handoff {
		switch self {
		case .Done: return self
		default:    return .Done(.Canceled)
		}
	}

	func complete() -> Handoff {
		switch self {
		case .Ready(let v):
			return .Done(.Completed(v))
		default:
			return .Done(.Canceled)
		}
	}

	var isReady: Bool {
		switch self {
		case .Ready:
			fallthrough
		case .Done:
			return true

		default: return false
		}
	}
}

// TODO: Remove
public typealias CommReadyCallback = () -> ()

public protocol Comm {
	var isReady: Bool { get }
	func onReady(_: CommReadyCallback)

	// Returns true if the Comm was canceled
	func cancel() -> HandoffResult
	// Returns true if the Comm was executed
	func proceed() -> HandoffResult
}

public class SyncedComm<V>: Comm, Unique {
	private let partner = dispatch_semaphore_create(0)

	// Sychronizes read/write of the has[Sender|Receiver] variables
	private let q = dispatch_queue_create("\(SyncedComm<V>.newURI).lock", DISPATCH_QUEUE_SERIAL)

	private lazy var triggerHandoff: () -> () = { go { self.proceed() }}

	private var handoff: Handoff<V> = .Empty {
		didSet {
			switch handoff {
			case .Ready:
				triggerHandoff()

			case .Done:
				release()

			default: break
			}
		}
	}

	private func setValue(v: V) { handoff = handoff.setValue(v) }
	private func hasReader()    { handoff = handoff.hasReader() }
	private func cancelHandoff() -> Handoff<V> {
		handoff = handoff.cancel()
		return handoff
	}

	private func completedHandoff() -> Handoff<V> {
		handoff = handoff.complete()
		return handoff
	}

	public var isReady: Bool {
		get {
			var ready: Bool = false
			dispatch_sync(q) { ready = self.handoff.isReady }
			return ready
		}
	}

	init() {}

