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 import java.util.concurrent.ConcurrentLinkedQueue;
  30 
  31 /**
  32  * This ReentrantContextProvider implementation uses one ConcurrentLinkedQueue
  33  * to store all ReentrantContext instances (thread and its child contexts)
  34  *
  35  * Note: this implementation keeps less contexts in memory depending on the
  36  * concurrent active threads in contrary to a ThreadLocal provider. However,
  37  * it is slower in highly concurrent workloads.
  38  *
  39  * @param <K> ReentrantContext subclass
  40  */
  41 public abstract class ReentrantContextProviderCLQ<K extends ReentrantContext>
  42     extends ReentrantContextProvider<K>
  43 {
  44     // ReentrantContext queue to store all contexts
  45     private final ConcurrentLinkedQueue<Reference<K>> ctxQueue
  46         = new ConcurrentLinkedQueue<Reference<K>>();
  47 
  48     /**
  49      * Create a new ReentrantContext provider using the given reference type
  50      * among hard, soft or weak based using a ConcurrentLinkedQueue storage
  51      *
  52      * @param refType reference type
  53      */
  54     public ReentrantContextProviderCLQ(final int refType) {
  55         super(refType);
  56     }
  57 
  58     /**
  59      * Give a ReentrantContext instance for the current thread
  60      *
  61      * @return ReentrantContext instance
  62      */
  63     @Override
  64     public final K acquire() {
  65         K ctx = null;
  66         // Drain queue if all referent are null:
  67         Reference<K> ref = null;
  68         while ((ctx == null) && ((ref = ctxQueue.poll()) != null)) {
  69             ctx = ref.get();
  70         }
  71         if (ctx == null) {
  72             // create a new ReentrantContext if none is available
  73             ctx = newContext();
  74             ctx.usage = USAGE_CLQ;
  75         }
  76         return ctx;
  77     }
  78 
  79     /**
  80      * Restore the given ReentrantContext instance for reuse
  81      *
  82      * @param ctx ReentrantContext instance
  83      */
  84     @Override
  85     public final void release(final K ctx) {
  86         if (ctx.usage == USAGE_CLQ) {
  87             ctxQueue.offer(getOrCreateReference(ctx));
  88         }
  89     }
  90 }