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 /** 28 * 29 * @param <K> 30 */ 31 public abstract class ReentrantContextProviderTL<K extends ReentrantContext> 32 extends ReentrantContextProvider<K> 33 { 34 // Thread-local storage: 35 private final ThreadLocal<Object> ctxTL = new ThreadLocal<Object>(); 36 37 // ReentrantContext CLQ provider for child contexts: 38 private final ReentrantContextProviderCLQ<K> ctxProviderCLQ; 39 40 public ReentrantContextProviderTL(final int refType) { 41 super(refType); 42 43 final ReentrantContextProviderTL<K> parent = this; 44 45 this.ctxProviderCLQ = new ReentrantContextProviderCLQ<K>(refType) { 46 @Override 47 protected K newContext() { 48 return parent.newContext(); 49 } 50 }; 51 } 52 53 /** 54 * Give a ReentrantContext instance from thread-local or CLQ storage 55 * 56 * @return ReentrantContext instance 57 */ 58 @Override 59 public final K acquire() { 60 K ctx = null; 61 62 final Object ref = ctxTL.get(); 63 if (ref != null) { 64 ctx = refWrapper.resolveReference(ref); 65 } 66 if (ctx == null) { 67 // create a new ReentrantContext if none is available 68 ctx = newContext(); 69 // update thread local reference: 70 ctxTL.set(refWrapper.getOrCreateReference(ctx)); 71 } 72 // Check reentrance: 73 if (ctx.usage == USAGE_TL_INACTIVE) { 74 ctx.usage = USAGE_TL_IN_USE; 75 } else { 76 // get or create another ReentrantContext from CLQ provider: 77 ctx = ctxProviderCLQ.acquire(); 78 } 79 return ctx; 80 } 81 82 /** 83 * Restore the given ReentrantContext instance for reuse 84 * @param ctx ReentrantContext instance 85 */ 86 @Override 87 public final void release(final K ctx) { 88 if (ctx.usage == USAGE_TL_IN_USE) { 89 ctx.usage = USAGE_TL_INACTIVE; 90 } else { 91 ctxProviderCLQ.release(ctx); 92 } 93 } 94 }