2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 import gov.nasa.jpf.annotation.NeverBreak;
21 import java.lang.ref.WeakReference;
22 import java.util.Objects;
23 import java.util.function.Supplier;
26 * model of java.lang.ThreadLocal, which avoids global shared objects
27 * that can otherwise considerably contribute to the state space
29 public class ThreadLocal<T> {
31 static class Entry<E> extends WeakReference<ThreadLocal<E>> {
35 Entry (ThreadLocal<E> key, E val){
40 Entry<E> getChildEntry (){
41 ThreadLocal<E> loc = get();
42 if (loc instanceof InheritableThreadLocal){
43 return new Entry<E>( loc, ((InheritableThreadLocal<E>)loc).childValue(val));
50 public ThreadLocal() {
54 * override to provide initial value
56 protected T initialValue() {
60 private native Entry<T> getEntry();
61 private native void addEntry (Entry<T> e);
62 private native void removeEntry (Entry<T> e);
65 Entry<T> e = getEntry();
69 e = new Entry<T>(this, v);
76 public void set (T v){
77 Entry<T> e = getEntry();
83 e = new Entry<T>(this, v);
89 Entry<T> e = getEntry();
96 // Java 8 provides this as an internal type to be used from lib classes
97 // ?? why is this not done with overridden initialValue() within the concrete ThreadLocal class
98 static final class SuppliedThreadLocal<E> extends ThreadLocal<E> {
100 // we need to preserve the modifiers since this might introduce races (supplier could be shared)
101 private final Supplier<? extends E> sup;
103 SuppliedThreadLocal(Supplier<? extends E> supplier) {
104 sup = Objects.requireNonNull(supplier);
108 protected E initialValue() {