private int doReadSocket(boolean block) {
Lock readLock = wrapper.getBlockingStatusReadLock();
WriteLock writeLock = wrapper.getBlockingStatusWriteLock();
boolean readDone = false;
int result = 0;
try {
readLock.lock();
if (wrapper.getBlockingStatus() == block) {
result = Socket.recvbb(socket, 0, buf.length - lastValid);
readDone = true;
}
} finally {
readLock.unlock();
}
if (!readDone) {
try {
writeLock.lock();
wrapper.setBlockingStatus(block);
// Set the current settings for this socket
Socket.optSet(socket, Socket.APR_SO_NONBLOCK, (block ? 0 : 1));
// Downgrade the lock
try {
readLock.lock();
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;