1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.internal.misc;
  26 
  27 import java.util.Collection;
  28 import java.util.HashSet;
  29 
  30 /**
  31  * A thread-local variable that is notified when a thread terminates and
  32  * it has been initialized in the terminating thread (even if it was
  33  * initialized with a null value).
  34  */
  35 public class JdkThreadLocal<T> extends ThreadLocal<T> {
  36 
  37     @Override
  38     protected final T initialValue() {
  39         T value = computeInitialValue();
  40         REGISTRY.get().add(this);
  41         return value;
  42     }
  43 
  44     @Override
  45     public void set(T value) {
  46         super.set(value);
  47         REGISTRY.get().add(this);
  48     }
  49 
  50     @Override
  51     public void remove() {
  52         super.remove();
  53         REGISTRY.get().remove(this);
  54     }
  55 
  56     /**
  57      * Just like {@link ThreadLocal#initialValue()} but subclasses of {@link JdkThreadLocal}
  58      * must override this method instead as {@link #initialValue()} is final.
  59      *
  60      * @return the initial value for this thread-local
  61      */
  62     protected T computeInitialValue() {
  63         return null;
  64     }
  65 
  66     /**
  67      * Invoked by a thread when terminating and this thread-local has an associated
  68      * value for the terminating thread (even if that value is null), so that any
  69      * native resources maintained by the value can be released.
  70      */
  71     protected void threadTerminated() {
  72     }
  73 
  74     // following method and field are implementation details and should only be
  75     // called from the corresponding code int Thread class.
  76 
  77     /**
  78      * Invokes the JdkThreadLocal's threadTerminated method on all instances of
  79      * given collection.
  80      */
  81     public static void threadTerminated(Collection<JdkThreadLocal<?>> jdkTls) {
  82         if (jdkTls != null) {
  83             for (JdkThreadLocal<?> jdkTl : jdkTls) {
  84                 jdkTl.threadTerminated();
  85             }
  86         }
  87     }
  88 
  89     /**
  90      * a per-thread registry of JdkThreadLocal(s) that have been initialized
  91      * in a particular thread.
  92      */
  93     public static final ThreadLocal<Collection<JdkThreadLocal<?>>> REGISTRY =
  94         new ThreadLocal<>() {
  95             @Override
  96             protected Collection<JdkThreadLocal<?>> initialValue() {
  97                 return new HashSet<>(4);
  98             }
  99         };
 100 }