org.jetbrains.jet.storage.LockBasedLazyResolveStorageManager.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.storage
import com.intellij.util.containers.ConcurrentWeakValueHashMap
import org.jetbrains.annotations.TestOnly
import org.jetbrains.jet.lang.diagnostics.Diagnostic
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.BindingTrace
import org.jetbrains.jet.lang.resolve.Diagnostics
import org.jetbrains.jet.util.slicedmap.ReadOnlySlice
import org.jetbrains.jet.util.slicedmap.WritableSlice
public class LockBasedLazyResolveStorageManager(private val storageManager: StorageManager): StorageManager by storageManager, LazyResolveStorageManager {
override fun createWeaklyRetainedMemoizedFunction(compute: Function1) =
storageManager.createMemoizedFunction(compute, ConcurrentWeakValueHashMap())
override fun createWeaklyRetainedMemoizedFunctionWithNullableValues(compute: Function1) =
storageManager.createMemoizedFunctionWithNullableValues(compute, ConcurrentWeakValueHashMap())
// It seems safe to have a separate lock for traces:
// no other locks will be acquired inside the trace operations
override fun createSafeTrace(originalTrace: BindingTrace) =
LockProtectedTrace(storageManager, originalTrace)
private class LockProtectedContext(private val storageManager: StorageManager, private val context: BindingContext) : BindingContext {
override fun getDiagnostics(): Diagnostics = storageManager.compute { context.getDiagnostics() }
override fun get(slice: ReadOnlySlice, key: K) = storageManager.compute { context.get(slice, key) }
override fun getKeys(slice: WritableSlice) = storageManager.compute { context.getKeys(slice) }
TestOnly
override fun getSliceContents(slice: ReadOnlySlice) = storageManager.compute { context.getSliceContents(slice) }
}
private class LockProtectedTrace(private val storageManager: StorageManager, private val trace: BindingTrace) : BindingTrace {
private val context: BindingContext = LockProtectedContext(storageManager, trace.getBindingContext())
override fun getBindingContext() = context
override fun record(slice: WritableSlice, key: K, value: V) {
storageManager.compute { trace.record(slice, key, value) }
}
override fun record(slice: WritableSlice, key: K) {
storageManager.compute { trace.record(slice, key) }
}
override fun get(slice: ReadOnlySlice, key: K): V = storageManager.compute { trace.get(slice, key) }
override fun getKeys(slice: WritableSlice): Collection = storageManager.compute { trace.getKeys(slice) }
override fun report(diagnostic: Diagnostic) {
storageManager.compute { trace.report(diagnostic) }
}
}
}