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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/tsccm/WaitingThread.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,196 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + *
     1.7 + *  Licensed to the Apache Software Foundation (ASF) under one or more
     1.8 + *  contributor license agreements.  See the NOTICE file distributed with
     1.9 + *  this work for additional information regarding copyright ownership.
    1.10 + *  The ASF licenses this file to You under the Apache License, Version 2.0
    1.11 + *  (the "License"); you may not use this file except in compliance with
    1.12 + *  the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + *  Unless required by applicable law or agreed to in writing, software
    1.17 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.18 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.19 + *  See the License for the specific language governing permissions and
    1.20 + *  limitations under the License.
    1.21 + * ====================================================================
    1.22 + *
    1.23 + * This software consists of voluntary contributions made by many
    1.24 + * individuals on behalf of the Apache Software Foundation.  For more
    1.25 + * information on the Apache Software Foundation, please see
    1.26 + * <http://www.apache.org/>.
    1.27 + *
    1.28 + */
    1.29 +
    1.30 +package ch.boye.httpclientandroidlib.impl.conn.tsccm;
    1.31 +
    1.32 +
    1.33 +import java.util.Date;
    1.34 +import java.util.concurrent.locks.Condition;
    1.35 +
    1.36 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
    1.37 +
    1.38 +/**
    1.39 + * Represents a thread waiting for a connection.
    1.40 + * This class implements throwaway objects. It is instantiated whenever
    1.41 + * a thread needs to wait. Instances are not re-used, except if the
    1.42 + * waiting thread experiences a spurious wakeup and continues to wait.
    1.43 + * <br/>
    1.44 + * All methods assume external synchronization on the condition
    1.45 + * passed to the constructor.
    1.46 + * Instances of this class do <i>not</i> synchronize access!
    1.47 + *
    1.48 + *
    1.49 + * @since 4.0
    1.50 + */
    1.51 +@NotThreadSafe
    1.52 +public class WaitingThread {
    1.53 +
    1.54 +    /** The condition on which the thread is waiting. */
    1.55 +    private final Condition cond;
    1.56 +
    1.57 +    /** The route specific pool on which the thread is waiting. */
    1.58 +    //@@@ replace with generic pool interface
    1.59 +    private final RouteSpecificPool pool;
    1.60 +
    1.61 +    /** The thread that is waiting for an entry. */
    1.62 +    private Thread waiter;
    1.63 +
    1.64 +    /** True if this was interrupted. */
    1.65 +    private boolean aborted;
    1.66 +
    1.67 +
    1.68 +    /**
    1.69 +     * Creates a new entry for a waiting thread.
    1.70 +     *
    1.71 +     * @param cond      the condition for which to wait
    1.72 +     * @param pool      the pool on which the thread will be waiting,
    1.73 +     *                  or <code>null</code>
    1.74 +     */
    1.75 +    public WaitingThread(Condition cond, RouteSpecificPool pool) {
    1.76 +
    1.77 +        if (cond == null) {
    1.78 +            throw new IllegalArgumentException("Condition must not be null.");
    1.79 +        }
    1.80 +
    1.81 +        this.cond = cond;
    1.82 +        this.pool = pool;
    1.83 +    }
    1.84 +
    1.85 +
    1.86 +    /**
    1.87 +     * Obtains the condition.
    1.88 +     *
    1.89 +     * @return  the condition on which to wait, never <code>null</code>
    1.90 +     */
    1.91 +    public final Condition getCondition() {
    1.92 +        // not synchronized
    1.93 +        return this.cond;
    1.94 +    }
    1.95 +
    1.96 +
    1.97 +    /**
    1.98 +     * Obtains the pool, if there is one.
    1.99 +     *
   1.100 +     * @return  the pool on which a thread is or was waiting,
   1.101 +     *          or <code>null</code>
   1.102 +     */
   1.103 +    public final RouteSpecificPool getPool() {
   1.104 +        // not synchronized
   1.105 +        return this.pool;
   1.106 +    }
   1.107 +
   1.108 +
   1.109 +    /**
   1.110 +     * Obtains the thread, if there is one.
   1.111 +     *
   1.112 +     * @return  the thread which is waiting, or <code>null</code>
   1.113 +     */
   1.114 +    public final Thread getThread() {
   1.115 +        // not synchronized
   1.116 +        return this.waiter;
   1.117 +    }
   1.118 +
   1.119 +
   1.120 +    /**
   1.121 +     * Blocks the calling thread.
   1.122 +     * This method returns when the thread is notified or interrupted,
   1.123 +     * if a timeout occurrs, or if there is a spurious wakeup.
   1.124 +     * <br/>
   1.125 +     * This method assumes external synchronization.
   1.126 +     *
   1.127 +     * @param deadline  when to time out, or <code>null</code> for no timeout
   1.128 +     *
   1.129 +     * @return  <code>true</code> if the condition was satisfied,
   1.130 +     *          <code>false</code> in case of a timeout.
   1.131 +     *          Typically, a call to {@link #wakeup} is used to indicate
   1.132 +     *          that the condition was satisfied. Since the condition is
   1.133 +     *          accessible outside, this cannot be guaranteed though.
   1.134 +     *
   1.135 +     * @throws InterruptedException     if the waiting thread was interrupted
   1.136 +     *
   1.137 +     * @see #wakeup
   1.138 +     */
   1.139 +    public boolean await(Date deadline)
   1.140 +        throws InterruptedException {
   1.141 +
   1.142 +        // This is only a sanity check. We cannot synchronize here,
   1.143 +        // the lock would not be released on calling cond.await() below.
   1.144 +        if (this.waiter != null) {
   1.145 +            throw new IllegalStateException
   1.146 +                ("A thread is already waiting on this object." +
   1.147 +                 "\ncaller: " + Thread.currentThread() +
   1.148 +                 "\nwaiter: " + this.waiter);
   1.149 +        }
   1.150 +
   1.151 +        if (aborted)
   1.152 +            throw new InterruptedException("Operation interrupted");
   1.153 +
   1.154 +        this.waiter = Thread.currentThread();
   1.155 +
   1.156 +        boolean success = false;
   1.157 +        try {
   1.158 +            if (deadline != null) {
   1.159 +                success = this.cond.awaitUntil(deadline);
   1.160 +            } else {
   1.161 +                this.cond.await();
   1.162 +                success = true;
   1.163 +            }
   1.164 +            if (aborted)
   1.165 +                throw new InterruptedException("Operation interrupted");
   1.166 +        } finally {
   1.167 +            this.waiter = null;
   1.168 +        }
   1.169 +        return success;
   1.170 +
   1.171 +    } // await
   1.172 +
   1.173 +
   1.174 +    /**
   1.175 +     * Wakes up the waiting thread.
   1.176 +     * <br/>
   1.177 +     * This method assumes external synchronization.
   1.178 +     */
   1.179 +    public void wakeup() {
   1.180 +
   1.181 +        // If external synchronization and pooling works properly,
   1.182 +        // this cannot happen. Just a sanity check.
   1.183 +        if (this.waiter == null) {
   1.184 +            throw new IllegalStateException
   1.185 +                ("Nobody waiting on this object.");
   1.186 +        }
   1.187 +
   1.188 +        // One condition might be shared by several WaitingThread instances.
   1.189 +        // It probably isn't, but just in case: wake all, not just one.
   1.190 +        this.cond.signalAll();
   1.191 +    }
   1.192 +
   1.193 +    public void interrupt() {
   1.194 +        aborted = true;
   1.195 +        this.cond.signalAll();
   1.196 +    }
   1.197 +
   1.198 +
   1.199 +} // class WaitingThread

mercurial