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