From 5b2d94a274abe82d26687f1c6ef19fb1d57df2dd Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 31 Oct 2015 09:57:50 +0100 Subject: [PATCH] Check that access to context base is singlethreaded. ContextBase is not intended to be threadsafe, We now test that indeed it is not shared by compileUnits calls operating on different threads. --- src/dotty/tools/dotc/Run.scala | 1 + src/dotty/tools/dotc/core/Contexts.scala | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/dotty/tools/dotc/Run.scala b/src/dotty/tools/dotc/Run.scala index 114f994be612..ba86e3e70de2 100644 --- a/src/dotty/tools/dotc/Run.scala +++ b/src/dotty/tools/dotc/Run.scala @@ -50,6 +50,7 @@ class Run(comp: Compiler)(implicit ctx: Context) { } protected def compileUnits() = Stats.monitorHeartBeat { + ctx.checkSingleThreaded() val phases = ctx.squashPhases(ctx.phasePlan, ctx.settings.Yskip.value, ctx.settings.YstopBefore.value, ctx.settings.YstopAfter.value, ctx.settings.Ycheck.value) ctx.usePhases(phases) diff --git a/src/dotty/tools/dotc/core/Contexts.scala b/src/dotty/tools/dotc/core/Contexts.scala index f9d64b2cc56f..d5fdba1af9a4 100644 --- a/src/dotty/tools/dotc/core/Contexts.scala +++ b/src/dotty/tools/dotc/core/Contexts.scala @@ -611,6 +611,16 @@ object Contexts { superIdOfClass.clear() lastSuperId = -1 } + + // Test that access is single threaded + + /** The thread on which `checkSingleThreaded was invoked last */ + @sharable private var thread: Thread = null + + /** Check that we are on the same thread as before */ + def checkSingleThreaded() = + if (thread == null) thread = Thread.currentThread() + else assert(thread == Thread.currentThread(), "illegal multithreaded access to ContextBase") } object Context {