Skip to content

Commit

Permalink
Add @sharable annotation
Browse files Browse the repository at this point in the history
Add @sharable annotation for classes and vals that are presumed
to be safely sharable between threads.

Also: Document CtxLazy.
  • Loading branch information
odersky committed Jul 6, 2015
1 parent 64f6518 commit 84ad34a
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/Bench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import reporting.Reporter

object Bench extends Driver {

private var numRuns = 1
@sharable private var numRuns = 1

def newCompiler(): Compiler = new Compiler

Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ object desugar {
else tdef
}

private val synthetic = Modifiers(Synthetic)
@sharable private val synthetic = Modifiers(Synthetic)

private def toDefParam(tparam: TypeDef): TypeDef =
tparam.withFlags(Param)
Expand Down
19 changes: 10 additions & 9 deletions src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package ast

import core._
Expand Down Expand Up @@ -26,7 +27,7 @@ object Trees {
type Untyped = Null

/** The total number of created tree nodes, maintained if Stats.enabled */
var ntrees = 0
@sharable var ntrees = 0

/** Modifiers and annotations for definitions
* @param flags The set flags
Expand Down Expand Up @@ -68,7 +69,7 @@ object Trees {
def tokenPos: Seq[(Token, Position)] = ???
}

private var nextId = 0 // for debugging
@sharable private var nextId = 0 // for debugging

type LazyTree = AnyRef /* really: Tree | Lazy[Tree] */
type LazyTreeList = AnyRef /* really: List[Tree] | Lazy[List[Tree]] */
Expand Down Expand Up @@ -723,9 +724,9 @@ object Trees {
setMods(Modifiers[T](PrivateLocal))
}

val theEmptyTree: Thicket[Type] = Thicket(Nil)
val theEmptyValDef = new EmptyValDef[Type]
val theEmptyModifiers = new Modifiers()
@sharable val theEmptyTree: Thicket[Type] = Thicket(Nil)
@sharable val theEmptyValDef = new EmptyValDef[Type]
@sharable val theEmptyModifiers = new Modifiers()

def genericEmptyValDef[T >: Untyped]: ValDef[T] = theEmptyValDef.asInstanceOf[ValDef[T]]
def genericEmptyTree[T >: Untyped]: Thicket[T] = theEmptyTree.asInstanceOf[Thicket[T]]
Expand Down Expand Up @@ -845,9 +846,9 @@ object Trees {
type Annotated = Trees.Annotated[T]
type Thicket = Trees.Thicket[T]

val EmptyTree: Thicket = genericEmptyTree
val EmptyValDef: ValDef = genericEmptyValDef
val EmptyModifiers: Modifiers = genericEmptyModifiers
@sharable val EmptyTree: Thicket = genericEmptyTree
@sharable val EmptyValDef: ValDef = genericEmptyValDef
@sharable val EmptyModifiers: Modifiers = genericEmptyModifiers

// ----- Auxiliary creation methods ------------------

Expand Down
4 changes: 2 additions & 2 deletions src/dotty/tools/dotc/config/Properties.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Properties extends PropertiesTrait {

/** Scala manifest attributes.
*/
val ScalaCompilerVersion = new AttributeName("Scala-Compiler-Version")
@sharable val ScalaCompilerVersion = new AttributeName("Scala-Compiler-Version")
}

trait PropertiesTrait {
Expand All @@ -23,7 +23,7 @@ trait PropertiesTrait {
protected val propFilename = "/" + propCategory + ".properties"

/** The loaded properties */
protected lazy val scalaProps: java.util.Properties = {
@sharable protected lazy val scalaProps: java.util.Properties = {
val props = new java.util.Properties
val stream = pickJarBasedOn getResourceAsStream propFilename
if (stream ne null)
Expand Down
5 changes: 3 additions & 2 deletions src/dotty/tools/dotc/config/ScalaVersion.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @author James Iry
*/
package dotty.tools.dotc.config
package dotty.tools
package dotc.config

import scala.util.{Try, Success, Failure}

Expand All @@ -15,7 +16,7 @@ sealed abstract class ScalaVersion extends Ordered[ScalaVersion] {
/**
* A scala version that sorts higher than all actual versions
*/
case object NoScalaVersion extends ScalaVersion {
@sharable case object NoScalaVersion extends ScalaVersion {
def unparse = "none"

def compare(that: ScalaVersion): Int = that match {
Expand Down
5 changes: 3 additions & 2 deletions src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import printing._
import config.{Settings, ScalaSettings, Platform, JavaPlatform}
import language.implicitConversions
import DenotTransformers.DenotTransformer

object Contexts {

/** A context is passed basically everywhere in dotc.
Expand Down Expand Up @@ -473,7 +474,7 @@ object Contexts {
gadt = new GADTMap(SimpleMap.Empty)
}

object NoContext extends Context {
@sharable object NoContext extends Context {
lazy val base = unsupported("base")
override val implicits: ContextualImplicits = new ContextualImplicits(Nil, null)(this)
}
Expand Down Expand Up @@ -620,7 +621,7 @@ object Contexts {
/** implicit conversion that injects all ContextBase members into a context */
implicit def toBase(ctx: Context): ContextBase = ctx.base

val theBase = new ContextBase // !!! DEBUG, so that we can use a minimal context for reporting even in code that normally cannot access a context
// @sharable val theBase = new ContextBase // !!! DEBUG, so that we can use a minimal context for reporting even in code that normally cannot access a context
}

/** Info that changes on each compiler run */
Expand Down
9 changes: 5 additions & 4 deletions src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package core

import Periods._, Contexts._, Symbols._, Denotations._, Names._, NameOps._, Annotations._
Expand Down Expand Up @@ -1713,8 +1714,8 @@ object SymDenotations {
validFor = Period.allInRun(NoRunId) // will be brought forward automatically
}

val NoDenotation = new NoDenotation
val NotDefinedHereDenotation = new NoDenotation
@sharable val NoDenotation = new NoDenotation
@sharable val NotDefinedHereDenotation = new NoDenotation

// ---- Completion --------------------------------------------------------

Expand Down Expand Up @@ -1757,7 +1758,7 @@ object SymDenotations {
val NoSymbolFn = (ctx: Context) => NoSymbol

/** A missing completer */
class NoCompleter extends LazyType {
@sharable class NoCompleter extends LazyType {
def complete(denot: SymDenotation)(implicit ctx: Context): Unit = unsupported("complete")
}

Expand Down
5 changes: 3 additions & 2 deletions src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package core

import Contexts._, Types._, Symbols._, Names._, Flags._, Scopes._
Expand Down Expand Up @@ -572,5 +573,5 @@ trait TypeOps { this: Context => // TODO: Make standalone object.

object TypeOps {
val emptyDNF = (Nil, Set[Name]()) :: Nil
var track = false // !!!DEBUG
@sharable var track = false // !!!DEBUG
}
16 changes: 9 additions & 7 deletions src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package core

import util.common._
Expand Down Expand Up @@ -35,7 +36,7 @@ import language.implicitConversions

object Types {

private var nextId = 0
@sharable private var nextId = 0

/** The class of types.
* The principal subclasses and sub-objects are as follows:
Expand Down Expand Up @@ -73,6 +74,7 @@ object Types {

// ----- Tests -----------------------------------------------------

// debug only: a unique identifier for a type
val uniqId = {
nextId = nextId + 1
// if (nextId == 19555)
Expand Down Expand Up @@ -2752,13 +2754,13 @@ object Types {
case class ImportType(expr: Tree) extends UncachedGroundType

/** Sentinel for "missing type" */
case object NoType extends CachedGroundType {
@sharable case object NoType extends CachedGroundType {
override def exists = false
override def computeHash = hashSeed
}

/** Missing prefix */
case object NoPrefix extends CachedGroundType {
@sharable case object NoPrefix extends CachedGroundType {
override def computeHash = hashSeed
}

Expand All @@ -2775,7 +2777,7 @@ object Types {

final class CachedWildcardType(optBounds: Type) extends WildcardType(optBounds)

object WildcardType extends WildcardType(NoType) {
@sharable object WildcardType extends WildcardType(NoType) {
def apply(bounds: TypeBounds)(implicit ctx: Context) = unique(new CachedWildcardType(bounds))
}

Expand Down Expand Up @@ -2983,7 +2985,7 @@ object Types {
}
}

object IdentityTypeMap extends TypeMap()(NoContext) {
@sharable object IdentityTypeMap extends TypeMap()(NoContext) {
override def stopAtStatic = true
def apply(tp: Type) = tp
}
Expand Down Expand Up @@ -3208,7 +3210,7 @@ object Types {

// ----- Debug ---------------------------------------------------------

var debugTrace = false
@sharable var debugTrace = false

val watchList = List[String](
) map (_.toTypeName)
Expand Down
4 changes: 2 additions & 2 deletions src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ object Parsers {
def nonePositive: Boolean = parCounts forall (_ <= 0)
}

object Location extends Enumeration {
@sharable object Location extends Enumeration {
val InParens, InBlock, InPattern, ElseWhere = Value
}

object ParamOwner extends Enumeration {
@sharable object ParamOwner extends Enumeration {
val Class, Type, TypeParam, Def = Value
}

Expand Down
1 change: 0 additions & 1 deletion src/dotty/tools/dotc/transform/CheckReentrant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import typer.Checking
import Names.Name
import NameOps._
import StdNames._
import util.CtxLazy


/** The first tree transform
Expand Down
10 changes: 9 additions & 1 deletion src/dotty/tools/dotc/transform/CtxLazy.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package dotty.tools.dotc
package util
package transform
import core.Contexts.Context

/** Utility class for lazy values whose evaluation depends on a context.
* This should be used whenever the evaluation of a lazy expression
* depends on some context, but the value can be re-used afterwards
* with a different context.
*
* A typical use case is a lazy val in a phase object which exists once per root context where
* the expression intiializing the lazy val depends only on the root context, but not any changes afterwards.
*/
class CtxLazy[T](expr: Context => T) {
private var myValue: T = _
private var forced = false
Expand Down
5 changes: 3 additions & 2 deletions src/dotty/tools/dotc/transform/TreeTransform.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package transform

import dotty.tools.dotc.ast.tpd
Expand Down Expand Up @@ -194,7 +195,7 @@ object TreeTransforms {
}
}

val NoTransform = new TreeTransform {
@sharable val NoTransform = new TreeTransform {
def phase = unsupported("phase")
idx = -1
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -681,4 +681,4 @@ class TermRefSet(implicit ctx: Context) extends mutable.Traversable[TermRef] {
f(TermRef(pre, sym))
}

object EmptyTermRefSet extends TermRefSet()(NoContext)
@sharable object EmptyTermRefSet extends TermRefSet()(NoContext)
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ trait Inferencing { this: Checking =>
}

/** An enumeration controlling the degree of forcing in "is-dully-defined" checks. */
object ForceDegree extends Enumeration {
@sharable object ForceDegree extends Enumeration {
val none, // don't force type variables
noBottom, // force type variables, fail if forced to Nothing or Null
all = Value // force type variables, don't fail
Expand Down
4 changes: 2 additions & 2 deletions src/dotty/tools/dotc/typer/ProtoTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ object ProtoTypes {
* operation is further selection. In this case, the expression need not be a value.
* @see checkValue
*/
object AnySelectionProto extends SelectionProto(nme.WILDCARD, WildcardType, NoViewsAllowed)
@sharable object AnySelectionProto extends SelectionProto(nme.WILDCARD, WildcardType, NoViewsAllowed)

/** A prototype for selections in pattern constructors */
class UnapplySelectionProto(name: Name) extends SelectionProto(name, WildcardType, NoViewsAllowed)
Expand Down Expand Up @@ -308,7 +308,7 @@ object ProtoTypes {
*
* [] _
*/
object AnyFunctionProto extends UncachedGroundType with MatchAlways
@sharable object AnyFunctionProto extends UncachedGroundType with MatchAlways

/** Add all parameters in given polytype `pt` to the constraint's domain.
* If the constraint contains already some of these parameters in its domain,
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) {
override def toString = file.toString
}

object NoSource extends SourceFile("<no source>", Nil) {
@sharable object NoSource extends SourceFile("<no source>", Nil) {
override def exists = false
}

5 changes: 3 additions & 2 deletions src/dotty/tools/dotc/util/Stats.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package util

import core.Contexts._
import collection.mutable

object Stats {
@sharable object Stats {

final val enabled = true

Expand Down

0 comments on commit 84ad34a

Please sign in to comment.