Skip to content

Commit f0d7652

Browse files
committed
Fix issue where portfolio solver waits for all solvers
1 parent 1de4b24 commit f0d7652

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/main/scala/inox/solvers/combinators/PortfolioSolver.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ trait PortfolioSolver extends Solver { self =>
5858
}
5959
}
6060

61-
tasks = Future.sequence(fs).map(_ => ())
62-
63-
val result = Future.find(fs.toList)(_._2 != Unknown)
64-
65-
val res = Await.result(result, Duration.Inf) match {
61+
inox.utils.findFirst(fs)(_._2 != Unknown) match {
6662
case Some((s, r)) =>
6763
resultSolver = s.getResultSolver
6864
resultSolver.foreach { solv =>
@@ -72,14 +68,13 @@ trait PortfolioSolver extends Solver { self =>
7268
r
7369
case None =>
7470
reporter.debug("No solver succeeded")
75-
//fs.foreach(f => println(f.value))
7671
config.cast(Unknown)
7772
}
7873

7974
// TODO: Decide if we really want to wait for all the solvers.
8075
// I understand we interrupt them, but what if one gets stuck
8176
// fs foreach { Await.ready(_, Duration.Inf) }
82-
res
77+
// res
8378
}
8479

8580

src/main/scala/inox/utils/package.scala

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
/* Copyright 2009-2018 EPFL, Lausanne */
1+
/* Copyright 2009-2021 EPFL, Lausanne */
22

33
package inox
44

5+
import scala.util._
6+
import scala.concurrent._
7+
import scala.concurrent.duration._
8+
import scala.concurrent.ExecutionContext.Implicits.global
9+
510
/** Various utilities used throughout the Inox system */
611
package object utils {
712

@@ -26,5 +31,22 @@ package object utils {
2631
}
2732
v2
2833
}
29-
34+
35+
def findFirst[T](futures: Seq[Future[T]])(cond: T => Boolean): Option[T] = {
36+
val p = Promise[Option[T]]()
37+
val n = futures.length
38+
var i = 0
39+
40+
for (future <- futures) {
41+
future.onComplete((result: Try[T]) => result match {
42+
case Success(res) if cond(res) =>
43+
p.trySuccess(Some(res))
44+
case _ =>
45+
synchronized { i += 1 }
46+
if (i == n) p.trySuccess(None)
47+
})
48+
}
49+
Await.result(p.future, Duration.Inf)
50+
}
51+
3052
}

0 commit comments

Comments
 (0)