Skip to content

Make lazy vals thread-safe by default #6579

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

Merged
merged 4 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backend
package jvm

import scala.tools.asm
import scala.annotation.threadUnsafe

/**
* This class mainly contains the method classBTypeFromSymbol, which extracts the necessary
Expand Down Expand Up @@ -30,14 +31,14 @@ class BTypesFromSymbols[I <: BackendInterface](val int: I) extends BTypes {
coreBTypes.setBTypes(new CoreBTypes[this.type](this))
}

protected lazy val classBTypeFromInternalNameMap = {
@threadUnsafe protected lazy val classBTypeFromInternalNameMap = {
perRunCaches.recordCache(collection.concurrent.TrieMap.empty[String, ClassBType])
}

/**
* Cache for the method classBTypeFromSymbol.
*/
private lazy val convertedClasses = perRunCaches.newMap[Symbol, ClassBType]()
@threadUnsafe private lazy val convertedClasses = perRunCaches.newMap[Symbol, ClassBType]()

/**
* The ClassBType for a class symbol `sym`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import dotty.tools.io.AbstractFile
import scala.language.implicitConversions
import scala.tools.asm


/* Interface to abstract over frontend inside backend.
* Intended to be implemented by both scalac and dotc
*/
Expand Down
17 changes: 9 additions & 8 deletions compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dotty.tools.dotc.transform.{Erasure, GenericSignatures}
import dotty.tools.dotc.transform.SymUtils._
import java.io.{File => _}

import scala.annotation.threadUnsafe
import scala.collection.generic.Clearable
import scala.collection.mutable
import scala.reflect.ClassTag
Expand Down Expand Up @@ -104,8 +105,8 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
val nme_EQEQ_LOCAL_VAR: Name = StdNames.nme.EQEQ_LOCAL_VAR

// require LambdaMetafactory: scalac uses getClassIfDefined, but we need those always.
override lazy val LambdaMetaFactory: ClassSymbol = ctx.requiredClass("java.lang.invoke.LambdaMetafactory")
override lazy val MethodHandle: ClassSymbol = ctx.requiredClass("java.lang.invoke.MethodHandle")
@threadUnsafe override lazy val LambdaMetaFactory: ClassSymbol = ctx.requiredClass("java.lang.invoke.LambdaMetafactory")
@threadUnsafe override lazy val MethodHandle: ClassSymbol = ctx.requiredClass("java.lang.invoke.MethodHandle")

val nme_valueOf: Name = StdNames.nme.valueOf
val nme_apply: TermName = StdNames.nme.apply
Expand Down Expand Up @@ -145,13 +146,13 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
val PartialFunctionClass: Symbol = defn.PartialFunctionClass
val AbstractPartialFunctionClass: Symbol = defn.AbstractPartialFunctionClass
val String_valueOf: Symbol = defn.String_valueOf_Object
lazy val Predef_classOf: Symbol = defn.ScalaPredefModule.requiredMethod(nme.classOf)
@threadUnsafe lazy val Predef_classOf: Symbol = defn.ScalaPredefModule.requiredMethod(nme.classOf)

lazy val AnnotationRetentionAttr: ClassSymbol = ctx.requiredClass("java.lang.annotation.Retention")
lazy val AnnotationRetentionSourceAttr: TermSymbol = ctx.requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("SOURCE")
lazy val AnnotationRetentionClassAttr: TermSymbol = ctx.requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("CLASS")
lazy val AnnotationRetentionRuntimeAttr: TermSymbol = ctx.requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("RUNTIME")
lazy val JavaAnnotationClass: ClassSymbol = ctx.requiredClass("java.lang.annotation.Annotation")
@threadUnsafe lazy val AnnotationRetentionAttr: ClassSymbol = ctx.requiredClass("java.lang.annotation.Retention")
@threadUnsafe lazy val AnnotationRetentionSourceAttr: TermSymbol = ctx.requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("SOURCE")
@threadUnsafe lazy val AnnotationRetentionClassAttr: TermSymbol = ctx.requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("CLASS")
@threadUnsafe lazy val AnnotationRetentionRuntimeAttr: TermSymbol = ctx.requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("RUNTIME")
@threadUnsafe lazy val JavaAnnotationClass: ClassSymbol = ctx.requiredClass("java.lang.annotation.Annotation")

def boxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses().map{x => // @darkdimius Are you sure this should be a def?
(x, Erasure.Boxing.boxMethod(x.asClass))
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/scalaPrimitives.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Names.TermName, StdNames._
import Types.{JavaArrayType, UnspecifiedErrorType, Type}
import Symbols.{Symbol, NoSymbol}

import scala.annotation.threadUnsafe
import scala.collection.immutable


Expand All @@ -37,7 +38,7 @@ import scala.collection.immutable
class DottyPrimitives(ctx: Context) {
import dotty.tools.backend.ScalaPrimitivesOps._

private lazy val primitives: immutable.Map[Symbol, Int] = init
@threadUnsafe private lazy val primitives: immutable.Map[Symbol, Int] = init

/** Return the code for the given symbol. */
def getPrimitive(sym: Symbol): Int = {
Expand Down
Loading