Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 17 | package org.apache.felix.shell.remote; |
| 18 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 19 | /** |
| 20 | * Implements a reentrant lock. |
| 21 | * <p/> |
| 22 | * Public domain code. |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 23 | */ |
| 24 | class ReentrantLock |
| 25 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 26 | protected Thread m_owner = null; |
| 27 | protected long m_holds = 0; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 28 | |
| 29 | public void acquire() throws InterruptedException |
| 30 | { |
| 31 | //log.debug("acquire()::" + Thread.currentThread().toString()); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 32 | if (Thread.interrupted()) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 33 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 34 | throw new InterruptedException(); |
| 35 | } |
| 36 | Thread caller = Thread.currentThread(); |
| 37 | synchronized (this) |
| 38 | { |
| 39 | if (caller == m_owner) |
| 40 | { |
| 41 | ++m_holds; |
| 42 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 43 | else |
| 44 | { |
| 45 | try |
| 46 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 47 | while (m_owner != null) |
| 48 | { |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 49 | wait(); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 50 | } |
| 51 | m_owner = caller; |
| 52 | m_holds = 1; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 53 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 54 | catch (InterruptedException ex) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 55 | { |
| 56 | notify(); |
| 57 | throw ex; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | }//acquire |
| 62 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 63 | public boolean attempt(long msecs) throws InterruptedException |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 64 | { |
| 65 | //log.debug("attempt()::" + Thread.currentThread().toString()); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 66 | if (Thread.interrupted()) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 67 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 68 | throw new InterruptedException(); |
| 69 | } |
| 70 | Thread caller = Thread.currentThread(); |
| 71 | synchronized (this) |
| 72 | { |
| 73 | if (caller == m_owner) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 74 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 75 | ++m_holds; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 78 | else if (m_owner == null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 79 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 80 | m_owner = caller; |
| 81 | m_holds = 1; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 82 | return true; |
| 83 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 84 | else if (msecs <= 0) |
| 85 | { |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 86 | return false; |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 87 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 88 | else |
| 89 | { |
| 90 | long waitTime = msecs; |
| 91 | long start = System.currentTimeMillis(); |
| 92 | try |
| 93 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 94 | for (;;) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 95 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 96 | wait(waitTime); |
| 97 | if (caller == m_owner) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 98 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 99 | ++m_holds; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 100 | return true; |
| 101 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 102 | else if (m_owner == null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 103 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 104 | m_owner = caller; |
| 105 | m_holds = 1; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | else |
| 109 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 110 | waitTime = msecs - (System.currentTimeMillis() - start); |
| 111 | if (waitTime <= 0) |
| 112 | { |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 113 | return false; |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 114 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 118 | catch (InterruptedException ex) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 119 | { |
| 120 | notify(); |
| 121 | throw ex; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | }//attempt |
| 126 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 127 | /** |
| 128 | * Release the lock. |
| 129 | * |
| 130 | * @throws Error thrown if not current owner of lock |
| 131 | */ |
| 132 | public synchronized void release() |
| 133 | { |
| 134 | //log.debug("release()::" + Thread.currentThread().toString()); |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 135 | if (Thread.currentThread() != m_owner) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 136 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 137 | throw new Error("Illegal Lock usage"); |
| 138 | } |
| 139 | |
| 140 | if (--m_holds == 0) |
| 141 | { |
| 142 | m_owner = null; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 143 | notify(); |
| 144 | } |
| 145 | }//release |
| 146 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 147 | /** |
| 148 | * Release the lock N times. <code>release(n)</code> is |
| 149 | * equivalent in effect to: |
| 150 | * <pre> |
| 151 | * for (int i = 0; i < n; ++i) release(); |
| 152 | * </pre> |
| 153 | * <p/> |
| 154 | * |
| 155 | * @param n times the lock should be released. |
| 156 | * @throws Error thrown if not current owner of lock |
| 157 | * or has fewer than N holds on the lock |
| 158 | */ |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 159 | public synchronized void release(long n) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 160 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 161 | if (Thread.currentThread() != m_owner || n > m_holds) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 162 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 163 | throw new Error("Illegal Lock usage"); |
| 164 | } |
| 165 | |
| 166 | m_holds -= n; |
| 167 | if (m_holds == 0) |
| 168 | { |
| 169 | m_owner = null; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 170 | notify(); |
| 171 | } |
| 172 | }//release |
| 173 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 174 | /** |
| 175 | * Return the number of unreleased acquires performed |
| 176 | * by the current thread. |
| 177 | * Returns zero if current thread does not hold lock. |
| 178 | * |
| 179 | * @return the number of unreleased acquires performed by the owner thread. |
| 180 | */ |
| 181 | public synchronized long holds() |
| 182 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 183 | if (Thread.currentThread() != m_owner) |
| 184 | { |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 185 | return 0; |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 186 | } |
| 187 | return m_holds; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 188 | }//holds |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 189 | }//class ReentrantLock |