Skip to content

Commit ffcbdea

Browse files
committed
Removed ICompilationThread and added TypeProviderLock singleton
1 parent f085e71 commit ffcbdea

File tree

9 files changed

+39
-56
lines changed

9 files changed

+39
-56
lines changed

src/absil/illib.fs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -653,15 +653,6 @@ type Lock<'LockTokenType when 'LockTokenType :> LockToken>() =
653653
//---------------------------------------------------
654654
// Misc
655655

656-
/// The thread in which compilation calls will be enqueued and done work on.
657-
type internal ICompilationThread =
658-
659-
/// Enqueue work to be done on a compilation thread.
660-
abstract EnqueueWork: (CompilationThreadToken -> unit) -> unit
661-
662-
/// Enqueue work to be done on a compilation thread.
663-
abstract EnqueueWorkAndWait: (CompilationThreadToken -> 'T) -> 'T
664-
665656
/// Get an initialization hole
666657
let getHole r = match !r with None -> failwith "getHole" | Some x -> x
667658

src/fsharp/CompilerConfig.fs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ type TcConfigBuilder =
434434
/// show messages about extension type resolution?
435435
mutable showExtensionTypeMessages: bool
436436
#endif
437-
mutable compilationThread: ICompilationThread
438437

439438
/// pause between passes?
440439
mutable pause: bool
@@ -595,11 +594,6 @@ type TcConfigBuilder =
595594
#if !NO_EXTENSIONTYPING
596595
showExtensionTypeMessages = false
597596
#endif
598-
compilationThread =
599-
let ctok = CompilationThreadToken ()
600-
{ new ICompilationThread with
601-
member _.EnqueueWork work = work ctok
602-
member _.EnqueueWorkAndWait work = work ctok }
603597
pause = false
604598
alwaysCallVirt = true
605599
noDebugData = false
@@ -994,7 +988,6 @@ type TcConfig private (data: TcConfigBuilder, validate: bool) =
994988
#if !NO_EXTENSIONTYPING
995989
member x.showExtensionTypeMessages = data.showExtensionTypeMessages
996990
#endif
997-
member x.compilationThread = data.compilationThread
998991
member x.pause = data.pause
999992
member x.alwaysCallVirt = data.alwaysCallVirt
1000993
member x.noDebugData = data.noDebugData

src/fsharp/CompilerConfig.fsi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ type TcConfigBuilder =
243243
#if !NO_EXTENSIONTYPING
244244
mutable showExtensionTypeMessages: bool
245245
#endif
246-
mutable compilationThread: ICompilationThread
247246
mutable pause: bool
248247
mutable alwaysCallVirt: bool
249248
mutable noDebugData: bool
@@ -421,7 +420,6 @@ type TcConfig =
421420
#if !NO_EXTENSIONTYPING
422421
member showExtensionTypeMessages: bool
423422
#endif
424-
member compilationThread: ICompilationThread
425423
member pause: bool
426424
member alwaysCallVirt: bool
427425
member noDebugData: bool

src/fsharp/CompilerImports.fs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,9 @@ type RawFSharpAssemblyDataBackedByFileOnDisk (ilModule: ILModuleDef, ilAssemblyR
695695
type TcImportsSafeDisposal
696696
(disposeActions: ResizeArray<unit -> unit>,
697697
#if !NO_EXTENSIONTYPING
698-
disposeTypeProviderActions: ResizeArray<unit -> unit>,
698+
disposeTypeProviderActions: ResizeArray<unit -> unit>
699699
#endif
700-
compilationThread: ICompilationThread) =
700+
) =
701701

702702
let mutable isDisposed = false
703703

@@ -707,9 +707,11 @@ type TcImportsSafeDisposal
707707
if verbose then
708708
dprintf "disposing of TcImports, %d binaries\n" disposeActions.Count
709709
#if !NO_EXTENSIONTYPING
710-
let actions = disposeTypeProviderActions
711-
if actions.Count > 0 then
712-
compilationThread.EnqueueWork (fun _ -> for action in actions do action())
710+
async {
711+
let actions = disposeTypeProviderActions
712+
if actions.Count > 0 then
713+
TypeProviderLock.Singleton.AcquireLock(fun _ -> for action in actions do action())
714+
} |> Async.Start // Make this async so we do not block dispose
713715
#endif
714716
for action in disposeActions do action()
715717

@@ -760,8 +762,7 @@ and TcImportsWeakHack (tcImports: WeakReference<TcImports>) =
760762
/// Is a disposable object, but it is recommended not to explicitly call Dispose unless you absolutely know nothing will be using its contents after the disposal.
761763
/// Otherwise, simply allow the GC to collect this and it will properly call Dispose from the finalizer.
762764
and [<Sealed>] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAssemblyResolutions, importsBase: TcImports option,
763-
ilGlobalsOpt, compilationThread: ICompilationThread,
764-
dependencyProviderOpt: DependencyProvider option) as this =
765+
ilGlobalsOpt, dependencyProviderOpt: DependencyProvider option) as this =
765766

