test/jdk/java/net/httpclient/http2/server/Queue.java
author chegar
Thu, 16 Nov 2017 10:29:18 +0000
branchhttp-client-branch
changeset 55819 18e431209168
parent 55799 c71f52f48d97
child 55852 32f6aefec11e
permissions -rw-r--r--
http-client-branch: Update copyright years in tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     1
/*
55819
18e431209168 http-client-branch: Update copyright years in tests
chegar
parents: 55799
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
55799
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     4
 *
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    10
 *
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    15
 * accompanied this code).
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    16
 *
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    20
 *
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    23
 * questions.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    24
 */
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    25
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    26
import jdk.incubator.http.internal.common.ExceptionallyCloseable;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    27
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    28
import java.io.IOException;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    29
import java.util.LinkedList;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    30
import java.util.stream.Stream;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    31
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    32
// Each stream has one of these for input. Each Http2Connection has one
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    33
// for output. Can be used blocking or asynchronously.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    34
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    35
public class Queue<T> implements ExceptionallyCloseable {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    36
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    37
    private final LinkedList<T> q = new LinkedList<>();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    38
    private volatile boolean closed = false;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    39
    private volatile Throwable exception = null;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    40
    private Runnable callback;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    41
    private boolean callbackDisabled = false;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    42
    private int waiters; // true if someone waiting
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    43
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    44
    public synchronized int size() {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    45
        return q.size();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    46
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    47
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    48
//    public synchronized boolean tryPut(T obj) throws IOException {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    49
//        if (closed) return false;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    50
//        put(obj);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    51
//        return true;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    52
//    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    53
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    54
    public synchronized void put(T obj) throws IOException {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    55
        if (closed) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    56
            throw new IOException("stream closed");
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    57
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    58
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    59
        q.add(obj);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    60
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    61
        if (waiters > 0) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    62
            notifyAll();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    63
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    64
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    65
        if (callbackDisabled) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    66
            return;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    67
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    68
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    69
        if (q.size() > 0 && callback != null) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    70
            // Note: calling callback while holding the lock is
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    71
            // dangerous and may lead to deadlocks.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    72
            callback.run();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    73
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    74
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    75
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    76
//    public synchronized void disableCallback() {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    77
//        callbackDisabled = true;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    78
//    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    79
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    80
//    public synchronized void enableCallback() {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    81
//        callbackDisabled = false;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    82
//        while (q.size() > 0) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    83
//            callback.run();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    84
//        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    85
//    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    86
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    87
//    /**
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    88
//     * callback is invoked any time put is called where
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    89
//     * the Queue was empty.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    90
//     */
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    91
//    public synchronized void registerPutCallback(Runnable callback) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    92
//        Objects.requireNonNull(callback);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    93
//        this.callback = callback;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    94
//        if (q.size() > 0) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    95
//            // Note: calling callback while holding the lock is
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    96
//            // dangerous and may lead to deadlocks.
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    97
//            callback.run();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    98
//        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
    99
//    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   100
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   101
    @Override
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   102
    public synchronized void close() {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   103
        closed = true;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   104
        notifyAll();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   105
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   106
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   107
    @Override
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   108
    public synchronized void closeExceptionally(Throwable t) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   109
        if (exception == null) exception = t;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   110
        else if (t != null && t != exception) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   111
            if (!Stream.of(exception.getSuppressed())
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   112
                .filter(x -> x == t)
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   113
                .findFirst()
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   114
                .isPresent())
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   115
            {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   116
                exception.addSuppressed(t);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   117
            }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   118
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   119
        close();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   120
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   121
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   122
    public synchronized T take() throws IOException {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   123
        if (closed) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   124
            throw newIOException("stream closed");
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   125
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   126
        try {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   127
            while (q.size() == 0) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   128
                waiters++;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   129
                wait();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   130
                if (closed) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   131
                    throw newIOException("Queue closed");
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   132
                }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   133
                waiters--;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   134
            }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   135
            return q.removeFirst();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   136
        } catch (InterruptedException ex) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   137
            throw new IOException(ex);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   138
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   139
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   140
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   141
    public synchronized T poll() throws IOException {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   142
        if (closed) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   143
            throw newIOException("stream closed");
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   144
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   145
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   146
        if (q.isEmpty()) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   147
            return null;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   148
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   149
        T res = q.removeFirst();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   150
        return res;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   151
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   152
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   153
//    public synchronized T[] pollAll(T[] type) throws IOException {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   154
//        T[] ret = q.toArray(type);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   155
//        q.clear();
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   156
//        return ret;
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   157
//    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   158
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   159
//    public synchronized void pushback(T v) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   160
//        q.addFirst(v);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   161
//    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   162
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   163
//    public synchronized void pushbackAll(T[] v) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   164
//        for (int i=v.length-1; i>=0; i--) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   165
//            q.addFirst(v[i]);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   166
//        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   167
//    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   168
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   169
    private IOException newIOException(String msg) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   170
        if (exception == null) {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   171
            return new IOException(msg);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   172
        } else {
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   173
            return new IOException(msg, exception);
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   174
        }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   175
    }
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   176
c71f52f48d97 http-client-branch: (cleanup)
prappo
parents:
diff changeset
   177
}