From 74041052473249a21c6e5faf9e90352115b3abf8 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Wed, 23 Jan 2013 15:02:55 +0400 Subject: [PATCH] Storage manager introduced Will be used to make lazily computed data thread-safe --- .../jet/lang/resolve/lazy/LazyValue.java | 27 +++++++ .../resolve/lazy/LockBasedStorageManager.java | 77 +++++++++++++++++++ .../jet/lang/resolve/lazy/ResolveSession.java | 8 ++ .../jet/lang/resolve/lazy/StorageManager.java | 30 ++++++++ 4 files changed, 142 insertions(+) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LazyValue.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LockBasedStorageManager.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/StorageManager.java diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LazyValue.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LazyValue.java new file mode 100644 index 00000000000..32b5225b303 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LazyValue.java @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2013 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.lang.resolve.lazy; + +import org.jetbrains.annotations.NotNull; + +public interface LazyValue { + /** + * Use Optional<T> + */ + @NotNull + T get(); +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LockBasedStorageManager.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LockBasedStorageManager.java new file mode 100644 index 00000000000..18c583d88c3 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/LockBasedStorageManager.java @@ -0,0 +1,77 @@ +/* + * Copyright 2010-2013 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.lang.resolve.lazy; + +import com.intellij.openapi.util.Computable; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +public class LockBasedStorageManager implements StorageManager { + + private final Object lock = new Object() { + @Override + public String toString() { + return "LockBasedStorageManager centralized lock"; + } + }; + + @NotNull + @Override + public ConcurrentMap createConcurrentMap() { + return new ConcurrentHashMap(); + } + + @NotNull + @Override + public LazyValue createLazyValue(@NotNull Computable computable) { + return new LockBasedLazyValue(lock, computable); + } + + private static class LockBasedLazyValue implements LazyValue { + private final Object lock; + private final Computable computable; + + @Nullable + private volatile T value; + + public LockBasedLazyValue(@NotNull Object lock, @NotNull Computable computable) { + this.lock = lock; + this.computable = computable; + } + + @NotNull + @Override + public T get() { + T _value = value; + if (_value != null) { + return _value; + } + + synchronized (lock) { + _value = value; + if (_value == null) { + value = computable.compute(); + _value = value; + } + return _value; + } + } + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/ResolveSession.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/ResolveSession.java index 11d031c4eaf..95730f40dca 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/ResolveSession.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/ResolveSession.java @@ -47,6 +47,8 @@ public class ResolveSession { } }; + private final StorageManager storageManager; + private final ModuleDescriptor module; private final LazyPackageDescriptor rootPackage; @@ -98,6 +100,7 @@ public class ResolveSession { @NotNull Predicate specialClasses, @NotNull BindingTrace delegationTrace ) { + this.storageManager = new LockBasedStorageManager(); this.classifierAliases = classifierAliases; this.specialClasses = specialClasses; this.trace = new ObservableBindingTrace(delegationTrace); @@ -121,6 +124,11 @@ public class ResolveSession { return specialClasses.apply(fqName); } + @NotNull + /*package*/ StorageManager getStorageManager() { + return storageManager; + } + @NotNull public ModuleConfiguration getModuleConfiguration() { return moduleConfiguration; diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/StorageManager.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/StorageManager.java new file mode 100644 index 00000000000..893a65c9ebd --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/StorageManager.java @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2013 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.lang.resolve.lazy; + +import com.intellij.openapi.util.Computable; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.ConcurrentMap; + +public interface StorageManager { + @NotNull + ConcurrentMap createConcurrentMap(); + + @NotNull + LazyValue createLazyValue(@NotNull Computable computable); +}