[e-cvs] cvs commit: e/src/jsrc/org/erights/e/elib/io ReaderWrapper.java TextReader.java
markm@eros.cs.jhu.edu
markm@eros.cs.jhu.edu
Tue, 28 Aug 2001 02:40:10 -0400
markm 01/08/28 02:40:10
Added: src/jsrc/org/erights/e/elib/io ReaderWrapper.java
TextReader.java
Log:
start on asynch reader
Revision Changes Path
1.1 e/src/jsrc/org/erights/e/elib/io/ReaderWrapper.java
Index: ReaderWrapper.java
===================================================================
package org.erights.e.elib.io;
/*
The contents of this file are subject to the Improvements to the
Distributed E Language Implementation License Version 1.0 (the
"License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.erights.org/mmlicense.html
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
The Original Code is the Improvements to the Distributed E Language
Implementation, released May 27, 1999.
The Initial Developer of the Original Code is Mark S. Miller.
Copyright (C) 1999 Mark S. Miller. All Rights Reserved.
Contributor(s): ______________________________________.
*/
import java.io.IOException;
import java.io.Reader;
import org.erights.e.elib.prim.E;
import org.erights.e.elib.prim.Message;
import org.erights.e.elib.prim.Runner;
import org.erights.e.elib.prim.SynchQueue;
import org.erights.e.elib.tables.FlexList;
/**
* Used only in the implementation of TextReader, to be run() in its own
* Thread.
*
* @author <a href="mailto:markm@caplet.com">Mark S. Miller</a>
* @author <a href="mailto:tstanley@cocoon.com">Terry Stanley</a>
*/
public class ReaderWrapper implements Runnable {
private Reader myWrapped;
private Object myQLock;
private SynchQueue myAsynchRequests;
/** If null, then not processing a request */
private Message myOptMsg;
/** for resolving Resolvers */
private Runner myCreator;
/**
* Will block reading. XXX should create this thread lazily, so that a
* wrapped Reader that's always ready will not need a thread.
*/
private Thread myReadingThread;
/** Assumes it has exclusive access to 'wrapped' */
public ReaderWrapper(Reader wrapped) {
myWrapped = wrapped;
myQLock = new Object();
myAsynchRequests = new SynchQueue(Message.class, myQLock);
myCreator = Runner.currentRunner();
myReadingThread = new Thread(this, "ReaderWrapper");
myReadingThread.start();
}
/**
*
*/
public void run() {
while (true) {
// If blocked on dequeue, myOptMsg will be null
synchronized (myQLock) {
myOptMsg = null;
//ensure assignment happens while still holding myQLock
myOptMsg = (Message)myAsynchRequests.dequeue();
}
// Using E.call(..) in a non-vat thread!
// If blocked performing the message, or if performing the
// message, myOptMsg will not be null.
Object result = E.callAll(this, myOptMsg.verb(), myOptMsg.args());
Object[] resolveArgs = { result };
myCreator.sendAllOnly(myOptMsg.resolver(),
"resolve",
resolveArgs);
}
}
/**
* If we're not in the midst of a request, the queue is empty, and the
* wrapped Reader is ready, then we're ready
*/
public boolean ready() throws IOException {
synchronized (myQLock) {
return myOptMsg == null &&
! myAsynchRequests.hasMoreElements() &&
myWrapped.ready();
}
}
}
1.1 e/src/jsrc/org/erights/e/elib/io/TextReader.java
Index: TextReader.java
===================================================================
package org.erights.e.elib.io;
/*
The contents of this file are subject to the Improvements to the
Distributed E Language Implementation License Version 1.0 (the
"License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.erights.org/mmlicense.html
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
The Original Code is the Improvements to the Distributed E Language
Implementation, released May 27, 1999.
The Initial Developer of the Original Code is Mark S. Miller.
Copyright (C) 1999 Mark S. Miller. All Rights Reserved.
Contributor(s): ______________________________________.
*/
import java.io.IOException;
import java.io.Reader;
import org.erights.e.elib.prim.Message;
import org.erights.e.elib.prim.Runner;
import org.erights.e.elib.tables.FlexList;
/**
* Turns a Reader into a non-blocking device.
* <p>
* A TextReader wraps a Reader for use the vat/Runner that created the
* TextReader. It spins off a separate thread to block on the wrapped
* Reader, in order to resolve promises for asynchronous requests to the
* TextReader.
* <p>
* A TextReader also supports synchronous requests, but these only succeed if
* there are no prior unsatisfied asynch requests, and if there are enough
* characters ready to satisfy the synch request without blocking.
* <p>
* If the wrapped Reader never says it's ready(), then the wrapping
* TextReader won't allow synchronous requests. A TextReader does not
* currently support mark() or reset() even if the wrapped Reader does. It's
* unclear whether this will change.
*
* @author <a href="mailto:markm@caplet.com">Mark S. Miller</a>
* @author <a href="mailto:tstanley@cocoon.com">Terry Stanley</a>
*/
public class TextReader extends Reader {
private Reader myWrapped;
private ReaderWrapper myOptWrapper;
public TextReader(Reader wrapped) {
myWrapped = wrapped;
myOptWrapper = null;
}
/**
*
*/
public void close() throws IOException {
if (null == myOptWrapper) {
myWrapped.close();
} else {
//XXX...
}
}
/**
*
*/
public int read(char[] cbuf, int off, int len) throws IOException {
if (! ready()) {
throw new IOException("would block");
}
int result = 0;
for (int i = off; i < off + len; i++) {
int c = myWrapped.read();
if (-1 == c) {
return result;
}
cbuf[i] = (char)c;
result++;
if (! ready()) {
return result;
}
}
return result;
}
/**
*
*/
public boolean ready() throws IOException {
if (null == myOptWrapper) {
return myWrapped.ready();
} else {
return myOptWrapper.ready();
}
}
}