File tree Expand file tree Collapse file tree 2 files changed +26
-9
lines changed Expand file tree Collapse file tree 2 files changed +26
-9
lines changed Original file line number Diff line number Diff line change @@ -58,11 +58,7 @@ trait PortfolioSolver extends Solver { self =>
58
58
}
59
59
}
60
60
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 {
66
62
case Some ((s, r)) =>
67
63
resultSolver = s.getResultSolver
68
64
resultSolver.foreach { solv =>
@@ -72,14 +68,13 @@ trait PortfolioSolver extends Solver { self =>
72
68
r
73
69
case None =>
74
70
reporter.debug(" No solver succeeded" )
75
- // fs.foreach(f => println(f.value))
76
71
config.cast(Unknown )
77
72
}
78
73
79
74
// TODO: Decide if we really want to wait for all the solvers.
80
75
// I understand we interrupt them, but what if one gets stuck
81
76
// fs foreach { Await.ready(_, Duration.Inf) }
82
- res
77
+ // res
83
78
}
84
79
85
80
Original file line number Diff line number Diff line change 1
- /* Copyright 2009-2018 EPFL, Lausanne */
1
+ /* Copyright 2009-2021 EPFL, Lausanne */
2
2
3
3
package inox
4
4
5
+ import scala .util ._
6
+ import scala .concurrent ._
7
+ import scala .concurrent .duration ._
8
+ import scala .concurrent .ExecutionContext .Implicits .global
9
+
5
10
/** Various utilities used throughout the Inox system */
6
11
package object utils {
7
12
@@ -26,5 +31,22 @@ package object utils {
26
31
}
27
32
v2
28
33
}
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
+
30
52
}
You can’t perform that action at this time.
0 commit comments