1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.hbase.procedure2; 20 21 import java.util.ArrayDeque; 22 import java.util.Deque; 23 import java.util.concurrent.locks.Condition; 24 import java.util.concurrent.locks.ReentrantLock; 25 26 import org.apache.hadoop.hbase.classification.InterfaceAudience; 27 import org.apache.hadoop.hbase.classification.InterfaceStability; 28 29 /** 30 * Simple runqueue for the procedures 31 */ 32 @InterfaceAudience.Private 33 @InterfaceStability.Evolving 34 public class ProcedureSimpleRunQueue implements ProcedureRunnableSet { 35 private final Deque<Procedure> runnables = new ArrayDeque<Procedure>(); 36 private final ReentrantLock lock = new ReentrantLock(); 37 private final Condition waitCond = lock.newCondition(); 38 39 @Override addFront(final Procedure proc)40 public void addFront(final Procedure proc) { 41 lock.lock(); 42 try { 43 runnables.addFirst(proc); 44 waitCond.signal(); 45 } finally { 46 lock.unlock(); 47 } 48 } 49 50 @Override addBack(final Procedure proc)51 public void addBack(final Procedure proc) { 52 lock.lock(); 53 try { 54 runnables.addLast(proc); 55 waitCond.signal(); 56 } finally { 57 lock.unlock(); 58 } 59 } 60 61 @Override yield(final Procedure proc)62 public void yield(final Procedure proc) { 63 addBack(proc); 64 } 65 66 @Override 67 @edu.umd.cs.findbugs.annotations.SuppressWarnings("WA_AWAIT_NOT_IN_LOOP") poll()68 public Procedure poll() { 69 lock.lock(); 70 try { 71 if (runnables.isEmpty()) { 72 waitCond.await(); 73 if (!runnables.isEmpty()) { 74 return runnables.pop(); 75 } 76 } else { 77 return runnables.pop(); 78 } 79 } catch (InterruptedException e) { 80 Thread.currentThread().interrupt(); 81 return null; 82 } finally { 83 lock.unlock(); 84 } 85 return null; 86 } 87 88 @Override signalAll()89 public void signalAll() { 90 lock.lock(); 91 try { 92 waitCond.signalAll(); 93 } finally { 94 lock.unlock(); 95 } 96 } 97 98 @Override clear()99 public void clear() { 100 lock.lock(); 101 try { 102 runnables.clear(); 103 } finally { 104 lock.unlock(); 105 } 106 } 107 108 @Override size()109 public int size() { 110 lock.lock(); 111 try { 112 return runnables.size(); 113 } finally { 114 lock.unlock(); 115 } 116 } 117 118 @Override completionCleanup(Procedure proc)119 public void completionCleanup(Procedure proc) { 120 } 121 }