1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
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
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")
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
89 public void signalAll() {
90 lock.lock();
91 try {
92 waitCond.signalAll();
93 } finally {
94 lock.unlock();
95 }
96 }
97
98 @Override
99 public void clear() {
100 lock.lock();
101 try {
102 runnables.clear();
103 } finally {
104 lock.unlock();
105 }
106 }
107
108 @Override
109 public int size() {
110 lock.lock();
111 try {
112 return runnables.size();
113 } finally {
114 lock.unlock();
115 }
116 }
117
118 @Override
119 public void completionCleanup(Procedure proc) {
120 }
121 }