mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/tsccm/WaitingThread.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 *
michael@0 4 * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0 5 * contributor license agreements. See the NOTICE file distributed with
michael@0 6 * this work for additional information regarding copyright ownership.
michael@0 7 * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0 8 * (the "License"); you may not use this file except in compliance with
michael@0 9 * the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing, software
michael@0 14 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 16 * See the License for the specific language governing permissions and
michael@0 17 * limitations under the License.
michael@0 18 * ====================================================================
michael@0 19 *
michael@0 20 * This software consists of voluntary contributions made by many
michael@0 21 * individuals on behalf of the Apache Software Foundation. For more
michael@0 22 * information on the Apache Software Foundation, please see
michael@0 23 * <http://www.apache.org/>.
michael@0 24 *
michael@0 25 */
michael@0 26
michael@0 27 package ch.boye.httpclientandroidlib.impl.conn.tsccm;
michael@0 28
michael@0 29
michael@0 30 import java.util.Date;
michael@0 31 import java.util.concurrent.locks.Condition;
michael@0 32
michael@0 33 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0 34
michael@0 35 /**
michael@0 36 * Represents a thread waiting for a connection.
michael@0 37 * This class implements throwaway objects. It is instantiated whenever
michael@0 38 * a thread needs to wait. Instances are not re-used, except if the
michael@0 39 * waiting thread experiences a spurious wakeup and continues to wait.
michael@0 40 * <br/>
michael@0 41 * All methods assume external synchronization on the condition
michael@0 42 * passed to the constructor.
michael@0 43 * Instances of this class do <i>not</i> synchronize access!
michael@0 44 *
michael@0 45 *
michael@0 46 * @since 4.0
michael@0 47 */
michael@0 48 @NotThreadSafe
michael@0 49 public class WaitingThread {
michael@0 50
michael@0 51 /** The condition on which the thread is waiting. */
michael@0 52 private final Condition cond;
michael@0 53
michael@0 54 /** The route specific pool on which the thread is waiting. */
michael@0 55 //@@@ replace with generic pool interface
michael@0 56 private final RouteSpecificPool pool;
michael@0 57
michael@0 58 /** The thread that is waiting for an entry. */
michael@0 59 private Thread waiter;
michael@0 60
michael@0 61 /** True if this was interrupted. */
michael@0 62 private boolean aborted;
michael@0 63
michael@0 64
michael@0 65 /**
michael@0 66 * Creates a new entry for a waiting thread.
michael@0 67 *
michael@0 68 * @param cond the condition for which to wait
michael@0 69 * @param pool the pool on which the thread will be waiting,
michael@0 70 * or <code>null</code>
michael@0 71 */
michael@0 72 public WaitingThread(Condition cond, RouteSpecificPool pool) {
michael@0 73
michael@0 74 if (cond == null) {
michael@0 75 throw new IllegalArgumentException("Condition must not be null.");
michael@0 76 }
michael@0 77
michael@0 78 this.cond = cond;
michael@0 79 this.pool = pool;
michael@0 80 }
michael@0 81
michael@0 82
michael@0 83 /**
michael@0 84 * Obtains the condition.
michael@0 85 *
michael@0 86 * @return the condition on which to wait, never <code>null</code>
michael@0 87 */
michael@0 88 public final Condition getCondition() {
michael@0 89 // not synchronized
michael@0 90 return this.cond;
michael@0 91 }
michael@0 92
michael@0 93
michael@0 94 /**
michael@0 95 * Obtains the pool, if there is one.
michael@0 96 *
michael@0 97 * @return the pool on which a thread is or was waiting,
michael@0 98 * or <code>null</code>
michael@0 99 */
michael@0 100 public final RouteSpecificPool getPool() {
michael@0 101 // not synchronized
michael@0 102 return this.pool;
michael@0 103 }
michael@0 104
michael@0 105
michael@0 106 /**
michael@0 107 * Obtains the thread, if there is one.
michael@0 108 *
michael@0 109 * @return the thread which is waiting, or <code>null</code>
michael@0 110 */
michael@0 111 public final Thread getThread() {
michael@0 112 // not synchronized
michael@0 113 return this.waiter;
michael@0 114 }
michael@0 115
michael@0 116
michael@0 117 /**
michael@0 118 * Blocks the calling thread.
michael@0 119 * This method returns when the thread is notified or interrupted,
michael@0 120 * if a timeout occurrs, or if there is a spurious wakeup.
michael@0 121 * <br/>
michael@0 122 * This method assumes external synchronization.
michael@0 123 *
michael@0 124 * @param deadline when to time out, or <code>null</code> for no timeout
michael@0 125 *
michael@0 126 * @return <code>true</code> if the condition was satisfied,
michael@0 127 * <code>false</code> in case of a timeout.
michael@0 128 * Typically, a call to {@link #wakeup} is used to indicate
michael@0 129 * that the condition was satisfied. Since the condition is
michael@0 130 * accessible outside, this cannot be guaranteed though.
michael@0 131 *
michael@0 132 * @throws InterruptedException if the waiting thread was interrupted
michael@0 133 *
michael@0 134 * @see #wakeup
michael@0 135 */
michael@0 136 public boolean await(Date deadline)
michael@0 137 throws InterruptedException {
michael@0 138
michael@0 139 // This is only a sanity check. We cannot synchronize here,
michael@0 140 // the lock would not be released on calling cond.await() below.
michael@0 141 if (this.waiter != null) {
michael@0 142 throw new IllegalStateException
michael@0 143 ("A thread is already waiting on this object." +
michael@0 144 "\ncaller: " + Thread.currentThread() +
michael@0 145 "\nwaiter: " + this.waiter);
michael@0 146 }
michael@0 147
michael@0 148 if (aborted)
michael@0 149 throw new InterruptedException("Operation interrupted");
michael@0 150
michael@0 151 this.waiter = Thread.currentThread();
michael@0 152
michael@0 153 boolean success = false;
michael@0 154 try {
michael@0 155 if (deadline != null) {
michael@0 156 success = this.cond.awaitUntil(deadline);
michael@0 157 } else {
michael@0 158 this.cond.await();
michael@0 159 success = true;
michael@0 160 }
michael@0 161 if (aborted)
michael@0 162 throw new InterruptedException("Operation interrupted");
michael@0 163 } finally {
michael@0 164 this.waiter = null;
michael@0 165 }
michael@0 166 return success;
michael@0 167
michael@0 168 } // await
michael@0 169
michael@0 170
michael@0 171 /**
michael@0 172 * Wakes up the waiting thread.
michael@0 173 * <br/>
michael@0 174 * This method assumes external synchronization.
michael@0 175 */
michael@0 176 public void wakeup() {
michael@0 177
michael@0 178 // If external synchronization and pooling works properly,
michael@0 179 // this cannot happen. Just a sanity check.
michael@0 180 if (this.waiter == null) {
michael@0 181 throw new IllegalStateException
michael@0 182 ("Nobody waiting on this object.");
michael@0 183 }
michael@0 184
michael@0 185 // One condition might be shared by several WaitingThread instances.
michael@0 186 // It probably isn't, but just in case: wake all, not just one.
michael@0 187 this.cond.signalAll();
michael@0 188 }
michael@0 189
michael@0 190 public void interrupt() {
michael@0 191 aborted = true;
michael@0 192 this.cond.signalAll();
michael@0 193 }
michael@0 194
michael@0 195
michael@0 196 } // class WaitingThread

mercurial