private int doReadSocket(boolean block) {
Lock readLock = wrapper.getBlockingStatusReadLock();
WriteLock writeLock = wrapper.getBlockingStatusWriteLock();
boolean readDone = false;
int result = 0;
readLock.lock();
try {
if (wrapper.getBlockingStatus() == block) {
result = Socket.recvbb(socket, 0, buf.length - lastValid);
readDone = true;
}
} finally {
readLock.unlock();
}
if (!readDone) {
writeLock.lock();
try {
wrapper.setBlockingStatus(block);
// Set the current settings for this socket
if (block) {
Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 0);
} else {
Socket.optSet(socket, Socket.APR_SO_NONBLOCK, 1);
Socket.timeoutSet(socket, 0);
}
// Downgrade the lock
readLock.lock();
try {
writeLock.unlock();
result = Socket.recvbb(socket, 0, buf.length - lastValid);
} finally {
readLock.unlock();
}
} finally {
// Should have been released above but may not have been on some
// exception paths
if (writeLock.isHeldByCurrentThread()) {
writeLock.unlock();
}
}
}
return result;