SI-7918 Use scala.util.DynamicVariable instead of ad hoc implementations

https://issues.scala-lang.org/browse/SI-7918