1 //****************************************************************************** 2 // 3 // File: IORequestList.java 4 // Package: edu.rit.mp 5 // Unit: Class edu.rit.mp.IORequestList 6 // 7 // This Java source file is copyright (C) 2006 by Alan Kaminsky. All rights 8 // reserved. For further information, contact the author, Alan Kaminsky, at 9 // ark@cs.rit.edu. 10 // 11 // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free 12 // software; you can redistribute it and/or modify it under the terms of the GNU 13 // General Public License as published by the Free Software Foundation; either 14 // version 3 of the License, or (at your option) any later version. 15 // 16 // PJ is distributed in the hope that it will be useful, but WITHOUT ANY 17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 18 // A PARTICULAR PURPOSE. See the GNU General Public License for more details. 19 // 20 // Linking this library statically or dynamically with other modules is making a 21 // combined work based on this library. Thus, the terms and conditions of the GNU 22 // General Public License cover the whole combination. 23 // 24 // As a special exception, the copyright holders of this library give you 25 // permission to link this library with independent modules to produce an 26 // executable, regardless of the license terms of these independent modules, and 27 // to copy and distribute the resulting executable under terms of your choice, 28 // provided that you also meet, for each linked independent module, the terms 29 // and conditions of the license of that module. An independent module is a module 30 // which is not derived from or based on this library. If you modify this library, 31 // you may extend this exception to your version of the library, but you are not 32 // obligated to do so. If you do not wish to do so, delete this exception 33 // statement from your version. 34 // 35 // A copy of the GNU General Public License is provided in the file gpl.txt. You 36 // may also obtain a copy of the GNU General Public License on the World Wide 37 // Web at http://www.gnu.org/licenses/gpl.html. 38 // 39 //****************************************************************************** 40 package edu.rit.mp; 41 42 import java.io.IOException; 43 import java.io.PrintStream; 44 import java.util.Iterator; 45 import java.util.LinkedList; 46 47 /** 48 * Class IORequestList provides a list of pending I/O requests in the Message 49 * Protocol (MP). 50 * 51 * @author Alan Kaminsky 52 * @version 26-Oct-2006 53 */ 54 class IORequestList { 55 56 // Hidden data members. 57 LinkedList<IORequest> myList = new LinkedList<IORequest>(); 58 59 // Exported constructors. 60 /** 61 * Construct a new I/O request list. 62 */ 63 public IORequestList() { 64 } 65 66 // Exported operations. 67 /** 68 * Add the given I/O request to this list. This list maintains the I/O 69 * requests in FIFO order. 70 * 71 * @param theIORequest I/O request. 72 */ 73 public synchronized void add(IORequest theIORequest) { 74 myList.add(theIORequest); 75 notifyAll(); 76 } 77 78 /** 79 * Remove an I/O request from this list that matches the given I/O request. 80 * The first matching I/O request, if any, is returned. 81 * 82 * @param theIORequest I/O request. 83 * @return Matching I/O request removed from this list, or null if there was 84 * no match. 85 */ 86 public synchronized IORequest removeMatch(IORequest theIORequest) { 87 Iterator<IORequest> iter = myList.iterator(); 88 while (iter.hasNext()) { 89 IORequest iorequest = iter.next(); 90 if (iorequest.match(theIORequest)) { 91 iter.remove(); 92 return iorequest; 93 } 94 } 95 return null; 96 } 97 98 /** 99 * Remove an I/O request from this list that matches the given channel, 100 * message tag, and message type. The first matching I/O request, if any, is 101 * returned. 102 * 103 * @param channel Channel. 104 * @param tag Message tag. 105 * @param type Message type. 106 * @return Matching I/O request removed from this list, or null if there was 107 * no match. 108 */ 109 public synchronized IORequest removeMatch(Channel channel, 110 Integer tag, 111 byte type) { 112 Iterator<IORequest> iter = myList.iterator(); 113 while (iter.hasNext()) { 114 IORequest iorequest = iter.next(); 115 if (iorequest.match(channel, tag, type)) { 116 iter.remove(); 117 return iorequest; 118 } 119 } 120 return null; 121 } 122 123 /** 124 * Wait for an I/O request that matches the given I/O request to be added to 125 * this list, then remove it. The first matching I/O request is returned. 126 * 127 * @param theIORequest I/O request. 128 * @return Matching I/O request removed from this list. 129 * @exception InterruptedException Thrown if the calling thread was 130 * interrupted while blocked in this method. 131 * @throws java.lang.InterruptedException if any. 132 */ 133 public synchronized IORequest waitForMatch(IORequest theIORequest) 134 throws InterruptedException { 135 IORequest iorequest; 136 while ((iorequest = removeMatch(theIORequest)) == null) { 137 //System.out.println ("IORequest.waitForMatch("+theIORequest+") waiting, myList="+this); 138 wait(); 139 } 140 return iorequest; 141 } 142 143 /** 144 * Wait for an I/O request that matches the given channel, message tag, and 145 * message type to be added to this list, then remove it. The first matching 146 * I/O request is returned. 147 * 148 * @return Matching I/O request removed from this list. 149 * @exception InterruptedException Thrown if the calling thread was 150 * interrupted while blocked in this method. 151 * @param channel a {@link edu.rit.mp.Channel} object. 152 * @param tag a {@link java.lang.Integer} object. 153 * @param type a byte. 154 * @throws java.lang.InterruptedException if any. 155 */ 156 public synchronized IORequest waitForMatch(Channel channel, 157 Integer tag, 158 byte type) 159 throws InterruptedException { 160 IORequest iorequest; 161 while ((iorequest = removeMatch(channel, tag, type)) == null) { 162 //System.out.println ("IORequest.waitForMatch("+channel+","+tag+","+type+") waiting, myList="+this); 163 wait(); 164 } 165 return iorequest; 166 } 167 168 /** 169 * Report that every I/O request in this list failed with an I/O exception. 170 * 171 * @param theIOException I/O exception. 172 */ 173 public synchronized void reportFailure(IOException theIOException) { 174 IORequest iorequest = null; 175 while (!myList.isEmpty()) { 176 iorequest = myList.remove(0); 177 iorequest.reportFailure(theIOException); 178 } 179 } 180 181 /** 182 * Dump the state of this I/O request list on the given print stream. For 183 * debugging. 184 * 185 * @param out Print stream. 186 * @param prefix String to print at the beginning of each line. 187 */ 188 public synchronized void dump(PrintStream out, 189 String prefix) { 190 out.println(prefix + getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(this))); 191 out.println(prefix + "\t" + myList.size() + " entries"); 192 for (IORequest r : myList) { 193 out.println(prefix + "\t" + r); 194 } 195 } 196 197 }