Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/MessageQueueBackend.java
41161 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.utilities;2526/** The backend for the message queue abstraction. This class is27instantiated first and queried to provide the two "sides" of the28message queue. */2930import java.util.LinkedList;3132public class MessageQueueBackend {33// The two queues34private MessageQueueImpl leftRightQueue;35private MessageQueueImpl rightLeftQueue;3637public MessageQueueBackend() {38LinkedList<Object> leftRightPipe = new LinkedList<>();39LinkedList<Object> rightLeftPipe = new LinkedList<>();40leftRightQueue = new MessageQueueImpl(rightLeftPipe, leftRightPipe);41rightLeftQueue = new MessageQueueImpl(leftRightPipe, rightLeftPipe);42}4344/** Get one of the two symmetric sides of this message queue. */45public MessageQueue getFirstQueue() {46return leftRightQueue;47}4849/** Get second of the two symmetric sides of this message queue. */50public MessageQueue getSecondQueue() {51return rightLeftQueue;52}5354private class MessageQueueImpl implements MessageQueue {55private LinkedList<Object> readList;56private LinkedList<Object> writeList;5758public MessageQueueImpl(LinkedList<Object> listToReadFrom, LinkedList<Object> listToWriteTo) {59readList = listToReadFrom;60writeList = listToWriteTo;61}6263public Object readMessage() {64synchronized(readList) {65while (readList.isEmpty()) {66try {67readList.wait();68}69catch (InterruptedException e) {70}71}72return readList.removeFirst();73}74}7576public Object readMessageWithTimeout(long millis) {77synchronized(readList) {78if (readList.isEmpty()) {79if (millis == 0) {80return null;81}82try {83readList.wait(millis);84}85catch (InterruptedException e) {86}87}88// If list is still empty after wait, return null89if (readList.isEmpty()) {90return null;91}92return readList.removeFirst();93}94}9596public void writeMessage(Object obj) {97synchronized(writeList) {98writeList.addLast(obj);99writeList.notify();100}101}102}103}104105106