766767
let mutable resolutions = initialResolutions
767768
let mutable importsBase: TcImports option = importsBase
@@ -786,7 +787,7 @@ and [<Sealed>] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
786787
let mutable tcImportsWeak = TcImportsWeakHack (WeakReference<_> this)
787788
#endif
788789

789-
let disposal = new TcImportsSafeDisposal(disposeActions, disposeTypeProviderActions, compilationThread)
790+
let disposal = new TcImportsSafeDisposal(disposeActions, disposeTypeProviderActions)
790791

791792
let CheckDisposed() =
792793
if disposed then assert false
@@ -1227,8 +1228,7 @@ and [<Sealed>] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
12271228
outputFile = tcConfig.outputFile
12281229
showResolutionMessages = tcConfig.showExtensionTypeMessages
12291230
referencedAssemblies = Array.distinct [| for r in tcImportsStrong.AllAssemblyResolutions() -> r.resolvedPath |]
1230-
temporaryFolder = FileSystem.GetTempPathShim()
1231-
compilationThread = tcConfig.compilationThread }
1231+
temporaryFolder = FileSystem.GetTempPathShim() }
12321232

12331233
// The type provider should not hold strong references to disposed
12341234
// TcImport objects. So the callbacks provided in the type provider config
@@ -1668,7 +1668,7 @@ and [<Sealed>] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
16681668
let tcResolutions = TcAssemblyResolutions.BuildFromPriorResolutions(ctok, tcConfig, frameworkDLLs, [])
16691669
let tcAltResolutions = TcAssemblyResolutions.BuildFromPriorResolutions(ctok, tcConfig, nonFrameworkDLLs, [])
16701670

1671-
let frameworkTcImports = new TcImports(tcConfigP, tcResolutions, None, None, tcConfig.compilationThread, None)
1671+
let frameworkTcImports = new TcImports(tcConfigP, tcResolutions, None, None, None)
16721672

16731673
// Fetch the primaryAssembly from the referenced assemblies otherwise
16741674
let primaryAssemblyReference =
@@ -1802,7 +1802,7 @@ and [<Sealed>] TcImports(tcConfigP: TcConfigProvider, initialResolutions: TcAsse
18021802
let tcConfig = tcConfigP.Get ctok
18031803
let tcResolutions = TcAssemblyResolutions.BuildFromPriorResolutions(ctok, tcConfig, nonFrameworkReferences, knownUnresolved)
18041804
let references = tcResolutions.GetAssemblyResolutions()
1805-
let tcImports = new TcImports(tcConfigP, tcResolutions, Some baseTcImports, Some tcGlobals.ilg, tcConfig.compilationThread, Some dependencyProvider)
1805+
let tcImports = new TcImports(tcConfigP, tcResolutions, Some baseTcImports, Some tcGlobals.ilg, Some dependencyProvider)
18061806
let! _assemblies = tcImports.RegisterAndImportReferencedAssemblies(ctok, references)
18071807
tcImports.ReportUnresolvedAssemblyReferences knownUnresolved
18081808
return tcImports

src/fsharp/ExtensionTyping.fs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ module internal ExtensionTyping =
3131
outputFile : string option
3232
showResolutionMessages : bool
3333
referencedAssemblies : string[]
34-
temporaryFolder : string
35-
compilationThread : ICompilationThread }
34+
temporaryFolder : string }
3635

3736
/// Load a the design-time part of a type-provider into the host process, and look for types
3837
/// marked with the TypeProviderAttribute attribute.
@@ -98,7 +97,7 @@ module internal ExtensionTyping =
9897
// reporting errors.
9998
let protect f =
10099
try
101-
resolutionEnvironment.compilationThread.EnqueueWorkAndWait(fun _ -> f ())
100+
TypeProviderLock.Singleton.AcquireLock(fun _ -> f ())
102101
with err ->
103102
let e = StripException (StripException err)
104103
raise (TypeProviderError(FSComp.SR.etTypeProviderConstructorException(e.Message), typeProviderImplementationType.FullName, m))
@@ -172,7 +171,7 @@ module internal ExtensionTyping =
172171
tpe.Iter(fun e -> errorR(NumberedError((e.Number, e.ContextualErrorMessage), m)) )
173172
[]
174173

175-
let providers = Tainted<_>.CreateAll(providerSpecs, resolutionEnvironment.compilationThread)
174+
let providers = Tainted<_>.CreateAll(providerSpecs)
176175

177176
providers
178177

src/fsharp/ExtensionTyping.fsi

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ module internal ExtensionTyping =
4242

4343
/// The folder for temporary files
4444
temporaryFolder : string
45-
46-
/// The compilation thread
47-
compilationThread : ICompilationThread
4845
}
4946

5047
/// Find and instantiate the set of ITypeProvider components for the given assembly reference

