Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move smartPos function to use it in Stainless + Portfolio solver fix #134

Merged
merged 5 commits into from
Apr 18, 2021

Conversation

jad-hamza
Copy link
Contributor

No description provided.

@jad-hamza jad-hamza changed the title Move smartPos function to use it in Stainless Move smartPos function to use it in Stainless + Portfolio solver fix Apr 16, 2021
@jad-hamza
Copy link
Contributor Author

I added a fix for epfl-lara/stainless#980

I found these links that discuss behavior changes for Future.find, it's probably safer to implement our own:
scala/bug#11827
https://stackoverflow.com/questions/53759765/scala-future-find
https://stackoverflow.com/questions/35961330/scala-future-list-first-completed-with-condition

However I'm not used to using Future or Promise, does the code look ok? @samarion @vkuncak
I tested it on a small example and it seems to work well, and it also seems to fix the Stainless issue:

import scala.util._
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

object Timeout {
  def findFirst[T](futures: Seq[Future[T]])(cond: T => Boolean): Option[T] = {
    val p = Promise[Option[T]]()
    val n = futures.length
    var i = 0

    for (future <- futures) {
      future.onComplete((result: Try[T]) => result match {
        case Success(res) if cond(res) =>
          p.trySuccess(Some(res))
        case _ =>
          synchronized { i += 1 }
          if (i == n) p.trySuccess(None)
      })
    }
    Await.result(p.future, Duration.Inf)
  }

  def main(args: Array[String]): Unit = {
    val times = Seq(4, 6, 10)
    val futures = times.map(i =>
      Future({
        println("waiting",i)
        Thread.sleep(i * 1000)
        println("waited i",i)
        i
      })
    )
    val res = findFirst(futures)(_ == 6)
    res match {
      case None => println("none")
      case Some(res) => println("result", res)
    }
  }
}

@samarion
Copy link
Member

Looks reasonable to me. You might want to use an AtomicInteger instead of the synchronized block because it's immune to thread starvation deadlocks. Also maybe add a dedicated FutureUtils.scala file?

@jad-hamza
Copy link
Contributor Author

jad-hamza commented Apr 16, 2021

Thanks I did the change.
The StringSuite failed because I tried to change to CVC4 1.8, I reverted to 1.7 and restarted the build

@jad-hamza
Copy link
Contributor Author

I moved CVC4 back to 1.8 for larabot, and added here some decoding for strings produced by CVC4 1.8, which contain characters of the form \u{ff} and u{f}.

@samarion samarion merged commit fdbf887 into epfl-lara:master Apr 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants