1 /*
   2  * Copyright (c) 2016, 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 
  26 package com.sun;
  27 
  28 import java.lang.ref.Reference;
  29 
  30 /**
  31 * This ReentrantContextProvider implementation uses a ThreadLocal to hold
  32  * the first ReentrantContext per thread and a ReentrantContextProviderCLQ to
  33  * store child ReentrantContext instances needed during recursion.
  34  *
  35  * Note: this implementation may keep up to one context in memory per thread.
  36  * Child contexts for recursive uses are stored in the queue using a WEAK
  37  * reference by default unless specified in the 2 argument constructor.
  38  *
  39  * @param <K> ReentrantContext subclass
  40  */
  41 public abstract class ReentrantContextProviderTL<K extends ReentrantContext>
  42     extends ReentrantContextProvider<K>
  43 {
  44     // Thread-local storage:
  45     private final ThreadLocal<Reference<K>> ctxTL
  46         = new ThreadLocal<Reference<K>>();
  47 
  48     // ReentrantContext CLQ provider for child contexts:
  49     private final ReentrantContextProviderCLQ<K> ctxProviderCLQ;
  50 
  51     /**
  52      * Create a new ReentrantContext provider using the given reference type
  53      * among hard, soft or weak.
  54      * It uses weak reference for the child contexts.
  55      *
  56      * @param refType reference type
  57      */
  58     public ReentrantContextProviderTL(final int refType) {
  59         this(refType, REF_WEAK);
  60     }
  61 
  62     /**
  63      * Create a new ReentrantContext provider using the given reference types
  64      * among hard, soft or weak
  65      *
  66      * @param refTypeTL reference type used by ThreadLocal
  67      * @param refTypeCLQ reference type used by ReentrantContextProviderCLQ
  68      */
  69     public ReentrantContextProviderTL(final int refTypeTL, final int refTypeCLQ)
  70     {
  71         super(refTypeTL);
  72 
  73         final ReentrantContextProviderTL<K> parent = this;
  74 
  75         this.ctxProviderCLQ = new ReentrantContextProviderCLQ<K>(refTypeCLQ) {
  76             @Override
  77             protected K newContext() {
  78                 return parent.newContext();
  79             }
  80         };
  81     }
  82 
  83     /**
  84      * Give a ReentrantContext instance for the current thread
  85      *
  86      * @return ReentrantContext instance
  87      */
  88     @Override
  89     public final K acquire() {
  90         K ctx = null;
  91         final Reference<K> ref = ctxTL.get();
  92         if (ref != null) {
  93             ctx = ref.get();
  94         }
  95         if (ctx == null) {
  96             // create a new ReentrantContext if none is available
  97             ctx = newContext();
  98             // update thread local reference:
  99             ctxTL.set(getOrCreateReference(ctx));
 100         }
 101         // Check reentrance:
 102         if (ctx.usage == USAGE_TL_INACTIVE) {
 103            ctx.usage = USAGE_TL_IN_USE;
 104         } else {
 105             // get or create another ReentrantContext from CLQ provider:
 106             ctx = ctxProviderCLQ.acquire();
 107         }
 108         return ctx;
 109     }
 110 
 111     /**
 112      * Restore the given ReentrantContext instance for reuse
 113      *
 114      * @param ctx ReentrantContext instance
 115      */
 116     @Override
 117     public final void release(final K ctx) {
 118         if (ctx.usage == USAGE_TL_IN_USE) {
 119            ctx.usage = USAGE_TL_INACTIVE;
 120         } else {
 121             ctxProviderCLQ.release(ctx);
 122         }
 123     }
 124 }