src/fsharp/service/IncrementalBuild.fs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,20 +2085,6 @@ type IncrementalBuilder(tcGlobals, frameworkTcImports, nonFrameworkAssemblyInput
20852085
// Never open PDB files for the language service, even if --standalone is specified
20862086
tcConfigB.openDebugInformationForLaterStaticLinking <- false
20872087

2088-
tcConfigB.compilationThread <-
2089-
{ new ICompilationThread with
2090-
member __.EnqueueWork work =
2091-
Reactor.Singleton.EnqueueOp ("Unknown", "ICompilationThread.EnqueueWork", "work", fun ctok ->
2092-
work ctok
2093-
)
2094-
member __.EnqueueWorkAndWait work =
2095-
Reactor.Singleton.ExecuteOrEnqueueAndAwaitOpAsync("Unknown", "ICompilationThread.EnqueueWorkAndWait", "work", fun ctok ->
2096-
cancellable {
2097-
return work ctok
2098-
}
2099-
) |> Async.RunSynchronously
2100-
}
2101-
21022088
tcConfigB, sourceFilesNew
21032089

21042090
match loadClosureOpt with

src/fsharp/tainted.fs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ open Microsoft.FSharp.Core.CompilerServices
1010
open FSharp.Compiler.AbstractIL.IL
1111
open FSharp.Compiler.AbstractIL.Internal.Library
1212

13+
[<Sealed>]
14+
type internal TypeProviderToken() = interface LockToken
15+
16+
[<Sealed>]
17+
type internal TypeProviderLock() =
18+
inherit Lock<TypeProviderToken>()
19+
20+
static member val Singleton = TypeProviderLock()
21+
1322
type internal TypeProviderError
1423
(
1524
errNum : int,
@@ -69,7 +78,7 @@ type internal TypeProviderError
6978
for msg in errors do
7079
f (new TypeProviderError(errNum, tpDesignation, m, [msg], typeNameContext, methodNameContext))
7180

72-
type TaintedContext = { TypeProvider : ITypeProvider; TypeProviderAssemblyRef : ILScopeRef; CompilationThread: ICompilationThread }
81+
type TaintedContext = { TypeProvider : ITypeProvider; TypeProviderAssemblyRef : ILScopeRef }
7382

7483
[<NoEquality>][<NoComparison>]
7584
type internal Tainted<'T> (context : TaintedContext, value : 'T) =
@@ -88,7 +97,7 @@ type internal Tainted<'T> (context : TaintedContext, value : 'T) =
8897

8998
member this.Protect f (range:range) =
9099
try
91-
context.CompilationThread.EnqueueWorkAndWait (fun _ -> f value)
100+
TypeProviderLock.Singleton.AcquireLock(fun _ -> f value)
92101
with
93102
| :? TypeProviderError -> reraise()
94103
| :? AggregateException as ae ->
@@ -141,9 +150,9 @@ type internal Tainted<'T> (context : TaintedContext, value : 'T) =
141150
/// Access the target object directly. Use with extreme caution.
142151
member this.AccessObjectDirectly = value
143152

144-
static member CreateAll(providerSpecs : (ITypeProvider * ILScopeRef) list, compilationThread) =
153+
static member CreateAll(providerSpecs : (ITypeProvider * ILScopeRef) list) =
145154
[for (tp,nm) in providerSpecs do
146-
yield Tainted<_>({ TypeProvider=tp; TypeProviderAssemblyRef=nm; CompilationThread=compilationThread },tp) ]
155+
yield Tainted<_>({ TypeProvider=tp; TypeProviderAssemblyRef=nm },tp) ]
147156

148157
member this.OfType<'U> () =
149158
match box value with

src/fsharp/tainted.fsi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ open FSharp.Compiler.Range
1212
open FSharp.Compiler.AbstractIL.IL
1313
open FSharp.Compiler.AbstractIL.Internal.Library
1414

15+
[<Sealed>]
16+
type internal TypeProviderToken =
17+
interface LockToken
18+
19+
[<Sealed;Class>]
20+
type internal TypeProviderLock =
21+
inherit Lock<TypeProviderToken>
22+
23+
static member Singleton : TypeProviderLock
24+
1525
/// Stores and transports aggregated list of errors reported by the type provider
1626
type internal TypeProviderError =
1727
inherit System.Exception
@@ -43,7 +53,7 @@ type internal TypeProviderError =
4353
type internal Tainted<'T> =
4454

4555
/// Create an initial tainted value
46-
static member CreateAll : (ITypeProvider * ILScopeRef) list * ICompilationThread -> Tainted<ITypeProvider> list
56+
static member CreateAll : (ITypeProvider * ILScopeRef) list -> Tainted<ITypeProvider> list
4757

4858
/// A type provider that produced the value
4959
member TypeProvider : Tainted<ITypeProvider>

0 commit comments

Comments
 (0)